Uninitialized variables: improved check to detect more errors. Ticket: #3369

This commit is contained in:
Daniel Marjamäki 2011-12-14 06:00:17 +01:00
parent c7ce87d060
commit ba463295c2
2 changed files with 17 additions and 0 deletions

View File

@ -1118,6 +1118,9 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int
if (tok->previous()->str() == "return")
uninitvarError(tok, tok->str());
else if (Token::Match(tok->next(), "++|--|%op%"))
uninitvarError(tok, tok->str());
else
// assume that variable is assigned
return true;

View File

@ -1700,6 +1700,20 @@ private:
}
void uninitvar2() {
// using uninit var
checkUninitVar2("void f() {\n"
" int x;\n"
" x++;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
checkUninitVar2("void f() {\n"
" int x;\n"
" int y = x & 3;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
// conditional initialization
checkUninitVar2("void f() {\n"
" int x;\n"
" if (y == 1) { x = 1; }\n"