Fixed #2017 (false positive::struct or union member 'Base::m_ui' is never used)

This commit is contained in:
Daniel Marjamäki 2010-09-04 11:21:34 +02:00
parent beb4dddb2e
commit 9415423352
2 changed files with 21 additions and 5 deletions

View File

@ -2059,7 +2059,7 @@ void CheckOther::checkStructMemberUsage()
{ {
if (tok2->str() == "(") if (tok2->str() == "(")
{ {
structname = ""; structname.clear();
break; break;
} }
@ -2067,20 +2067,24 @@ void CheckOther::checkStructMemberUsage()
break; break;
} }
// bail out if struct is inherited
if (!structname.empty() && Token::findmatch(tok, (",|private|protected|public " + structname).c_str()))
structname.clear();
// Bail out if some data is casted to struct.. // Bail out if some data is casted to struct..
const std::string s("( struct| " + tok->next()->str() + " * ) & %var% ["); const std::string s("( struct| " + tok->next()->str() + " * ) & %var% [");
if (Token::findmatch(tok, s.c_str())) if (Token::findmatch(tok, s.c_str()))
structname = ""; structname.clear();
// 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 = ""; structname.clear();
else if (Token::findmatch(tok, (structname + " %type% *").c_str())) else if (Token::findmatch(tok, (structname + " %type% *").c_str()))
structname = ""; structname = "";
} }
if (tok->str() == "}") if (tok->str() == "}")
structname = ""; structname.clear();
if (!structname.empty() && Token::Match(tok, "[{;]")) if (!structname.empty() && Token::Match(tok, "[{;]"))
{ {
@ -2097,7 +2101,7 @@ void CheckOther::checkStructMemberUsage()
else else
continue; continue;
// Check if the struct variable is anywhere in the file // Check if the struct variable is used anywhere in the file
const std::string usagePattern(". " + varname); const std::string usagePattern(". " + varname);
bool used = false; bool used = false;
for (const Token *tok2 = _tokenizer->tokens(); tok2; tok2 = tok2->next()) for (const Token *tok2 = _tokenizer->tokens(); tok2; tok2 = tok2->next())

View File

@ -44,6 +44,7 @@ private:
TEST_CASE(structmember6); TEST_CASE(structmember6);
TEST_CASE(structmember7); TEST_CASE(structmember7);
TEST_CASE(structmember8); TEST_CASE(structmember8);
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(localvar1); TEST_CASE(localvar1);
@ -279,6 +280,17 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void structmember9()
{
checkStructMemberUsage("struct base {\n"
" int a;\n"
"};\n"
"\n"
"struct derived : public base {"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void structmember_extern() void structmember_extern()
{ {
// extern struct => no false positive // extern struct => no false positive