diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 1f80810dc..cba0f56fa 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -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 (")) - return nullptr; + 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!! diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 044efca34..a971a9e09 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -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