diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 645380f85..16b144558 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2104,10 +2104,17 @@ void CheckOther::checkVariableScope() bool used = false; // Don't warn about unused variables for (; tok != var->scope()->classEnd; tok = tok->next()) { if (tok->str() == "{" && tok->strAt(-1) != "=") { - if (used || !checkInnerScope(tok, var, used)) { + if (used) { + bool used2 = false; + if (!checkInnerScope(tok, var, used2) || used2) { + reduce = false; + break; + } + } else if (!checkInnerScope(tok, var, used)) { reduce = false; break; } + tok = tok->link(); } else if (tok->varId() == var->varId() || tok->str() == "goto") { reduce = false; @@ -2127,7 +2134,6 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us bool noContinue = true; const Token* forHeadEnd = 0; const Token* end = tok->link(); - if (scope->type == Scope::eUnconditional && (tok->strAt(-1) == ")" || tok->previous()->isName())) // Might be an unknown macro like BOOST_FOREACH loopVariable = true; diff --git a/test/testother.cpp b/test/testother.cpp index 9a43d00a6..ed91edb51 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -915,6 +915,16 @@ private: " }\n" "}"); ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'x' can be reduced.\n", errout.str()); + + varScope("void f() {\n" + " int x;\n" + " if (a) {\n" + " x = stuff(x);\n" + " morestuff(x);\n" + " }\n" + " if (b) {}\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable 'x' can be reduced.\n", errout.str()); } void checkOldStylePointerCast(const char code[]) {