Uninitialized variables: fixed false positive in while loop
This commit is contained in:
parent
6b01d2d5a8
commit
6997d38881
|
@ -1503,14 +1503,16 @@ 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;
|
||||||
|
if (tok->strAt(1) == "=") {
|
||||||
for (const Token *tok2 = tok->next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) {
|
for (const Token *tok2 = tok->next(); tok2 && tok2->str() != ";"; tok2 = tok2->next()) {
|
||||||
if (tok2->varId() == var.varId()) {
|
if (tok2->varId() == var.varId()) {
|
||||||
assign = false;
|
assign = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (assign)
|
if (assign)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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