Skip to content

Commit df74fc0

Browse files
Fix #13544 FN unreachableCode / Fixup #13491 FP unreachableCode for declaration in switch (#7219)
1 parent 599f505 commit df74fc0

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

lib/checkother.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -871,8 +871,21 @@ static bool isVardeclInSwitch(const Token* tok)
871871
return false;
872872
if (!isNestedInSwitch(tok->scope()))
873873
return false;
874-
const Token* end = Token::findsimplematch(tok, ";");
875-
return end && end->previous()->variable() && end->previous()->variable()->nameToken() == end->previous();
874+
if (const Token* end = Token::findsimplematch(tok, ";")) {
875+
for (const Token* tok2 = tok; tok2 != end; tok2 = tok2->next()) {
876+
if (tok2->isKeyword() && tok2->str() == "case")
877+
return false;
878+
if (tok2->variable() && tok2->variable()->nameToken() == tok2) {
879+
end = tok2->scope()->bodyEnd;
880+
for (const Token* tok3 = tok2; tok3 != end; tok3 = tok3->next()) {
881+
if (tok3->isKeyword())
882+
return tok3->str() == "case";
883+
}
884+
return false;
885+
}
886+
}
887+
}
888+
return false;
876889
}
877890

878891
//---------------------------------------------------------------------------

test/testother.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5710,6 +5710,20 @@ class TestOther : public TestFixture {
57105710
"}\n");
57115711
ASSERT_EQUALS("", errout_str());
57125712

5713+
check("int f(int i) {\n"
5714+
" switch (i) {\n"
5715+
" case 0:\n"
5716+
" return 0;\n"
5717+
" int a[1];\n"
5718+
" case 1:\n"
5719+
" case 2:\n"
5720+
" a[0] = 5;\n"
5721+
" return a[0] + i;\n"
5722+
" }\n"
5723+
" return 3;\n"
5724+
"}\n");
5725+
ASSERT_EQUALS("", errout_str());
5726+
57135727
check("int f(int i) {\n"
57145728
" switch (i) {\n"
57155729
" case 0:\n"
@@ -5780,6 +5794,18 @@ class TestOther : public TestFixture {
57805794
" return x;\n"
57815795
"}\n");
57825796
ASSERT_EQUALS("", errout_str());
5797+
5798+
check("bool f(int x, int y) {\n" // #13544
5799+
" switch (x) {\n"
5800+
" case 1: {\n"
5801+
" return y != 0;\n"
5802+
" int z = y + 5;\n"
5803+
" return z != 7;\n"
5804+
" }\n"
5805+
" }\n"
5806+
" return false;\n"
5807+
"}\n");
5808+
ASSERT_EQUALS("[test.cpp:5]: (style) Statements following 'return' will never be executed.\n", errout_str());
57835809
}
57845810

57855811

0 commit comments

Comments
 (0)