diff --git a/src/checkother.cpp b/src/checkother.cpp index 401f386db..c0f257ea5 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -387,23 +387,7 @@ void CheckOther::checkVariableScope() { if (tok2->str() == "{") { - int indentlevel2 = 0; - for (tok = tok2; tok; tok = tok->next()) - { - if (tok->str() == "{") - { - ++indentlevel2; - } - if (tok->str() == "}") - { - --indentlevel2; - if (indentlevel2 <= 0) - { - tok = tok->next(); - break; - } - } - } + tok = tok2->link(); break; } if (Token::Match(tok2, "[,);]")) @@ -415,11 +399,11 @@ void CheckOther::checkVariableScope() break; } - if (tok->str() == "{") + else if (tok->str() == "{") { ++indentlevel; } - if (tok->str() == "}") + else if (tok->str() == "}") { --indentlevel; if (indentlevel == 0) diff --git a/src/checkother.h b/src/checkother.h index f70bd5984..62377fab0 100644 --- a/src/checkother.h +++ b/src/checkother.h @@ -53,6 +53,7 @@ public: checkOther.warningOldStylePointerCast(); checkOther.checkUnsignedDivision(); checkOther.checkCharVariable(); + checkOther.checkVariableScope(); } } @@ -63,7 +64,6 @@ public: if (settings->_checkCodingStyle) { checkOther.warningRedundantCode(); - checkOther.checkVariableScope(); checkOther.checkConstantFunctionParameter(); checkOther.checkStructMemberUsage(); checkOther.checkIncompleteStatement(); diff --git a/test/testother.cpp b/test/testother.cpp index e474ff365..2a6362995 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -59,6 +59,7 @@ private: TEST_CASE(varScope4); TEST_CASE(varScope5); TEST_CASE(varScope6); + TEST_CASE(varScope7); TEST_CASE(nullpointer1); TEST_CASE(nullpointer2); @@ -441,7 +442,6 @@ private: Tokenizer tokenizer; std::istringstream istr(code); tokenizer.tokenize(istr, "test.cpp"); - tokenizer.simplifyTokenList(); // Clear the error buffer.. errout.str(""); @@ -582,6 +582,19 @@ private: ASSERT_EQUALS("", errout.str()); } + void varScope7() + { + varScope("void f(int x)\n" + "{\n" + " int y = 0;\n" + " b(y);\n" + " if (x) {\n" + " y++;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void checkNullPointer(const char code[]) { // Tokenize..