Fixed #2770 (False positives (scope can be reduced / variable is assigned value that is never used))

This commit is contained in:
Daniel Marjamäki 2011-05-06 21:16:01 +02:00
parent b9ee867344
commit 3629f953f0
3 changed files with 30 additions and 9 deletions

View File

@ -2230,6 +2230,15 @@ void CheckOther::functionVariableUsage()
else
variables.modified(tok->varId());
}
else if (tok->isAssignmentOp())
{
for (const Token *tok2 = tok->next(); tok2 && tok2->str() != ";"; tok2 = tok2->next())
{
if (tok2->varId())
variables.read(tok2->varId());
}
}
}
// Check usage of all variables in the current scope..
@ -2473,19 +2482,13 @@ void CheckOther::lookupVar(const Token *tok1, const std::string &varname)
for_or_while = true;
}
if (Token::simpleMatch(tok, "do {"))
else if (Token::simpleMatch(tok, "do {"))
for_or_while = true;
// possible unexpanded macro hiding for/while..
if (Token::Match(tok->previous(), "[;{}] %type% {"))
else if (tok->str() != "else" && Token::Match(tok->previous(), "[;{}] %type% {"))
{
bool upper = true;
for (unsigned int i = 0; i < tok->str().length(); ++i)
{
if (!std::isupper(tok->str()[i]))
upper = false;
}
for_or_while |= upper;
for_or_while = true;
}
if (parlevel == 0 && (tok->str() == ";"))

View File

@ -60,6 +60,7 @@ private:
TEST_CASE(varScope9); // classes may have extra side-effects
TEST_CASE(varScope10); // Undefined macro FOR
TEST_CASE(varScope11); // #2475 - struct initialization is not inner scope
TEST_CASE(varScope12); // variable usage in inner loop
TEST_CASE(oldStylePointerCast);
@ -686,6 +687,17 @@ private:
ASSERT_EQUALS("", errout.str());
}
void varScope12()
{
// #2770
varScope("void f() {\n"
" int i = 0;\n"
" forever {\n"
" if (i++ == 42) { break; }\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void checkOldStylePointerCast(const char code[])
{

View File

@ -2394,6 +2394,12 @@ private:
" a |= b;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Variable 'a' is assigned a value that is never used\n", errout.str());
functionVariableUsage("void foo() {\n"
" int a = 1;\n"
" (b).x += a;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void localvarFor()