Uninitialized variables: Fixed false positive in new checking when variable is initialized in condition

This commit is contained in:
Daniel Marjamäki 2011-12-14 17:17:24 +01:00
parent 6a4b1127aa
commit 419ae2a135
2 changed files with 17 additions and 0 deletions

View File

@ -1074,6 +1074,15 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int
// Inner scope..
if (Token::Match(tok, "if (")) {
// initialization in condition..
const Token * const endToken = tok->next()->link();
for (const Token *tok2 = tok->tokAt(2); tok2 != endToken; tok2 = tok2->next()) {
if (tok2->varId() == varid) {
// TODO: better checks if this is initialization or usage
return true;
}
}
// goto the {
tok = tok->next()->link()->next();

View File

@ -1722,6 +1722,14 @@ private:
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: x\n", errout.str());
// initialization in condition
checkUninitVar2("void f() {\n"
" int a;\n"
" if (init(&a)) { }\n"
" a++;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// return, break, continue
checkUninitVar2("void f() {\n"
" int x;\n"