diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 4e36467bf04..7ce49292ffd 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -947,7 +947,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable* const vari } // Returning from function.. - else if ((tok3->scope()->type != ScopeType::eLambda || tok3->scope() == variable->scope()) && tok3->str() == "return") { + else if (((!isWithinScope(tok3, variable, ScopeType::eLambda) && !isWithinScope(tok3, variable, ScopeType::eSwitch)) || tok3->scope() == variable->scope()) && tok3->str() == "return") { // Returning from function without deallocating struct member? if (!Token::Match(tok3, "return %varid% ;", structid) && !Token::Match(tok3, "return & %varid%", structid) && diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 39f298fa923..08ea1530f90 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2064,7 +2064,22 @@ class TestMemleakStructMember : public TestFixture { " }\n" " return g(&a);\n" "}\n"); - TODO_ASSERT_EQUALS("", "[test.cpp:9:9]: (error) Memory leak: a.str [memleak]\n", errout_str()); + ASSERT_EQUALS("", errout_str()); + + check("struct S { int *p; };\n" + "S f(int i) {\n" + " S s;\n" + " switch(i) {\n" + " case 1:\n" + " s.p = new int;\n" + " break;\n" + " default: {\n" + " return {};\n" + " }\n" + " }\n" + " return s;\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); } void ifelse() {