From a6e901e6983ad8d7cf888ab52e18f39cff8046df Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 10 Apr 2026 11:40:37 +0200 Subject: [PATCH 1/5] Update checkmemoryleak.cpp --- lib/checkmemoryleak.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 4e36467bf04..91e80e0e887 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 (((tok3->scope()->type != ScopeType::eLambda && tok3->scope()->type != 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) && From 6dcc01066e5d5ab075c9924352ddd7f2ab1b3ef9 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 10 Apr 2026 11:41:16 +0200 Subject: [PATCH 2/5] Update testmemleak.cpp --- test/testmemleak.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 39f298fa923..fc010e02475 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2064,7 +2064,7 @@ 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()); } void ifelse() { From e1abbccd66b097ab7af9ed9195a3e993f46832d3 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 10 Apr 2026 12:51:21 +0200 Subject: [PATCH 3/5] Update testmemleak.cpp [skip ci] --- test/testmemleak.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index fc010e02475..28515735ef4 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2065,6 +2065,21 @@ class TestMemleakStructMember : public TestFixture { " return g(&a);\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + check("struct S { int *p; }; + "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() { From 347a22a1f7dd326ec76ecf0baa4522e63c502b14 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 10 Apr 2026 12:52:50 +0200 Subject: [PATCH 4/5] Update checkmemoryleak.cpp --- lib/checkmemoryleak.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 91e80e0e887..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()->type != ScopeType::eSwitch) || 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) && From 02d8d77e73ee4ffd95c51662d8569f90303115da Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 10 Apr 2026 12:54:50 +0200 Subject: [PATCH 5/5] Update testmemleak.cpp --- test/testmemleak.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 28515735ef4..08ea1530f90 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2066,7 +2066,7 @@ class TestMemleakStructMember : public TestFixture { "}\n"); ASSERT_EQUALS("", errout_str()); - check("struct S { int *p; }; + check("struct S { int *p; };\n" "S f(int i) {\n" " S s;\n" " switch(i) {\n"