diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 73cfe8e42..6698b96df 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1049,8 +1049,11 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us if (Token::Match(tok, "& %varid%", var->declarationId())) // Taking address of variable return false; - if (Token::Match(tok, "%varid% =", var->declarationId())) + if (Token::Match(tok, "%varid% =", var->declarationId())) { + if (!bFirstAssignment && var->isInit() && Token::findmatch(tok->tokAt(2), "%varid%", Token::findsimplematch(tok->tokAt(3), ";"), var->declarationId())) + return false; bFirstAssignment = true; + } if (!bFirstAssignment && Token::Match(tok, "* %varid%", var->declarationId())) // dereferencing means access to previous content return false; diff --git a/test/testother.cpp b/test/testother.cpp index 9e54183b9..cb15ac2e2 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -105,6 +105,7 @@ private: TEST_CASE(varScope31); // #11099 TEST_CASE(varScope32); // #11441 TEST_CASE(varScope33); + TEST_CASE(varScope34); TEST_CASE(oldStylePointerCast); TEST_CASE(invalidPointerCast); @@ -1614,6 +1615,19 @@ private: errout.str()); } + void varScope34() { // #11742 + check("void f() {\n" + " bool b = false;\n" + " int i = 1;\n" + " for (int k = 0; k < 20; ++k) {\n" + " b = !b;\n" + " if (b)\n" + " i++;\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + #define checkOldStylePointerCast(code) checkOldStylePointerCast_(code, __FILE__, __LINE__) void checkOldStylePointerCast_(const char code[], const char* file, int line) { // Clear the error buffer..