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

@ -472,6 +472,19 @@ 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->str() == "=")
{
assignment = true;
break;
}
}
if (!assignment)
{
for (const Token *tok2 = tok.next(); tok2 && tok2->str() != ";"; tok2 = tok2->next())
{
@ -482,6 +495,7 @@ private:
use(checks, tok2);
}
}
}
if (tok.varId())
{

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"