Fixed #874 (false positive: uninitialized variable)
This commit is contained in:
parent
768225bb1b
commit
44a9c9b458
|
@ -1258,6 +1258,31 @@ static const Token *uninitvar_checkscope(const Token *tok, const unsigned int va
|
|||
return 0;
|
||||
}
|
||||
|
||||
if (Token::Match(tok, "%var% ("))
|
||||
{
|
||||
// is the variable passed as a parameter to some function?
|
||||
unsigned int parlevel = 0;
|
||||
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
|
||||
{
|
||||
if (tok2->str() == "{")
|
||||
++parlevel;
|
||||
|
||||
else if (tok2->str() == "}")
|
||||
{
|
||||
if (parlevel <= 1)
|
||||
break;
|
||||
--parlevel;
|
||||
}
|
||||
|
||||
else if (tok2->varId() == varid)
|
||||
{
|
||||
// it is possible that the variable is initialized here
|
||||
init = true;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Token::Match(tok, "return| %varid% .|", varid))
|
||||
return tok;
|
||||
}
|
||||
|
|
|
@ -933,6 +933,7 @@ private:
|
|||
|
||||
void uninitvar1()
|
||||
{
|
||||
// if..
|
||||
checkUninitVar("static void foo()\n"
|
||||
"{\n"
|
||||
" Foo *p;\n"
|
||||
|
@ -963,12 +964,26 @@ private:
|
|||
"}\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
// member variables..
|
||||
checkUninitVar("class Fred\n"
|
||||
"{\n"
|
||||
" int i;\n"
|
||||
" int a() { return i; }\n"
|
||||
"};\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
// function calls..
|
||||
checkUninitVar("void assignOne(int &x)\n"
|
||||
"{ x = 1; }\n"
|
||||
"\n"
|
||||
"int f()\n"
|
||||
"{\n"
|
||||
" int i;\n"
|
||||
" assignOne(i);\n"
|
||||
" return i;\n"
|
||||
"};\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue