fix false positive for unused local class/struct variable

This commit is contained in:
Robert Reif 2011-07-01 08:45:29 -04:00
parent 18e6509c5d
commit 301e59cea0
2 changed files with 19 additions and 3 deletions

View File

@ -1753,7 +1753,7 @@ void CheckOther::functionVariableUsage()
// standard type declaration with possible initialization
// int i; int j = 0; static int k;
if (Token::Match(tok, "[;{}] static| %type% %var% ;|=") &&
(nextIsStandardType(tok) || isRecordTypeWithoutSideEffects(tok->strAt(1) == "static" ? tok->tokAt(3) : tok->tokAt(2))))
!Token::Match(tok->next(), "return|throw"))
{
tok = tok->next();
@ -1761,8 +1761,11 @@ void CheckOther::functionVariableUsage()
if (isStatic)
tok = tok->next();
if (tok->isStandardType() || isRecordTypeWithoutSideEffects(tok->next()))
{
variables.addVar(tok->next(), Variables::standard, info,
tok->tokAt(2)->str() == "=" || isStatic);
}
tok = tok->next();
}

View File

@ -105,6 +105,7 @@ private:
TEST_CASE(localvarStruct3);
TEST_CASE(localvarStruct4); // Ticket #31: sigsegv on incomplete struct
TEST_CASE(localvarStruct5);
TEST_CASE(localvarStruct6);
TEST_CASE(localvarOp); // Usage with arithmetic operators
TEST_CASE(localvarInvert); // Usage with inverted variable
@ -2451,6 +2452,18 @@ private:
ASSERT_EQUALS("", errout.str());
}
void localvarStruct6()
{
functionVariableUsage("class Type { };\n"
"class A {\n"
"public:\n"
" Type & get() { return t; }\n"
"private:\n"
" Type t;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void localvarOp()
{
const char op[] = "+-*/%&|^";