diff --git a/src/checkother.cpp b/src/checkother.cpp index 036375b48..321300503 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -594,7 +594,7 @@ void CheckOther::checkVariableScope() continue; // Variable declaration? - if (Token::Match(tok1, "%type% %var% ; %var% = %any% ;")) + if (Token::Match(tok1, "%type% %var% ; %var% = %num% ;")) { // Tokenizer modify "int i = 0;" to "int i; i = 0;", // so to handle this situation we just skip diff --git a/test/testother.cpp b/test/testother.cpp index 4b79e8a15..4a8e41ecd 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -58,6 +58,7 @@ private: TEST_CASE(varScope3); TEST_CASE(varScope4); TEST_CASE(varScope5); + TEST_CASE(varScope6); TEST_CASE(nullpointer1); TEST_CASE(nullpointer2); @@ -495,6 +496,21 @@ private: ASSERT_EQUALS("[test.cpp:3]: (style) The scope of the variable i can be limited\n", errout.str()); } + void varScope6() + { + varScope("void f(int x)\n" + "{\n" + " int i = x;\n" + " if (a) {\n" + " x++;\n" + " }\n" + " if (b) {\n" + " c(i);\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void checkNullPointer(const char code[])