Uninitialized variables: Fixed false positive if there is assignment in return statement
This commit is contained in:
parent
1bb7a1c23c
commit
9a3f95613a
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in New Issue