Fixed #3603 (False Positive: Variable is assigned a value that is never used)

This commit is contained in:
Daniel Marjamäki 2012-02-19 15:25:46 +01:00
parent b1ff900aaa
commit 69d03bac34
2 changed files with 28 additions and 10 deletions

View File

@ -662,17 +662,24 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
break;
}
if (Token::Match(tok, "%type% const| *|&| const| *| const| %var% [;=[(]") && tok->str() != "return" && tok->str() != "throw") { // Declaration: Skip
tok = tok->next();
while (Token::Match(tok, "const|*|&"))
if (Token::Match(tok, "%type% const| *|&| const| *| const| %var% [;=[(]")) { // Declaration: Skip
// Before a declaration there should be ;{}
const Token *prev = tok;
while (prev && prev->isName() && prev->str() != "return" && prev->str() != "throw")
prev = prev->previous();
if (Token::Match(prev, "[;{}]")) {
tok = tok->next();
tok = Token::findmatch(tok, "[;=[(]");
if (tok && Token::Match(tok, "( %var% )")) // Simple initialization through copy ctor
tok = tok->next();
else if (tok && Token::Match(tok, "= %var% ;")) // Simple initialization
tok = tok->next();
if (!tok)
break;
while (Token::Match(tok, "const|*|&"))
tok = tok->next();
tok = Token::findmatch(tok, "[;=[(]");
if (tok && Token::Match(tok, "( %var% )")) // Simple initialization through copy ctor
tok = tok->next();
else if (tok && Token::Match(tok, "= %var% ;")) // Simple initialization
tok = tok->next();
if (!tok)
break;
}
}
// Freeing memory (not considered "using" the pointer if it was also allocated in this function)
if (Token::Match(tok, "free|g_free|kfree|vfree ( %var% )") ||

View File

@ -87,6 +87,7 @@ private:
TEST_CASE(localvar39); // ticket #3454
TEST_CASE(localvar40); // ticket #3473
TEST_CASE(localvar41); // ticket #3481
TEST_CASE(localvar42); // ticket #3603
TEST_CASE(localvaralias1);
TEST_CASE(localvaralias2); // ticket #1637
TEST_CASE(localvaralias3); // ticket #1639
@ -1401,6 +1402,16 @@ private:
ASSERT_EQUALS("", errout.str());
}
void localvar42() {
// #3603 - false positive 'x is assigned a value that is never used'
functionVariableUsage("int f() {\n"
" int x = 1;\n"
" int y = FOO::VALUE * x;\n"
" return y;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void localvaralias1() {
functionVariableUsage("void foo()\n"
"{\n"