You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"Using an operator like '" + operatorString + "' in a case label is suspicious. Did you intend to use a bitwise operator, multiple case labels or if/else instead?", CWE398, Certainty::inconclusive);
969
969
}
970
970
971
+
voidCheckOtherImpl::checkUnreachableSwitchCase()
972
+
{
973
+
if (!mSettings.severity.isEnabled(Severity::style))
Copy file name to clipboardExpand all lines: test/testother.cpp
+56Lines changed: 56 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -142,6 +142,7 @@ class TestOther : public TestFixture {
142
142
TEST_CASE(switchRedundantOperationTest);
143
143
TEST_CASE(switchRedundantBitwiseOperationTest);
144
144
TEST_CASE(unreachableCode);
145
+
TEST_CASE(unreachableSwitchCase); // #8442
145
146
TEST_CASE(redundantContinue);
146
147
147
148
TEST_CASE(suspiciousCase);
@@ -6348,6 +6349,61 @@ class TestOther : public TestFixture {
6348
6349
ASSERT_EQUALS("", errout_str());
6349
6350
}
6350
6351
6352
+
void unreachableSwitchCase() {
6353
+
check("enum T { A, B};\n"
6354
+
"void f(const T &t) {\n"
6355
+
" if (t == A) {\n"
6356
+
" switch (t) {\n"
6357
+
" case A:\n"
6358
+
" break;\n"
6359
+
" case B:\n"
6360
+
" break;\n"
6361
+
" }\n"
6362
+
" }\n"
6363
+
"}\n");
6364
+
ASSERT_EQUALS("[test.cpp:7:9]: (style) Switch case 'B' can never be selected because the switch condition has a known value. [unreachableSwitchCase]\n", errout_str());
6365
+
6366
+
check("void f(int t) {\n"
6367
+
" if (t == 0) {\n"
6368
+
" switch (t) {\n"
6369
+
" case 0:\n"
6370
+
" break;\n"
6371
+
" case 1:\n"
6372
+
" break;\n"
6373
+
" }\n"
6374
+
" }\n"
6375
+
"}\n");
6376
+
ASSERT_EQUALS("[test.cpp:6:9]: (style) Switch case '1' can never be selected because the switch condition has a known value. [unreachableSwitchCase]\n", errout_str());
6377
+
6378
+
check("void f(int t) {\n"
6379
+
" switch (t) {\n"
6380
+
" case 0:\n"
6381
+
" break;\n"
6382
+
" case 1:\n"
6383
+
" break;\n"
6384
+
" }\n"
6385
+
"}\n");
6386
+
ASSERT_EQUALS("", errout_str());
6387
+
6388
+
check("void f(int x, int y) {\n"
6389
+
" if (x == 0) {\n"
6390
+
" switch (x) {\n"
6391
+
" case 0:\n"
6392
+
" switch (y) {\n"
6393
+
" case 1:\n"
6394
+
" break;\n"
6395
+
" case 2:\n"
6396
+
" break;\n"
6397
+
" }\n"
6398
+
" break;\n"
6399
+
" case 1:\n"
6400
+
" break;\n"
6401
+
" }\n"
6402
+
" }\n"
6403
+
"}\n");
6404
+
ASSERT_EQUALS("[test.cpp:12:9]: (style) Switch case '1' can never be selected because the switch condition has a known value. [unreachableSwitchCase]\n", errout_str());
0 commit comments