Uninitialized variables: fixed false positive in while loop
This commit is contained in:
parent
6b01d2d5a8
commit
6997d38881
|
@ -1503,12 +1503,14 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const
|
||||||
} else {
|
} else {
|
||||||
if (isVariableUsage(tok, var.isPointer(), _tokenizer->isCPP()))
|
if (isVariableUsage(tok, var.isPointer(), _tokenizer->isCPP()))
|
||||||
usetok = tok;
|
usetok = tok;
|
||||||
else if (tok->strAt(1) == "=") {
|
else {
|
||||||
bool assign = true;
|
bool assign = true;
|
||||||
for (const Token *tok2 = tok->next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) {
|
if (tok->strAt(1) == "=") {
|
||||||
if (tok2->varId() == var.varId()) {
|
for (const Token *tok2 = tok->next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) {
|
||||||
assign = false;
|
if (tok2->varId() == var.varId()) {
|
||||||
break;
|
assign = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (assign)
|
if (assign)
|
||||||
|
|
|
@ -2842,6 +2842,15 @@ private:
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
|
||||||
|
|
||||||
|
checkUninitVar2("void f() {\n"
|
||||||
|
" int x;\n"
|
||||||
|
" while (a) {\n"
|
||||||
|
" init(&x);\n"
|
||||||
|
" x++;\n"
|
||||||
|
" }\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
checkUninitVar2("void f() {\n"
|
checkUninitVar2("void f() {\n"
|
||||||
" int x;\n"
|
" int x;\n"
|
||||||
" while (a) {\n"
|
" while (a) {\n"
|
||||||
|
|
Loading…
Reference in New Issue