From 6d6254cf292d642b5c602069d702b035cf511d96 Mon Sep 17 00:00:00 2001 From: Anton Lindqvist Date: Thu, 14 Nov 2024 08:42:23 +0100 Subject: [PATCH] Fix FP unusedStructMember Triggering while a function parameter has the same name as a struct member. --- lib/checkunusedvar.cpp | 2 +- test/testunusedvar.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 755b77517c7..80b82aca15f 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -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; diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 3d57b789c02..e2b038c130e 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -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); @@ -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");