Skip to content

Commit

Permalink
Fix FP unusedStructMember
Browse files Browse the repository at this point in the history
Triggering while a function parameter has the same name as a struct member.
  • Loading branch information
mptre committed Nov 14, 2024
1 parent 648d1c8 commit 6d6254c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/checkunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1544,7 +1544,7 @@ 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%") && !tok->next()->variable() && !tok->next()->function() && tok->strAt(1) == var.name()) {
if (Token::Match(tok, ". %name%") && !tok->next()->function() && tok->strAt(1) == var.name()) {
// not known => assume variable is used
use = true;
break;
Expand Down
12 changes: 12 additions & 0 deletions test/testunusedvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class TestUnusedVar : public TestFixture {
TEST_CASE(structmember23);
TEST_CASE(structmember24); // #10847
TEST_CASE(structmember25);
TEST_CASE(structmember26);
TEST_CASE(structmember_macro);
TEST_CASE(classmember);

Expand Down Expand Up @@ -1962,6 +1963,17 @@ class TestUnusedVar : public TestFixture {
errout_str());
}

void structmember26() {
checkStructMemberUsage("struct S {\n"
" int a;\n"
"};\n"
"struct S f(int a) {\n"
" struct S s = {.a = 1};\n"
" return s;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void structmember_macro() {
checkStructMemberUsageP("#define S(n) struct n { int a, b, c; };\n"
"S(unused);\n");
Expand Down

0 comments on commit 6d6254c

Please sign in to comment.