Uninitialized variables: Fixed false positive when there is assignment in condition

This commit is contained in:
Daniel Marjamäki 2012-07-17 07:03:40 +02:00
parent ddfc968028
commit d34924ba6d
2 changed files with 17 additions and 1 deletions

View File

@ -33,6 +33,16 @@ namespace {
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Skip [ .. ]
static const Token * skipBrackets(const Token *tok)
{
while (tok && tok->str() == "[")
tok = tok->link()->next();
return tok;
}
/// @addtogroup Checks /// @addtogroup Checks
/// @{ /// @{
@ -860,7 +870,7 @@ private:
if (tok.varId() && Token::Match(&tok, "%var% <|<=|==|!=|)")) if (tok.varId() && Token::Match(&tok, "%var% <|<=|==|!=|)"))
use(checks, &tok); use(checks, &tok);
else if (Token::Match(&tok, "!| %var% [")) else if (Token::Match(&tok, "!| %var% [") && !Token::simpleMatch(skipBrackets(tok.next()), "="))
use_array_or_pointer_data(checks, tok.str() == "!" ? tok.next() : &tok); use_array_or_pointer_data(checks, tok.str() == "!" ? tok.next() : &tok);
else if (Token::Match(&tok, "!| %var% (")) { else if (Token::Match(&tok, "!| %var% (")) {

View File

@ -688,6 +688,12 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void f() {\n"
" char a[10];\n"
" if (a[0] = x){}\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar("int foo(int x)\n" checkUninitVar("int foo(int x)\n"
"{\n" "{\n"
" int i;\n" " int i;\n"