Uninitialized variables: improved check to detect more errors. Ticket: #3369
This commit is contained in:
parent
c7ce87d060
commit
ba463295c2
|
@ -1118,6 +1118,9 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int
|
||||||
if (tok->previous()->str() == "return")
|
if (tok->previous()->str() == "return")
|
||||||
uninitvarError(tok, tok->str());
|
uninitvarError(tok, tok->str());
|
||||||
|
|
||||||
|
else if (Token::Match(tok->next(), "++|--|%op%"))
|
||||||
|
uninitvarError(tok, tok->str());
|
||||||
|
|
||||||
else
|
else
|
||||||
// assume that variable is assigned
|
// assume that variable is assigned
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -1700,6 +1700,20 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
void uninitvar2() {
|
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"
|
checkUninitVar2("void f() {\n"
|
||||||
" int x;\n"
|
" int x;\n"
|
||||||
" if (y == 1) { x = 1; }\n"
|
" if (y == 1) { x = 1; }\n"
|
||||||
|
|
Loading…
Reference in New Issue