diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 43829c89b..0d33b471f 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1595,7 +1595,7 @@ void CheckOther::checkVariableScope() tok = tok->tokAt(3); if (!tok->isNumber() && tok->type() != Token::eString && tok->type() != Token::eChar && !tok->isBoolean()) continue; - } else if (tok->str() == "=" && + } else if ((tok->str() == "=") && ((!tok->next()->isNumber() && tok->next()->type() != Token::eString && tok->next()->type() != Token::eChar && !tok->next()->isBoolean()) || tok->strAt(2) != ";")) continue; lookupVar(tok, var); diff --git a/test/testother.cpp b/test/testother.cpp index b106e2771..c481f84f1 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -64,6 +64,7 @@ private: TEST_CASE(varScope11); // #2475 - struct initialization is not inner scope TEST_CASE(varScope12); TEST_CASE(varScope13); // variable usage in inner loop + TEST_CASE(varScope14); TEST_CASE(oldStylePointerCast); TEST_CASE(invalidPointerCast); @@ -754,6 +755,17 @@ private: ASSERT_EQUALS("", errout.str()); } + void varScope14() { + // #3941 + varScope("void f() {\n" + " const int i( foo());\n" + " if(a) {\n" + " for ( ; i < 10; ++i) ;\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void checkOldStylePointerCast(const char code[]) { // Clear the error buffer.. errout.str("");