Unused struct member: Fixed false positives

This commit is contained in:
Daniel Marjamäki 2010-04-17 11:16:05 +02:00
parent c0e9a546f7
commit b19113426f
2 changed files with 49 additions and 0 deletions

View File

@ -995,6 +995,12 @@ void CheckOther::checkStructMemberUsage()
if (Token::Match(tok, "struct|union %type% {"))
{
structname.clear();
if (Token::simpleMatch(tok->previous(), "extern"))
continue;
if ((!tok->previous() || Token::simpleMatch(tok->previous(), ";")) && Token::Match(tok->tokAt(2)->link(), ("} ; " + tok->strAt(1) + " %var% ;").c_str()))
continue;
structname = tok->strAt(1);
// Bail out if struct/union contain any functions

View File

@ -60,6 +60,7 @@ private:
TEST_CASE(structmember6);
TEST_CASE(structmember7);
TEST_CASE(structmember8);
TEST_CASE(structmember_extern); // No false positives for extern structs
TEST_CASE(localvar1);
TEST_CASE(localvar2);
@ -247,6 +248,48 @@ private:
ASSERT_EQUALS("", errout.str());
}
void structmember_extern()
{
// extern struct => no false positive
check("extern struct AB\n"
"{\n"
" int a;\n"
" int b;\n"
"} ab;\n"
"\n"
"void foo()\n"
"{\n"
" ab.b = 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// global linkage => no false positive
check("struct AB\n"
"{\n"
" int a;\n"
" int b;\n"
"} ab;\n"
"\n"
"void foo()\n"
"{\n"
" ab.b = 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// static linkage => error message
check("static struct AB\n"
"{\n"
" int a;\n"
" int b;\n"
"} ab;\n"
"\n"
"void foo()\n"
"{\n"
" ab.b = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) struct or union member 'AB::a' is never used\n", errout.str());
}