From dbf1f199c34a93496b0d24b3ded2a621e0460ff3 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 25 Dec 2024 09:01:47 +0100 Subject: [PATCH] Fix #12244 FN: unreachableCode with unconditional scope (#7130) --- lib/checkother.cpp | 2 ++ test/testother.cpp | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index eb4b0e0ea98..42ca0f0d2ba 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -887,6 +887,8 @@ void CheckOther::checkUnreachableCode() tok2 = tok2->link(); if (tok2->str() == ";") { secondBreak = tok2->next(); + while (Token::simpleMatch(secondBreak, "}") && secondBreak->scope()->type == Scope::ScopeType::eUnconditional) + secondBreak = secondBreak->next(); break; } } diff --git a/test/testother.cpp b/test/testother.cpp index 6e7b2ec04b8..1caa442ffa8 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -5606,6 +5606,15 @@ class TestOther : public TestFixture { " } while (0);\n" "}\n"); ASSERT_EQUALS("[test.cpp:4]: (style) Statements following 'break' will never be executed.\n", errout_str()); + + check("void f() {\n" // #12244 + " {\n" + " std::cout << \"x\";\n" + " return;\n" + " }\n" + " std::cout << \"y\";\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:6]: (style) Statements following 'return' will never be executed.\n", errout_str()); } void redundantContinue() {