Partial fix for #10848 FP: unusedStructMember (#3880)

This commit is contained in:
chrchr-github 2022-03-09 20:25:58 +01:00 committed by GitHub
parent 557263acde
commit 2616046461
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 4 deletions

View File

@ -1259,8 +1259,11 @@ void SymbolDatabase::createSymbolDatabaseSetVariablePointers()
fixVarId(varIds, tok, const_cast<Token *>(membertok), membervar); fixVarId(varIds, tok, const_cast<Token *>(membertok), membervar);
} }
} else if (tok->valueType() && tok->valueType()->type == ValueType::CONTAINER) { } else if (tok->valueType() && tok->valueType()->type == ValueType::CONTAINER) {
if (Token::Match(var->typeStartToken(), "std :: %type% < %type% *| *| >")) { if (Token::Match(var->typeStartToken(), "std :: %type% < %name%")) {
const Type * type2 = var->typeStartToken()->tokAt(4)->type(); const Token* type2tok = var->typeStartToken()->tokAt(4);
while (type2tok && type2tok->isKeyword())
type2tok = type2tok->next();
const Type* type2 = type2tok ? type2tok->type() : nullptr;
if (type2 && type2->classScope && type2->classScope->definedType) { if (type2 && type2->classScope && type2->classScope->definedType) {
const Variable *membervar = type2->classScope->getVariable(membertok->str()); const Variable *membervar = type2->classScope->getVariable(membertok->str());
if (membervar) { if (membervar) {

View File

@ -66,7 +66,7 @@ private:
TEST_CASE(structmember16); // #10485 TEST_CASE(structmember16); // #10485
TEST_CASE(structmember17); // #10591 TEST_CASE(structmember17); // #10591
TEST_CASE(structmember18); // #10684 TEST_CASE(structmember18); // #10684
TEST_CASE(structmember19); // #10826 TEST_CASE(structmember19); // #10826, #10848
TEST_CASE(localvar1); TEST_CASE(localvar1);
TEST_CASE(localvar2); TEST_CASE(localvar2);
@ -1654,7 +1654,7 @@ private:
"[test.cpp:5]: (style) struct member 'S::c' is never used.\n", "[test.cpp:5]: (style) struct member 'S::c' is never used.\n",
errout.str()); errout.str());
checkStructMemberUsage("struct S {\n" checkStructMemberUsage("struct S {\n" // #10848
" struct T {\n" " struct T {\n"
" int i;\n" " int i;\n"
" } t[2];\n" " } t[2];\n"
@ -1701,6 +1701,15 @@ private:
" return sp->a;\n" " return sp->a;\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkStructMemberUsage("typedef struct { int i; } A;\n"
"typedef struct { std::vector<A> v; } B;\n"
"const A& f(const std::vector<const B*>& b, int idx) {\n"
" const A& a = b[0]->v[idx];\n"
" return a;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:1]: (style) struct member 'A::i' is never used.\n",
errout.str());
} }
void functionVariableUsage_(const char* file, int line, const char code[], const char filename[] = "test.cpp") { void functionVariableUsage_(const char* file, int line, const char code[], const char filename[] = "test.cpp") {