Skip to content

Commit 2921054

Browse files
committed
Include known value in unreachable switch case error message
1 parent 20ff103 commit 2921054

4 files changed

Lines changed: 9 additions & 8 deletions

File tree

lib/checkother.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,16 +1015,16 @@ void CheckOtherImpl::checkUnreachableSwitchCase()
10151015
continue;
10161016
if (switchValue->intvalue == caseValue->intvalue)
10171017
continue;
1018-
unreachableSwitchCaseError(tok, caseExpression->expressionString());
1018+
unreachableSwitchCaseError(tok, caseExpression->expressionString(), MathLib::toString(switchValue->intvalue));
10191019
}
10201020
}
10211021
}
10221022

1023-
void CheckOtherImpl::unreachableSwitchCaseError(const Token* tok, const std::string& caseExpression)
1023+
void CheckOtherImpl::unreachableSwitchCaseError(const Token* tok, const std::string& caseExpression, const std::string& switchValue)
10241024
{
10251025
reportError(tok, Severity::style, "unreachableSwitchCase",
10261026
"Switch case '" + caseExpression +
1027-
"' can never be selected because the switch condition has a known value.",
1027+
"' can never be selected because the switch condition is known to be " + switchValue + ".",
10281028
CWE561, Certainty::normal);
10291029
}
10301030

@@ -4968,7 +4968,7 @@ void CheckOther::getErrorMessages(ErrorLogger& errorLogger, const Settings &sett
49684968
c.duplicateExpressionTernaryError(nullptr, ErrorPath{});
49694969
c.duplicateBreakError(nullptr, false);
49704970
c.unreachableCodeError(nullptr, nullptr, false);
4971-
c.unreachableSwitchCaseError(nullptr, "case");
4971+
c.unreachableSwitchCaseError(nullptr, "case", "0");
49724972
c.unsignedLessThanZeroError(nullptr, nullptr, "varname");
49734973
c.unsignedPositiveError(nullptr, nullptr, "varname");
49744974
c.pointerLessThanZeroError(nullptr, nullptr);

lib/checkother.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ class CPPCHECKLIB CheckOtherImpl : public CheckImpl {
293293
void redundantCopyError(const Token *tok1, const Token* tok2, const std::string& var);
294294
void redundantBitwiseOperationInSwitchError(const Token *tok, const std::string &varname);
295295
void suspiciousCaseInSwitchError(const Token* tok, const std::string& operatorString);
296-
void unreachableSwitchCaseError(const Token* tok, const std::string& caseExpression);
296+
void unreachableSwitchCaseError(const Token* tok, const std::string& caseExpression, const std::string& switchValue);
297297
void selfAssignmentError(const Token *tok, const std::string &varname);
298298
void misusedScopeObjectError(const Token *tok, const std::string &varname, bool isAssignment = false);
299299
void duplicateBranchError(const Token *tok1, const Token *tok2, ErrorPath errors);

releasenotes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ New checks:
88
- Warn when feof() is used as a while loop condition (wrongfeofUsage).
99
- ftell() result is unspecified when file is opened in mode "t".
1010
- Detect when an STL algorithm such as std::copy, std::equal, std::transform, etc. accesses more elements through an iterator than are available in the container (algorithmOutOfBounds).
11+
- Detect switch cases that cannot be selected when the switch condition has a known value (unreachableSwitchCase).
1112

1213
C/C++ support:
1314
-

test/testother.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6361,7 +6361,7 @@ class TestOther : public TestFixture {
63616361
" }\n"
63626362
" }\n"
63636363
"}\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());
6364+
ASSERT_EQUALS("[test.cpp:7:9]: (style) Switch case 'B' can never be selected because the switch condition is known to be 0. [unreachableSwitchCase]\n", errout_str());
63656365

63666366
check("void f(int t) {\n"
63676367
" if (t == 0) {\n"
@@ -6373,7 +6373,7 @@ class TestOther : public TestFixture {
63736373
" }\n"
63746374
" }\n"
63756375
"}\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());
6376+
ASSERT_EQUALS("[test.cpp:6:9]: (style) Switch case '1' can never be selected because the switch condition is known to be 0. [unreachableSwitchCase]\n", errout_str());
63776377

63786378
check("void f(int t) {\n"
63796379
" switch (t) {\n"
@@ -6401,7 +6401,7 @@ class TestOther : public TestFixture {
64016401
" }\n"
64026402
" }\n"
64036403
"}\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());
6404+
ASSERT_EQUALS("[test.cpp:12:9]: (style) Switch case '1' can never be selected because the switch condition is known to be 0. [unreachableSwitchCase]\n", errout_str());
64056405
}
64066406

64076407
void redundantContinue() {

0 commit comments

Comments
 (0)