Fix #11577 FN unusedStructMember when casting address (regression) (#4816)

This commit is contained in:
chrchr-github 2023-04-04 21:57:09 +02:00 committed by GitHub
parent 115f17cfe6
commit 0f47948bf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 16 deletions

View File

@ -1479,20 +1479,6 @@ void CheckUnusedVar::checkStructMemberUsage()
bailout = true; bailout = true;
break; 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) if (bailout)
break; break;
} }

View File

@ -1677,7 +1677,7 @@ private:
" uint8_t* p = (uint8_t*)&s;\n" " uint8_t* p = (uint8_t*)&s;\n"
" return p[10];\n" " return p[10];\n"
"};\n"); "};\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("[test.cpp:1]: (style) struct member 'S::padding' is never used.\n", errout.str());
checkStructMemberUsage("struct S { uint8_t padding[500]; };\n" checkStructMemberUsage("struct S { uint8_t padding[500]; };\n"
"uint8_t f(const S& s) {\n" "uint8_t f(const S& s) {\n"
@ -1685,7 +1685,14 @@ private:
" auto p = reinterpret_cast<const uint8_t*>(&s);\n" " auto p = reinterpret_cast<const uint8_t*>(&s);\n"
" return p[10];\n" " return p[10];\n"
"};\n"); "};\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("[test.cpp:1]: (style) struct member 'S::padding' is never used.\n", errout.str());
checkStructMemberUsage("struct S { int i, j; };\n" // #11577
"void f(S s) {\n"
" void* p = (void*)&s;\n"
" if (s.i) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:1]: (style) struct member 'S::j' is never used.\n", errout.str());
} }
void structmember19() { void structmember19() {