Fixed #4168 (False positive: (style) struct or union member 'super::x' is never used)

This commit is contained in:
Daniel Marjamäki 2012-11-18 15:24:47 +01:00
parent 7ecfb189a2
commit 382e1e95ef
2 changed files with 25 additions and 0 deletions

View File

@ -1082,6 +1082,18 @@ void CheckUnusedVar::checkStructMemberUsage()
if (Token::findmatch(tok, s.c_str()))
structname.clear();
// Bail out if instance is initialized with {}..
if (!structname.empty()) {
const std::string pattern1(structname + " %var% ;");
const Token *tok2 = tok;
while (NULL != (tok2 = Token::findmatch(tok2->next(), pattern1.c_str()))) {
if (Token::simpleMatch(tok2->tokAt(3), (tok2->strAt(1) + " = {").c_str())) {
structname.clear();
break;
}
}
}
// Try to prevent false positives when struct members are not used directly.
if (Token::findmatch(tok, (structname + " *").c_str()))
structname.clear();

View File

@ -45,6 +45,7 @@ private:
TEST_CASE(structmember9); // #2017 - struct is inherited
TEST_CASE(structmember_extern); // No false positives for extern structs
TEST_CASE(structmember10);
TEST_CASE(structmember11); // #4168 - initialization with {} / passed by address to unknown function
TEST_CASE(localvar1);
TEST_CASE(localvar2);
@ -333,6 +334,18 @@ private:
ASSERT_EQUALS("", errout.str());
}
void structmember11() { // #4168
checkStructMemberUsage("struct abc { int x; };\n"
"struct abc s = {0};\n"
"void f() { do_something(&s); }\n");
ASSERT_EQUALS("", errout.str());
checkStructMemberUsage("struct abc { int x; };\n"
"struct abc s = {0};\n"
"void f() { }\n");
TODO_ASSERT_EQUALS("abc::x is not used", "", errout.str());
}
void structmember_extern() {
// extern struct => no false positive
checkStructMemberUsage("extern struct AB\n"