diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index b8b6e2e0d..f9c6a59da 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -705,7 +705,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const for (; defValTok; defValTok = defValTok->next()) { if (defValTok->str() == "[") defValTok = defValTok->link(); - else if (defValTok->str() == "(" || defValTok->str() == "{" || defValTok->str() == "=") { + else if (defValTok->str() == "(" || defValTok->str() == "{" || defValTok->str() == "=" || defValTok->str() == ":") { variables.addVar(&*i, type, true); break; } else if (defValTok->str() == ";" || defValTok->str() == "," || defValTok->str() == ")") { diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 50177eb3f..e7a88070b 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -154,6 +154,7 @@ private: TEST_CASE(localvarSwitch); // #3744 - false positive when localvar is used in switch TEST_CASE(localvarNULL); // #4203 - Setting NULL value is not redundant - it is safe TEST_CASE(localvarUnusedGoto); // #4447, #4558 goto + TEST_CASE(localvarRangeBasedFor); // #7075 TEST_CASE(localvarCpp11Initialization); @@ -3844,6 +3845,15 @@ private: ASSERT_EQUALS("", errout.str()); } + void localvarRangeBasedFor() { + // #7075 + functionVariableUsage("void reset() {\n" + " for (auto & e : array)\n" + " e = 0;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void chainedAssignment() { // #5466 functionVariableUsage("void NotUsed(double* pdD, int n) {\n"