Skip to content

Commit c3002f1

Browse files
Fix #551 Detect unused Private member variables (#4973)
1 parent 029056b commit c3002f1

3 files changed

Lines changed: 20 additions & 7 deletions

File tree

lib/checkunusedvar.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ void CheckUnusedVar::checkStructMemberUsage()
14371437
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
14381438

14391439
for (const Scope &scope : symbolDatabase->scopeList) {
1440-
if (scope.type != Scope::eStruct && scope.type != Scope::eUnion)
1440+
if (scope.type != Scope::eStruct && scope.type != Scope::eClass && scope.type != Scope::eUnion)
14411441
continue;
14421442

14431443
if (scope.bodyStart->fileIndex() != 0 || scope.className.empty())
@@ -1521,16 +1521,21 @@ void CheckUnusedVar::checkStructMemberUsage()
15211521
break;
15221522
}
15231523
}
1524-
if (!use)
1525-
unusedStructMemberError(var.nameToken(), scope.className, var.name(), scope.type == Scope::eUnion);
1524+
if (!use) {
1525+
std::string prefix = "struct";
1526+
if (scope.type == Scope::ScopeType::eClass)
1527+
prefix = "class";
1528+
else if (scope.type == Scope::ScopeType::eUnion)
1529+
prefix = "union";
1530+
unusedStructMemberError(var.nameToken(), scope.className, var.name(), prefix);
1531+
}
15261532
}
15271533
}
15281534
}
15291535

1530-
void CheckUnusedVar::unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname, bool isUnion)
1536+
void CheckUnusedVar::unusedStructMemberError(const Token* tok, const std::string& structname, const std::string& varname, const std::string& prefix)
15311537
{
1532-
const std::string prefix = isUnion ? "union member " : "struct member ";
1533-
reportError(tok, Severity::style, "unusedStructMember", "$symbol:" + structname + "::" + varname + '\n' + prefix + "'$symbol' is never used.", CWE563, Certainty::normal);
1538+
reportError(tok, Severity::style, "unusedStructMember", "$symbol:" + structname + "::" + varname + '\n' + prefix + " member '$symbol' is never used.", CWE563, Certainty::normal);
15341539
}
15351540

15361541
bool CheckUnusedVar::isRecordTypeWithoutSideEffects(const Type* type)

lib/checkunusedvar.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class CPPCHECKLIB CheckUnusedVar : public Check {
7777
std::list<const Function*> checkedFuncs);
7878

7979
// Error messages..
80-
void unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname, bool isUnion = false);
80+
void unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname, const std::string& prefix = "struct");
8181
void unusedVariableError(const Token *tok, const std::string &varname);
8282
void allocatedButUnusedVariableError(const Token *tok, const std::string &varname);
8383
void unreadVariableError(const Token *tok, const std::string &varname, bool modified);

test/testunusedvar.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class TestUnusedVar : public TestFixture {
7575
TEST_CASE(structmember21); // #4759
7676
TEST_CASE(structmember22); // #11016
7777
TEST_CASE(structmember23);
78+
TEST_CASE(classmember);
7879

7980
TEST_CASE(localvar1);
8081
TEST_CASE(localvar2);
@@ -1870,6 +1871,13 @@ class TestUnusedVar : public TestFixture {
18701871
ASSERT_EQUALS("", errout.str());
18711872
}
18721873

1874+
void classmember() {
1875+
checkStructMemberUsage("class C {\n"
1876+
" int i{};\n"
1877+
"};\n");
1878+
ASSERT_EQUALS("[test.cpp:2]: (style) class member 'C::i' is never used.\n", errout.str());
1879+
}
1880+
18731881
void functionVariableUsage_(const char* file, int line, const char code[], const char filename[] = "test.cpp") {
18741882
// Clear the error buffer..
18751883
errout.str("");

0 commit comments

Comments
 (0)