Skip to content

Commit 1599f85

Browse files
Fix #13478 FP unreachableCode within switch (danmar#7137)
1 parent 4441ebd commit 1599f85

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/checkother.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,14 @@ void CheckOther::checkUnreachableCode()
903903
}
904904
while (Token::simpleMatch(secondBreak, "}") && secondBreak->scope()->type == Scope::ScopeType::eUnconditional)
905905
secondBreak = secondBreak->next();
906-
if (Token::simpleMatch(secondBreak, ";"))
906+
if (secondBreak && secondBreak->scope()->nestedIn && secondBreak->scope()->nestedIn->type == Scope::ScopeType::eSwitch &&
907+
tok->str() == "break") {
908+
while (Token::simpleMatch(secondBreak, "{") && secondBreak->scope()->type == Scope::ScopeType::eUnconditional)
909+
secondBreak = secondBreak->next();
910+
if (Token::Match(secondBreak, "%name% :"))
911+
continue;
912+
}
913+
if (Token::simpleMatch(secondBreak, "; }"))
907914
continue;
908915

909916
// Statements follow directly, no line between them. (#3383)

test/testother.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5649,6 +5649,16 @@ class TestOther : public TestFixture {
56495649
" { return 0; };\n"
56505650
"}\n");
56515651
ASSERT_EQUALS("", errout_str());
5652+
5653+
check("int f(int i) {\n" // #13478
5654+
" int x = 0;\n"
5655+
" switch (i) {\n"
5656+
" { case 0: x = 5; break; }\n"
5657+
" { case 1: x = 7; break; }\n"
5658+
" }\n"
5659+
" return x;\n"
5660+
"}\n");
5661+
ASSERT_EQUALS("", errout_str());
56525662
}
56535663

56545664

0 commit comments

Comments
 (0)