From 5fac1f2daa68965eb31ac58960973b0dc185592d Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 28 Jul 2026 15:11:43 +0200 Subject: [PATCH 1/4] Update checkleakautovar.cpp --- lib/checkleakautovar.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index e8271f9eed0..cde222e6874 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -342,6 +342,8 @@ bool CheckLeakAutoVarImpl::checkScope(const Token * const startToken, } } + if (tok->str() == "}" && tok == tok->scope()->bodyEnd && tok->scope()->type == ScopeType::eUnconditional) + ret(tok, varInfo); // look for end of statement const bool isInit = Token::Match(tok->tokAt(-1), "%var% {|(") && tok->tokAt(-1)->variable() && tok->tokAt(-1) == tok->tokAt(-1)->variable()->nameToken(); From 2bd7e6f7035cf912423aefcccc64f07f04a49d52 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 28 Jul 2026 15:13:27 +0200 Subject: [PATCH 2/4] Update testleakautovar.cpp --- test/testleakautovar.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index b7f8b024a8c..95cb946a14c 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -211,6 +211,7 @@ class TestLeakAutoVar : public TestFixture { TEST_CASE(inlineFunction); // #3989 TEST_CASE(smartPtrInContainer); // #8262 + TEST_CASE(unconditionalScope); TEST_CASE(functionCallCastConfig); // #9652 TEST_CASE(functionCallLeakIgnoreConfig); // #7923 @@ -3178,6 +3179,24 @@ class TestLeakAutoVar : public TestFixture { ASSERT_EQUALS("", errout_str()); } + void unconditionalScope() { // #14945 + check("void f() {\n" + " {\n" + " int* p = new int;\n" + " *p = 1;\n" + " }\n" + " {\n" + " int* q = new int;\n" + " *q = 2;\n" + " delete q;\n" + " }\n" + " int* r = new int;\n" + " *r = 3;\n" + " delete r;\n" + "}\n", dinit(CheckOptions, $.cpp = true)); + ASSERT_EQUALS("[test.cpp:5:5]: (error) Memory leak: p [memleak]\n", errout_str()); + } + void functionCallCastConfig() { // #9652 constexpr char xmldata[] = "\n" "\n" From 4e595f4af3b74463d885181c10ce3b7747432c81 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 28 Jul 2026 16:06:52 +0200 Subject: [PATCH 3/4] Update checkleakautovar.cpp [skip ci] --- lib/checkleakautovar.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index cde222e6874..e5220e701a3 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -342,8 +342,8 @@ bool CheckLeakAutoVarImpl::checkScope(const Token * const startToken, } } - if (tok->str() == "}" && tok == tok->scope()->bodyEnd && tok->scope()->type == ScopeType::eUnconditional) - ret(tok, varInfo); + if (tok == tok->scope()->bodyEnd && tok->scope()->type == ScopeType::eUnconditional) + ret(tok, varInfo, /*isEndOfScope*/ true); // look for end of statement const bool isInit = Token::Match(tok->tokAt(-1), "%var% {|(") && tok->tokAt(-1)->variable() && tok->tokAt(-1) == tok->tokAt(-1)->variable()->nameToken(); From e155074b9eba9e3809a02ea224ab7c1b64a7d715 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 28 Jul 2026 16:09:14 +0200 Subject: [PATCH 4/4] Update testleakautovar.cpp --- test/testleakautovar.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 95cb946a14c..6b6397851fb 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -3179,8 +3179,8 @@ class TestLeakAutoVar : public TestFixture { ASSERT_EQUALS("", errout_str()); } - void unconditionalScope() { // #14945 - check("void f() {\n" + void unconditionalScope() { + check("void f() {\n" // #14945 " {\n" " int* p = new int;\n" " *p = 1;\n" @@ -3195,6 +3195,16 @@ class TestLeakAutoVar : public TestFixture { " delete r;\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:5:5]: (error) Memory leak: p [memleak]\n", errout_str()); + + check("void f() {\n" + " int* p = new int;\n" + " {\n" + " (void)p;\n" + " }\n" + " *p = 1;\n" + " delete p;\n" + "}\n", dinit(CheckOptions, $.cpp = true)); + ASSERT_EQUALS("", errout_str()); } void functionCallCastConfig() { // #9652