Fix #551 Detect unused Private member variables (#4973)

This commit is contained in:
chrchr-github 2023-04-17 20:34:39 +02:00 committed by GitHub
parent 029056b0b4
commit c3002f1230
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 7 deletions

View File

@ -1437,7 +1437,7 @@ void CheckUnusedVar::checkStructMemberUsage()
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (const Scope &scope : symbolDatabase->scopeList) {
if (scope.type != Scope::eStruct && scope.type != Scope::eUnion)
if (scope.type != Scope::eStruct && scope.type != Scope::eClass && scope.type != Scope::eUnion)
continue;
if (scope.bodyStart->fileIndex() != 0 || scope.className.empty())
@ -1521,16 +1521,21 @@ void CheckUnusedVar::checkStructMemberUsage()
break;
}
}
if (!use)
unusedStructMemberError(var.nameToken(), scope.className, var.name(), scope.type == Scope::eUnion);
if (!use) {
std::string prefix = "struct";
if (scope.type == Scope::ScopeType::eClass)
prefix = "class";
else if (scope.type == Scope::ScopeType::eUnion)
prefix = "union";
unusedStructMemberError(var.nameToken(), scope.className, var.name(), prefix);
}
}
}
}
void CheckUnusedVar::unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname, bool isUnion)
void CheckUnusedVar::unusedStructMemberError(const Token* tok, const std::string& structname, const std::string& varname, const std::string& prefix)
{
const std::string prefix = isUnion ? "union member " : "struct member ";
reportError(tok, Severity::style, "unusedStructMember", "$symbol:" + structname + "::" + varname + '\n' + prefix + "'$symbol' is never used.", CWE563, Certainty::normal);
reportError(tok, Severity::style, "unusedStructMember", "$symbol:" + structname + "::" + varname + '\n' + prefix + " member '$symbol' is never used.", CWE563, Certainty::normal);
}
bool CheckUnusedVar::isRecordTypeWithoutSideEffects(const Type* type)

View File

@ -77,7 +77,7 @@ private:
std::list<const Function*> checkedFuncs);
// Error messages..
void unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname, bool isUnion = false);
void unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname, const std::string& prefix = "struct");
void unusedVariableError(const Token *tok, const std::string &varname);
void allocatedButUnusedVariableError(const Token *tok, const std::string &varname);
void unreadVariableError(const Token *tok, const std::string &varname, bool modified);

View File

@ -75,6 +75,7 @@ private:
TEST_CASE(structmember21); // #4759
TEST_CASE(structmember22); // #11016
TEST_CASE(structmember23);
TEST_CASE(classmember);
TEST_CASE(localvar1);
TEST_CASE(localvar2);
@ -1870,6 +1871,13 @@ private:
ASSERT_EQUALS("", errout.str());
}
void classmember() {
checkStructMemberUsage("class C {\n"
" int i{};\n"
"};\n");
ASSERT_EQUALS("[test.cpp:2]: (style) class member 'C::i' is never used.\n", errout.str());
}
void functionVariableUsage_(const char* file, int line, const char code[], const char filename[] = "test.cpp") {
// Clear the error buffer..
errout.str("");