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")
{
// is there assignment in the return statement?
bool assignment = false;
for (const Token *tok2 = tok.next(); tok2 && tok2->str() != ";"; tok2 = tok2->next())
{
if (tok2->isName() && tok2->strAt(1) == "(")
tok2 = tok2->next()->link();
if (tok2->str() == "=")
{
assignment = true;
break;
}
}
else if (tok2->varId())
use(checks, tok2);
if (!assignment)
{
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");
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"
"{\n"
" int a;\n"