From d363a493155a0a117783c1dddd3c5cb039cdea45 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 14 Jan 2025 10:56:50 +0100 Subject: [PATCH 1/7] Update testother.cpp --- test/testother.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/testother.cpp b/test/testother.cpp index 4dceade465e..f96a917c55e 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -5710,6 +5710,20 @@ class TestOther : public TestFixture { "}\n"); ASSERT_EQUALS("", errout_str()); + check("int f(int i) {\n" + " switch (i) {\n" + " case 0:\n" + " return 0;\n" + " int a[1];\n" + " case 1:\n" + " case 2:\n" + " a[0] = 5;\n" + " return a[0] + i;\n" + " }\n" + " return 3;\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + check("int f(int i) {\n" " switch (i) {\n" " case 0:\n" From e630baa3d59854241dc0db3ed602df63fb6af785 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 14 Jan 2025 10:58:17 +0100 Subject: [PATCH 2/7] Update checkother.cpp --- lib/checkother.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 99490707cb3..a80b5d3cf25 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -871,8 +871,14 @@ static bool isVardeclInSwitch(const Token* tok) return false; if (!isNestedInSwitch(tok->scope())) return false; - const Token* end = Token::findsimplematch(tok, ";"); - return end && end->previous()->variable() && end->previous()->variable()->nameToken() == end->previous(); + if (const Token* end = Token::findsimplematch(tok, ";")) { + for (const Token* tok2 = tok; tok2 != end; tok2 = tok2->next()) { + if (tok2->str() == "case") + return false; + if (tok2->variable() && tok2->variable()->nameToken() == tok2) + return true; + } + } } //--------------------------------------------------------------------------- From d443b89717a784bd9aff6ef4fa761106e3a77aed Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 14 Jan 2025 11:02:34 +0100 Subject: [PATCH 3/7] Update checkother.cpp --- lib/checkother.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index a80b5d3cf25..9f7b6d420ec 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -879,6 +879,7 @@ static bool isVardeclInSwitch(const Token* tok) return true; } } + return false; } //--------------------------------------------------------------------------- From 38bbe462e183eb5e4052c7debefc25c40beff489 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 14 Jan 2025 11:34:33 +0100 Subject: [PATCH 4/7] Update checkother.cpp --- lib/checkother.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 9f7b6d420ec..4d668bd4856 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -873,10 +873,16 @@ static bool isVardeclInSwitch(const Token* tok) return false; if (const Token* end = Token::findsimplematch(tok, ";")) { for (const Token* tok2 = tok; tok2 != end; tok2 = tok2->next()) { - if (tok2->str() == "case") + if (tok2->isKeyword() && tok2->str() == "case") return false; - if (tok2->variable() && tok2->variable()->nameToken() == tok2) - return true; + if (tok2->variable() && tok2->variable()->nameToken() == tok2) { + end = tok2->scope()->bodyEnd; + for (const Token* tok3 = tok; tok3 != end; tok3 = tok3->next()) { + if (tok3->isKeyword() && tok3->str() == "case") + return true; + } + return false; + } } } return false; From fe131b8f9b02072d84a9a20bd10aa26aad9a2563 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 14 Jan 2025 11:35:09 +0100 Subject: [PATCH 5/7] Update testother.cpp --- test/testother.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/testother.cpp b/test/testother.cpp index f96a917c55e..25998f61300 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -5794,6 +5794,18 @@ class TestOther : public TestFixture { " return x;\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + check("bool f(int x, int y) {\n" // #13544 + " switch (x) {\n" + " case 1: {\n" + " return y != 0;\n" + " int z = y + 5;\n" + " return z != 7;\n" + " }\n" + " }\n" + " return false;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:5]: (style) Statements following 'return' will never be executed.\n", errout_str()); } From 34ce2d130a6ff7cd5545d6177e38dabb3c1a1354 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 14 Jan 2025 16:14:48 +0100 Subject: [PATCH 6/7] Update checkother.cpp --- lib/checkother.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 4d668bd4856..71630115d3d 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -878,10 +878,10 @@ static bool isVardeclInSwitch(const Token* tok) if (tok2->variable() && tok2->variable()->nameToken() == tok2) { end = tok2->scope()->bodyEnd; for (const Token* tok3 = tok; tok3 != end; tok3 = tok3->next()) { - if (tok3->isKeyword() && tok3->str() == "case") - return true; + if (tok3->isKeyword()) + return tok3->str() == "case"; } - return false; + break; } } } From 6d5623bb37dc6e90b316b80494482317bc3a2f1d Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 14 Jan 2025 18:28:19 +0100 Subject: [PATCH 7/7] Update checkother.cpp --- lib/checkother.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 71630115d3d..7a6682973ff 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -877,11 +877,11 @@ static bool isVardeclInSwitch(const Token* tok) return false; if (tok2->variable() && tok2->variable()->nameToken() == tok2) { end = tok2->scope()->bodyEnd; - for (const Token* tok3 = tok; tok3 != end; tok3 = tok3->next()) { + for (const Token* tok3 = tok2; tok3 != end; tok3 = tok3->next()) { if (tok3->isKeyword()) return tok3->str() == "case"; } - break; + return false; } } }