Uninitialized variables; Fixed FP in range based for loop

This commit is contained in:
Daniel Marjamäki 2021-05-17 13:31:16 +02:00
parent 4322311482
commit bde8b4316e
2 changed files with 15 additions and 2 deletions

View File

@ -1132,6 +1132,13 @@ const Token* CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer,
return nullptr; return nullptr;
} }
// LHS in range for loop:
if (Token::simpleMatch(valueExpr->astParent(), ":") &&
astIsLhs(valueExpr) &&
valueExpr->astParent()->astParent() &&
Token::simpleMatch(valueExpr->astParent()->astParent()->previous(), "for ("))
return nullptr;
// Stream read/write // Stream read/write
// FIXME this code is a hack!! // FIXME this code is a hack!!
if (mTokenizer->isCPP() && Token::Match(valueExpr->astParent(), "<<|>>")) { if (mTokenizer->isCPP() && Token::Match(valueExpr->astParent(), "<<|>>")) {

View File

@ -4121,14 +4121,20 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void uninitvar_rangeBasedFor() { // #7078 void uninitvar_rangeBasedFor() {
checkUninitVar("void function(Entry& entry) {\n" checkUninitVar("void function(Entry& entry) {\n" // #7078
" for (auto* expr : entry.exprs) {\n" " for (auto* expr : entry.exprs) {\n"
" expr->operate();\n" " expr->operate();\n"
" expr->operate();\n" " expr->operate();\n"
" }\n" " }\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar("void f() {\n"
" int *item;\n"
" for (item: itemList) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
void uninitvar_static() { // #8734 void uninitvar_static() { // #8734