Fixed #5259 (Improve check: Uninitialized variable not reported when used in array initialization)
This commit is contained in:
parent
5c488b9519
commit
9d26be8380
|
@ -543,6 +543,25 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Token *errorToken = nullptr;
|
||||||
|
visitAstNodes(tok->next(),
|
||||||
|
[&](const Token *child) {
|
||||||
|
if (child->isUnaryOp("&"))
|
||||||
|
return ChildrenToVisit::none;
|
||||||
|
if (child->str() == "," || child->str() == "{" || child->isConstOp())
|
||||||
|
return ChildrenToVisit::op1_and_op2;
|
||||||
|
if (child->str() == "." && Token::Match(child->astOperand1(), "%varid", var.declarationId()) && child->astOperand2() && child->astOperand2()->str() == membervar) {
|
||||||
|
errorToken = child;
|
||||||
|
return ChildrenToVisit::done;
|
||||||
|
}
|
||||||
|
return ChildrenToVisit::none;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (errorToken) {
|
||||||
|
uninitStructMemberError(errorToken->astOperand2(), errorToken->astOperand1()->str() + "." + membervar);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// Skip block
|
// Skip block
|
||||||
tok = end;
|
tok = end;
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -3074,6 +3074,13 @@ private:
|
||||||
"}\n", "test.c");
|
"}\n", "test.c");
|
||||||
ASSERT_EQUALS("[test.c:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
|
ASSERT_EQUALS("[test.c:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
|
||||||
|
|
||||||
|
checkUninitVar("struct AB { int a; int b; };\n"
|
||||||
|
"void f(void) {\n"
|
||||||
|
" AB ab1;\n"
|
||||||
|
" AB ab2 = { ab1.a, 0 };\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab1.a\n", errout.str());
|
||||||
|
|
||||||
checkUninitVar("struct AB { int a; int b; };\n"
|
checkUninitVar("struct AB { int a; int b; };\n"
|
||||||
"void f(void) {\n"
|
"void f(void) {\n"
|
||||||
" struct AB ab;\n"
|
" struct AB ab;\n"
|
||||||
|
|
Loading…
Reference in New Issue