From 2e707974d07f5e4dd8de026fa30b1488850a596c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 21 Jan 2010 18:50:56 +0100 Subject: [PATCH] Fixed #1273 (scope of variable limited not detected in latest versions) --- lib/checkother.cpp | 23 ++++++++++++++--------- test/testother.cpp | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 3454390d2..bb835c99b 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -457,10 +457,11 @@ void CheckOther::lookupVar(const Token *tok1, const char varname[]) tok = tok->next(); // Check if the variable is used in this indentlevel.. - bool used = false, used1 = false; + bool used1 = false; // used in one sub-scope -> reducable + bool used2 = false; // used in more sub-scopes -> not reducable int indentlevel = 0; int parlevel = 0; - bool for_or_while = false; + bool for_or_while = false; // is sub-scope a "for/while/etc". anything that is not "if" while (tok) { if (tok->str() == "{") @@ -475,10 +476,10 @@ void CheckOther::lookupVar(const Token *tok1, const char varname[]) --indentlevel; if (indentlevel == 0) { - if (for_or_while && used) + if (for_or_while && used2) return; - used1 |= used; - used = false; + used2 |= used1; + used1 = false; } } @@ -500,9 +501,13 @@ void CheckOther::lookupVar(const Token *tok1, const char varname[]) else if (tok->str() == varname) { - if (indentlevel == 0 || used1) + if (indentlevel == 0) + return; + used1 = true; + if (for_or_while && !Token::simpleMatch(tok->next(), "=")) + used2 = true; + if (used1 && used2) return; - used = true; } else if (indentlevel == 0) @@ -511,7 +516,7 @@ void CheckOther::lookupVar(const Token *tok1, const char varname[]) // If %unknown% is anything except if, we assume // that it is a for or while loop or a macro hiding either one if (Token::simpleMatch(tok->next(), "(") && - Token::simpleMatch(tok->next()->link()->next(), "{")) + Token::simpleMatch(tok->next()->link(), ") {")) { if (tok->str() != "if") for_or_while = true; @@ -530,7 +535,7 @@ void CheckOther::lookupVar(const Token *tok1, const char varname[]) // Warning if this variable: // * not used in this indentlevel // * used in lower indentlevel - if (!used && used1) + if (used1 || used2) variableScopeError(tok1, varname); } //--------------------------------------------------------------------------- diff --git a/test/testother.cpp b/test/testother.cpp index f08fdc584..6614fed17 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -60,6 +60,7 @@ private: TEST_CASE(varScope5); TEST_CASE(varScope6); TEST_CASE(varScope7); + TEST_CASE(varScope8); TEST_CASE(nullpointer1); TEST_CASE(nullpointer2); @@ -601,6 +602,19 @@ private: ASSERT_EQUALS("", errout.str()); } + void varScope8() + { + varScope("void test() {\n" + " float edgeResistance=1;\n" + " std::vector edges;\n" + " BOOST_FOREACH(int edge, edges) {\n" + " edgeResistance = (edge+1) / 2.0;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable edgeResistance can be reduced\n", errout.str()); + } + + void checkNullPointer(const char code[]) { // Tokenize..