Uninitialized variables; Fixed false positive in range for loop

This commit is contained in:
Daniel Marjamäki 2021-05-19 21:12:11 +02:00
parent fcdc0088c7
commit ca5fab8219
2 changed files with 15 additions and 4 deletions

View File

@ -1138,12 +1138,17 @@ const Token* CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer,
return nullptr;
}
// LHS in range for loop:
// range for loop
if (Token::simpleMatch(valueExpr->astParent(), ":") &&
astIsLhs(valueExpr) &&
valueExpr->astParent()->astParent() &&
Token::simpleMatch(valueExpr->astParent()->astParent()->previous(), "for ("))
Token::simpleMatch(valueExpr->astParent()->astParent()->previous(), "for (")) {
if (astIsLhs(valueExpr))
return nullptr;
// Taking value by reference?
const Token *lhs = valueExpr->astParent()->astOperand1();
if (lhs && lhs->variable() && lhs->variable()->nameToken() == lhs && lhs->variable()->isReference())
return nullptr;
}
// Stream read/write
// FIXME this code is a hack!!

View File

@ -4164,6 +4164,12 @@ private:
" for (item: itemList) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void f() {\n"
" int buf[10];\n"
" for (int &i: buf) { i = 0; }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void uninitvar_static() { // #8734