Fixed #10485 (FP unusedStructMember for array size)

This commit is contained in:
Daniel Marjamäki 2021-10-30 19:34:46 +02:00
parent f363d8948b
commit 3f7093004a
2 changed files with 20 additions and 8 deletions

View File

@ -1456,14 +1456,17 @@ void CheckUnusedVar::checkStructMemberUsage()
continue; continue;
// Check if the struct member variable is used anywhere in the file // Check if the struct member variable is used anywhere in the file
std::string tmp(". " + var.name()); bool use = false;
if (Token::findsimplematch(mTokenizer->tokens(), tmp.c_str(), tmp.size())) for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {
continue; if (tok->variable() != &var)
tmp = (":: " + var.name()); continue;
if (Token::findsimplematch(mTokenizer->tokens(), tmp.c_str(), tmp.size())) if (tok != var.nameToken()) {
continue; use = true;
break;
unusedStructMemberError(var.nameToken(), scope.className, var.name(), scope.type == Scope::eUnion); }
}
if (!use)
unusedStructMemberError(var.nameToken(), scope.className, var.name(), scope.type == Scope::eUnion);
} }
} }
} }

View File

@ -60,6 +60,7 @@ private:
TEST_CASE(structmember14); // #6508 - (struct x){1,2,..} TEST_CASE(structmember14); // #6508 - (struct x){1,2,..}
TEST_CASE(structmember15); // #3088 - #pragma pack(1) TEST_CASE(structmember15); // #3088 - #pragma pack(1)
TEST_CASE(structmember_sizeof); TEST_CASE(structmember_sizeof);
TEST_CASE(structmember16); // #10485
TEST_CASE(localvar1); TEST_CASE(localvar1);
TEST_CASE(localvar2); TEST_CASE(localvar2);
@ -1563,6 +1564,14 @@ private:
ASSERT_EQUALS("", errout.str()); 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") { void functionVariableUsage(const char code[], const char filename[]="test.cpp") {
// Clear the error buffer.. // Clear the error buffer..
errout.str(""); errout.str("");