Skip to content

Commit

Permalink
naming + simpler tests + remove redundant checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ludviggunne committed Sep 28, 2024
1 parent 5ba0d44 commit b30fb27
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 36 deletions.
12 changes: 6 additions & 6 deletions lib/checkunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1535,11 +1535,11 @@ void CheckUnusedVar::checkStructMemberUsage()
continue;

// Keep track of number of non-static members encountered so far
int nummembs = 0;
int memberCount = 0;

for (const Variable &var : scope.varlist) {
if (!var.isStatic()) {
nummembs++;
memberCount++;
}

// only warn for variables without side effects
Expand All @@ -1551,18 +1551,18 @@ void CheckUnusedVar::checkStructMemberUsage()
// Check if the struct member variable is used anywhere in the file
bool use = false;
for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, "%name% {") && !Token::Match(tok->previous(), "class|struct") && tok->str() == scope.className && tok->linkAt(1)) {
if (Token::Match(tok, "%name% {") && !Token::Match(tok->previous(), "class|struct") && tok->str() == scope.className) {
const Token *inner = tok->next()->astOperand2();
// Find number of initialized members in braced initalizer
int initmembs = 0;
int initializedMemberCount = 0;
while (inner) {
initmembs++;
initializedMemberCount++;
if (!inner->isInitComma()) {
break;
}
inner = inner->astOperand1();
}
if (initmembs >= nummembs) {
if (initializedMemberCount >= memberCount) {
use = true;
break;
}
Expand Down
49 changes: 19 additions & 30 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1967,49 +1967,38 @@ class TestUnusedVar : public TestFixture {

void structmember26() { // #10305
checkStructMemberUsage("struct S {\n"
" const int* p{};\n"
" int p;\n"
"};\n"
"struct C {\n"
" int i{};\n"
" S f() const {\n"
" return S{ &i };\n"
" }\n"
"};\n");
"S f() const {\n"
" return S{ 0 };\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void structmember27() {
checkStructMemberUsage("struct S {\n"
" const int* p{};\n"
" static int* q;\n"
" const int* r{};\n"
"};\n"
"struct C {\n"
" int i{};\n"
" int j{};\n"
" S f() const {\n"
" return S{ &i, &j };\n"
" }\n"
" int p;\n"
" static int q;\n"
" int r;\n"
"};\n"
"int *S::q = 0;\n");
"S f() const {\n"
" return S{ 0, 0 };\n"
"}\n"
"int S::q = 0;\n");
ASSERT_EQUALS("", errout_str());
}

void structmember28() {
checkStructMemberUsage("struct S {\n"
" const int* p{};\n"
" static int* q;\n"
" const int* r{};\n"
" const int* s{};"
"};\n"
"struct C {\n"
" int i{};\n"
" int j{};\n"
" S f() const {\n"
" return S{ &i, &j };\n"
" }\n"
" int p;\n"
" static int q;\n"
" int r;\n"
" int s;"
"};\n"
"int *S::q = 0;\n");
"S f() const {\n"
" return S{ 0, 0 };\n"
"}\n"
"int S::q = 0;\n");
ASSERT_EQUALS("[test.cpp:5]: (style) struct member 'S::s' is never used.\n", errout_str());
}

Expand Down

0 comments on commit b30fb27

Please sign in to comment.