From d9f7042992e384c7c55714afe0bbe8a71e3fa8ba Mon Sep 17 00:00:00 2001 From: PKEuS Date: Mon, 18 Mar 2013 08:17:53 -0700 Subject: [PATCH] Fixed false negative #4663 --- lib/checkother.cpp | 10 ++++++++-- test/testother.cpp | 10 ++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) 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[]) {