diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 59e394d40..6f76f95e4 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -667,7 +667,7 @@ void CheckOther::checkVariableScope() lookupVar(tok1->tokAt(6), tok1->strAt(1)); } } - else if (Token::Match(tok1, "%type% %var% [;=]")) + else if (tok1->isStandardType() && Token::Match(tok1, "%type% %var% [;=]")) { lookupVar(tok1, tok1->strAt(1)); } diff --git a/test/testother.cpp b/test/testother.cpp index 2b6766954..b4580d5f2 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -61,6 +61,7 @@ private: TEST_CASE(varScope6); TEST_CASE(varScope7); TEST_CASE(varScope8); + TEST_CASE(varScope9); // classes may have extra side-effects TEST_CASE(nullpointer1); TEST_CASE(nullpointer2); @@ -633,6 +634,22 @@ private: ASSERT_EQUALS("[test.cpp:2]: (style) The scope of the variable edgeResistance can be reduced\n", errout.str()); } + void varScope9() + { + // classes may have extra side effects + varScope("class fred {\n" + "public:\n" + " void x();\n" + "};\n" + "void test(int a) {\n" + " fred f;\n" + " if (a == 2) {\n" + " f.x();\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void checkNullPointer(const char code[]) {