Uninitialized variables: Fixed false positive if there is assignment in return statement

This commit is contained in:
Daniel Marjamäki 2011-07-24 22:41:40 +02:00
parent 1bb7a1c23c
commit 9a3f95613a
2 changed files with 24 additions and 4 deletions

View File

@ -473,13 +473,27 @@ private:
if (tok.str() == "return") if (tok.str() == "return")
{ {
// is there assignment in the return statement?
bool assignment = false;
for (const Token *tok2 = tok.next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) for (const Token *tok2 = tok.next(); tok2 && tok2->str() != ";"; tok2 = tok2->next())
{ {
if (tok2->isName() && tok2->strAt(1) == "(") if (tok2->str() == "=")
tok2 = tok2->next()->link(); {
assignment = true;
break;
}
}
else if (tok2->varId()) if (!assignment)
use(checks, tok2); {
for (const Token *tok2 = tok.next(); tok2 && tok2->str() != ";"; tok2 = tok2->next())
{
if (tok2->isName() && tok2->strAt(1) == "(")
tok2 = tok2->next()->link();
else if (tok2->varId())
use(checks, tok2);
}
} }
} }

View File

@ -175,6 +175,12 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: ret\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: ret\n", errout.str());
checkUninitVar("static int foo() {\n"
" int ret;\n"
" return ret = 5;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void f()\n" checkUninitVar("void f()\n"
"{\n" "{\n"
" int a;\n" " int a;\n"