From 3f7093004a1173f6afdb5380cf28fe05ac50d209 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 30 Oct 2021 19:34:46 +0200 Subject: [PATCH] Fixed #10485 (FP unusedStructMember for array size) --- lib/checkunusedvar.cpp | 19 +++++++++++-------- test/testunusedvar.cpp | 9 +++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 5f77a6534..ad422db41 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1456,14 +1456,17 @@ void CheckUnusedVar::checkStructMemberUsage() continue; // Check if the struct member variable is used anywhere in the file - std::string tmp(". " + var.name()); - if (Token::findsimplematch(mTokenizer->tokens(), tmp.c_str(), tmp.size())) - continue; - tmp = (":: " + var.name()); - if (Token::findsimplematch(mTokenizer->tokens(), tmp.c_str(), tmp.size())) - continue; - - unusedStructMemberError(var.nameToken(), scope.className, var.name(), scope.type == Scope::eUnion); + bool use = false; + for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) { + if (tok->variable() != &var) + continue; + if (tok != var.nameToken()) { + use = true; + break; + } + } + if (!use) + unusedStructMemberError(var.nameToken(), scope.className, var.name(), scope.type == Scope::eUnion); } } } diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 5fb60f986..51888a3b0 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -60,6 +60,7 @@ private: TEST_CASE(structmember14); // #6508 - (struct x){1,2,..} TEST_CASE(structmember15); // #3088 - #pragma pack(1) TEST_CASE(structmember_sizeof); + TEST_CASE(structmember16); // #10485 TEST_CASE(localvar1); TEST_CASE(localvar2); @@ -1563,6 +1564,14 @@ private: ASSERT_EQUALS("", errout.str()); } + void structmember16() { + checkStructMemberUsage("struct S {\n" + " static const int N = 128;\n" // <- used + " char E[N];\n" // <- not used + "};\n"); + ASSERT_EQUALS("[test.cpp:3]: (style) struct member 'S::E' is never used.\n", errout.str()); + } + void functionVariableUsage(const char code[], const char filename[]="test.cpp") { // Clear the error buffer.. errout.str("");