Fix #10684 FP unusedStructMember with static object (#3681)

This commit is contained in:
chrchr-github 2022-02-02 19:28:16 +01:00 committed by GitHub
parent 9d36dd56a8
commit 8cf5c8fbf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -1426,6 +1426,19 @@ void CheckUnusedVar::checkStructMemberUsage()
bailout = true;
break;
}
if (var && (var->typeStartToken()->str() == scope.className || var->typeEndToken()->str() == scope.className)) {
const std::string addressPattern("!!" + scope.className + " & " + var->name()); // cast from struct
const Token* addrTok = scope.bodyEnd;
do {
addrTok = Token::findmatch(addrTok, addressPattern.c_str());
if ((addrTok && addrTok->str() == ")" && addrTok->link()->isCast()) || isCPPCast(addrTok)) {
bailout = true;
break;
}
if (addrTok)
addrTok = addrTok->next();
} while (addrTok);
}
}
if (bailout)
continue;

View File

@ -66,6 +66,7 @@ private:
TEST_CASE(structmember_sizeof);
TEST_CASE(structmember16); // #10485
TEST_CASE(structmember17); // #10591
TEST_CASE(structmember18); // #10684
TEST_CASE(localvar1);
TEST_CASE(localvar2);
@ -1607,6 +1608,24 @@ private:
TODO_ASSERT_EQUALS("", "[test.cpp:1]: (style) struct member 'T::i' is never used.\n", errout.str()); // due to removeMacroInClassDef()
}
void structmember18() { // #10684
checkStructMemberUsage("struct S { uint8_t padding[500]; };\n"
"static S s = { 0 };\n"
"uint8_t f() {\n"
" uint8_t* p = (uint8_t*)&s;\n"
" return p[10];\n"
"};\n");
ASSERT_EQUALS("", errout.str());
checkStructMemberUsage("struct S { uint8_t padding[500]; };\n"
"uint8_t f(const S& s) {\n"
" std::cout << &s;\n"
" auto p = reinterpret_cast<const uint8_t*>(&s);\n"
" return p[10];\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void functionVariableUsage_(const char* file, int line, const char code[], const char filename[] = "test.cpp") {
// Clear the error buffer..
errout.str("");