Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/checkunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/checkunusedvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class CPPCHECKLIB CheckUnusedVar : public Check {
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);
Expand Down
8 changes: 8 additions & 0 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class TestUnusedVar : public TestFixture {
TEST_CASE(structmember21); // #4759
TEST_CASE(structmember22); // #11016
TEST_CASE(structmember23);
TEST_CASE(classmember);

TEST_CASE(localvar1);
TEST_CASE(localvar2);
Expand Down Expand Up @@ -1870,6 +1871,13 @@ class TestUnusedVar : public TestFixture {
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("");
Expand Down