diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 99024ed9a..ef1e458d2 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -972,6 +972,13 @@ void CheckMemoryLeakClass::simplifycode(Token *tok) done = false; } + // Replace "loop if return ;" with "if return ;" + if (Token::Match(tok2->next(), "loop if return")) + { + erase(tok2, tok2->tokAt(2)); + done = false; + } + // Delete if block in "alloc ; if(!var) return ;" if (Token::Match(tok2, "alloc ; if(!var) return ;")) { diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 2b5345ca2..9adbc2e85 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -109,6 +109,7 @@ private: TEST_CASE(switch1); TEST_CASE(switch2); + TEST_CASE(switch3); TEST_CASE(ret1); TEST_CASE(ret2); @@ -835,7 +836,6 @@ private: ASSERT_EQUALS(std::string(""), errout.str()); } - void switch2() { const std::string code("void f()\n" @@ -856,6 +856,23 @@ private: ASSERT_EQUALS("[test.cpp:12]: Memory leak: str\n", errout.str()); } + void switch3() + { + check("void f()\n" + "{\n" + " char *str = new char[10];\n" + " while (abc)\n" + " {\n" + " switch (def)\n" + " {\n" + " default:\n" + " return;\n" + " }\n" + " }\n" + " delete [] str;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:9]: Memory leak: str\n", errout.str()); + }