diff --git a/lib/checkother.cpp b/lib/checkother.cpp index d6b11a6d0..645380f85 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2149,7 +2149,7 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us if (tok == forHeadEnd) forHeadEnd = 0; - if (noContinue && tok->scope() == scope && !forHeadEnd && scope->type != Scope::eSwitch && Token::Match(tok, "%varid% =", var->varId())) { // Assigned in outer scope. + if (loopVariable && noContinue && tok->scope() == scope && !forHeadEnd && scope->type != Scope::eSwitch && Token::Match(tok, "%varid% =", var->varId())) { // Assigned in outer scope. loopVariable = false; unsigned int indent = 0; for (const Token* tok2 = tok->tokAt(2); tok2; tok2 = tok2->next()) { // Ensure that variable isn't used on right side of =, too diff --git a/test/testother.cpp b/test/testother.cpp index 40d961cab..9a43d00a6 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -69,6 +69,7 @@ private: TEST_CASE(varScope14); TEST_CASE(varScope15); // #4573 if-else-if TEST_CASE(varScope16); + TEST_CASE(varScope17); TEST_CASE(oldStylePointerCast); TEST_CASE(invalidPointerCast); @@ -905,6 +906,17 @@ private: ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'a' can be reduced.\n", errout.str()); } + void varScope17() { + varScope("void f() {\n" + " int x;\n" + " if (a) {\n" + " x = stuff(x);\n" + " morestuff(x);\n" + " }\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'x' can be reduced.\n", errout.str()); + } + void checkOldStylePointerCast(const char code[]) { // Clear the error buffer.. errout.str("");