Skip to content

Commit 9d22586

Browse files
committed
Improved CheckOther::checkUnusedLabel(): Write a warning instead of a style message if it happens inside a switch()
1 parent cdf6835 commit 9d22586

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

lib/checkother.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2481,7 +2481,7 @@ void CheckOther::raceAfterInterlockedDecrementError(const Token* tok)
24812481

24822482
void CheckOther::checkUnusedLabel()
24832483
{
2484-
if (!_settings->isEnabled("style"))
2484+
if (!_settings->isEnabled("style") && !_settings->isEnabled("warning"))
24852485
return;
24862486

24872487
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
@@ -2495,16 +2495,23 @@ void CheckOther::checkUnusedLabel()
24952495

24962496
if (Token::Match(tok, "{|}|; %name% :") && tok->strAt(1) != "default") {
24972497
if (!Token::findsimplematch(scope->classStart->next(), ("goto " + tok->strAt(1)).c_str(), scope->classEnd->previous()))
2498-
unusedLabelError(tok->next());
2498+
unusedLabelError(tok->next(), tok->scope()->type == Scope::eSwitch);
24992499
}
25002500
}
25012501
}
25022502
}
25032503

2504-
void CheckOther::unusedLabelError(const Token* tok)
2504+
void CheckOther::unusedLabelError(const Token* tok, bool inSwitch)
25052505
{
2506-
reportError(tok, Severity::style, "unusedLabel",
2507-
"Label '" + (tok?tok->str():emptyString) + "' is not used.");
2506+
if (inSwitch) {
2507+
if (!tok || _settings->isEnabled("warning"))
2508+
reportError(tok, Severity::warning, "unusedLabelSwitch",
2509+
"Label '" + (tok ? tok->str() : emptyString) + "' is not used. Should this be a 'case' of the enclosing switch()?");
2510+
} else {
2511+
if (!tok || _settings->isEnabled("style"))
2512+
reportError(tok, Severity::style, "unusedLabel",
2513+
"Label '" + (tok ? tok->str() : emptyString) + "' is not used.");
2514+
}
25082515
}
25092516

25102517

lib/checkother.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ class CPPCHECKLIB CheckOther : public Check {
254254
void commaSeparatedReturnError(const Token *tok);
255255
void redundantPointerOpError(const Token* tok, const std::string& varname, bool inconclusive);
256256
void raceAfterInterlockedDecrementError(const Token* tok);
257-
void unusedLabelError(const Token* tok);
257+
void unusedLabelError(const Token* tok, bool inSwitch);
258258
void unknownEvaluationOrder(const Token* tok);
259259

260260
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
@@ -309,7 +309,8 @@ class CPPCHECKLIB CheckOther : public Check {
309309
c.nanInArithmeticExpressionError(0);
310310
c.commaSeparatedReturnError(0);
311311
c.redundantPointerOpError(0, "varname", false);
312-
c.unusedLabelError(0);
312+
c.unusedLabelError(0, true);
313+
c.unusedLabelError(0, false);
313314
c.unknownEvaluationOrder(0);
314315
}
315316

test/testother.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6108,6 +6108,19 @@ class TestOther : public TestFixture {
61086108
" };\n"
61096109
"}");
61106110
ASSERT_EQUALS("", errout.str());
6111+
6112+
check("int test(char art) {\n"
6113+
" switch (art) {\n"
6114+
" caseZERO:\n"
6115+
" return 0;\n"
6116+
" case1:\n"
6117+
" return 1;\n"
6118+
" case 2:\n"
6119+
" return 2;\n"
6120+
" }\n"
6121+
"}");
6122+
ASSERT_EQUALS("[test.cpp:3]: (warning) Label 'caseZERO' is not used. Should this be a 'case' of the enclosing switch()?\n"
6123+
"[test.cpp:5]: (warning) Label 'case1' is not used. Should this be a 'case' of the enclosing switch()?\n", errout.str());
61116124
}
61126125

61136126
void testEvaluationOrder() {

0 commit comments

Comments
 (0)