Fixed #4168 (False positive: (style) struct or union member 'super::x' is never used)
This commit is contained in:
parent
7ecfb189a2
commit
382e1e95ef
|
@ -1082,6 +1082,18 @@ void CheckUnusedVar::checkStructMemberUsage()
|
||||||
if (Token::findmatch(tok, s.c_str()))
|
if (Token::findmatch(tok, s.c_str()))
|
||||||
structname.clear();
|
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.
|
// Try to prevent false positives when struct members are not used directly.
|
||||||
if (Token::findmatch(tok, (structname + " *").c_str()))
|
if (Token::findmatch(tok, (structname + " *").c_str()))
|
||||||
structname.clear();
|
structname.clear();
|
||||||
|
|
|
@ -45,6 +45,7 @@ private:
|
||||||
TEST_CASE(structmember9); // #2017 - struct is inherited
|
TEST_CASE(structmember9); // #2017 - struct is inherited
|
||||||
TEST_CASE(structmember_extern); // No false positives for extern structs
|
TEST_CASE(structmember_extern); // No false positives for extern structs
|
||||||
TEST_CASE(structmember10);
|
TEST_CASE(structmember10);
|
||||||
|
TEST_CASE(structmember11); // #4168 - initialization with {} / passed by address to unknown function
|
||||||
|
|
||||||
TEST_CASE(localvar1);
|
TEST_CASE(localvar1);
|
||||||
TEST_CASE(localvar2);
|
TEST_CASE(localvar2);
|
||||||
|
@ -333,6 +334,18 @@ private:
|
||||||
ASSERT_EQUALS("", errout.str());
|
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() {
|
void structmember_extern() {
|
||||||
// extern struct => no false positive
|
// extern struct => no false positive
|
||||||
checkStructMemberUsage("extern struct AB\n"
|
checkStructMemberUsage("extern struct AB\n"
|
||||||
|
|
Loading…
Reference in New Issue