Fixed #2017 (false positive::struct or union member 'Base::m_ui' is never used)
This commit is contained in:
parent
beb4dddb2e
commit
9415423352
|
@ -2059,7 +2059,7 @@ void CheckOther::checkStructMemberUsage()
|
|||
{
|
||||
if (tok2->str() == "(")
|
||||
{
|
||||
structname = "";
|
||||
structname.clear();
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -2067,20 +2067,24 @@ void CheckOther::checkStructMemberUsage()
|
|||
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..
|
||||
const std::string s("( struct| " + tok->next()->str() + " * ) & %var% [");
|
||||
if (Token::findmatch(tok, s.c_str()))
|
||||
structname = "";
|
||||
structname.clear();
|
||||
|
||||
// Try to prevent false positives when struct members are not used directly.
|
||||
if (Token::findmatch(tok, (structname + " *").c_str()))
|
||||
structname = "";
|
||||
structname.clear();
|
||||
else if (Token::findmatch(tok, (structname + " %type% *").c_str()))
|
||||
structname = "";
|
||||
}
|
||||
|
||||
if (tok->str() == "}")
|
||||
structname = "";
|
||||
structname.clear();
|
||||
|
||||
if (!structname.empty() && Token::Match(tok, "[{;]"))
|
||||
{
|
||||
|
@ -2097,7 +2101,7 @@ void CheckOther::checkStructMemberUsage()
|
|||
else
|
||||
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);
|
||||
bool used = false;
|
||||
for (const Token *tok2 = _tokenizer->tokens(); tok2; tok2 = tok2->next())
|
||||
|
|
|
@ -44,6 +44,7 @@ private:
|
|||
TEST_CASE(structmember6);
|
||||
TEST_CASE(structmember7);
|
||||
TEST_CASE(structmember8);
|
||||
TEST_CASE(structmember9); // #2017 - struct is inherited
|
||||
TEST_CASE(structmember_extern); // No false positives for extern structs
|
||||
|
||||
TEST_CASE(localvar1);
|
||||
|
@ -279,6 +280,17 @@ private:
|
|||
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()
|
||||
{
|
||||
// extern struct => no false positive
|
||||
|
|
Loading…
Reference in New Issue