parent
ad17a0d721
commit
bebf8ccdd5
|
@ -1248,34 +1248,12 @@ void CheckUnusedVar::checkStructMemberUsage()
|
|||
continue;
|
||||
|
||||
// Check if the struct member variable is used anywhere in the file
|
||||
const std::string usagePattern(". " + *memberVarName);
|
||||
bool used = false;
|
||||
const Token* usageTok = _tokenizer->tokens();
|
||||
while ((usageTok = Token::findsimplematch(usageTok->next(), usagePattern.c_str())) != nullptr) {
|
||||
// Locate the containing struct variable and ensure it's of the same type, not a random struct
|
||||
const Token* structVarTok = usageTok->previous();
|
||||
// Walk backwards over array accesses
|
||||
while (structVarTok && structVarTok->link())
|
||||
structVarTok = structVarTok->link()->previous();
|
||||
if (!structVarTok)
|
||||
if (Token::findsimplematch(_tokenizer->tokens(), (". " + *memberVarName).c_str()))
|
||||
continue;
|
||||
const Variable* structVar = structVarTok->variable();
|
||||
if (structVar && structVar->type() && structVar->type()->name() == structname) {
|
||||
used = true;
|
||||
break;
|
||||
}
|
||||
const Function* function = structVarTok->function();
|
||||
if (function && function->retType && function->retType->name() == structname) {
|
||||
used = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!used) {
|
||||
unusedStructMemberError(tok->next(), structname, *memberVarName, tok->scope()->type == Scope::eUnion);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CheckUnusedVar::unusedStructMemberError(const Token *tok, const std::string &structname, const std::string &varname, bool isUnion)
|
||||
|
|
|
@ -223,50 +223,6 @@ private:
|
|||
ASSERT_EQUALS("[test.cpp:3]: (style) union member 'abc::a' is never used.\n"
|
||||
"[test.cpp:4]: (style) union member 'abc::b' is never used.\n"
|
||||
"[test.cpp:5]: (style) union member 'abc::c' is never used.\n", errout.str());
|
||||
|
||||
checkStructMemberUsage("struct A\n"
|
||||
"{\n"
|
||||
" int a;\n"
|
||||
"};\n"
|
||||
"struct B\n"
|
||||
"{\n"
|
||||
" int a;\n"
|
||||
"};\n"
|
||||
"void foo()\n"
|
||||
"{\n"
|
||||
" A a;\n"
|
||||
" a.a;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:7]: (style) struct member 'B::a' is never used.\n", errout.str());
|
||||
|
||||
checkStructMemberUsage("struct A\n"
|
||||
"{\n"
|
||||
" int a;\n"
|
||||
"};\n"
|
||||
"struct B\n"
|
||||
"{\n"
|
||||
" int a;\n"
|
||||
"};\n"
|
||||
"void foo(A* a)\n"
|
||||
"{\n"
|
||||
" a->a;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:7]: (style) struct member 'B::a' is never used.\n", errout.str());
|
||||
|
||||
checkStructMemberUsage("struct A\n"
|
||||
"{\n"
|
||||
" int a;\n"
|
||||
"};\n"
|
||||
"struct B\n"
|
||||
"{\n"
|
||||
" int a;\n"
|
||||
"};\n"
|
||||
"A& bar();\n"
|
||||
"void foo()\n"
|
||||
"{\n"
|
||||
" bar().a;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:7]: (style) struct member 'B::a' is never used.\n", errout.str());
|
||||
}
|
||||
|
||||
void structmember2() {
|
||||
|
|
Loading…
Reference in New Issue