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,7 +662,13 @@ 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
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();
while (Token::Match(tok, "const|*|&"))
tok = tok->next();
@ -674,6 +680,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
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% )") ||
Token::Match(tok, "delete %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"