diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 66d4fd70b..9bd084595 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1132,6 +1132,13 @@ const Token* CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, 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 // FIXME this code is a hack!! if (mTokenizer->isCPP() && Token::Match(valueExpr->astParent(), "<<|>>")) { diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 9aaa024c1..72a684aac 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -4121,14 +4121,20 @@ private: ASSERT_EQUALS("", errout.str()); } - void uninitvar_rangeBasedFor() { // #7078 - checkUninitVar("void function(Entry& entry) {\n" + void uninitvar_rangeBasedFor() { + checkUninitVar("void function(Entry& entry) {\n" // #7078 " for (auto* expr : entry.exprs) {\n" " expr->operate();\n" " expr->operate();\n" " }\n" "}"); ASSERT_EQUALS("", errout.str()); + + checkUninitVar("void f() {\n" + " int *item;\n" + " for (item: itemList) {}\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void uninitvar_static() { // #8734