From 9d2258677de15e3b28a545eb6e765d6473ebd348 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Sun, 31 Jan 2016 12:04:28 +0100 Subject: [PATCH] Improved CheckOther::checkUnusedLabel(): Write a warning instead of a style message if it happens inside a switch() --- lib/checkother.cpp | 17 ++++++++++++----- lib/checkother.h | 5 +++-- test/testother.cpp | 13 +++++++++++++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 3be57f901..4f6b472b3 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2481,7 +2481,7 @@ void CheckOther::raceAfterInterlockedDecrementError(const Token* tok) void CheckOther::checkUnusedLabel() { - if (!_settings->isEnabled("style")) + if (!_settings->isEnabled("style") && !_settings->isEnabled("warning")) return; const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); @@ -2495,16 +2495,23 @@ void CheckOther::checkUnusedLabel() if (Token::Match(tok, "{|}|; %name% :") && tok->strAt(1) != "default") { if (!Token::findsimplematch(scope->classStart->next(), ("goto " + tok->strAt(1)).c_str(), scope->classEnd->previous())) - unusedLabelError(tok->next()); + unusedLabelError(tok->next(), tok->scope()->type == Scope::eSwitch); } } } } -void CheckOther::unusedLabelError(const Token* tok) +void CheckOther::unusedLabelError(const Token* tok, bool inSwitch) { - reportError(tok, Severity::style, "unusedLabel", - "Label '" + (tok?tok->str():emptyString) + "' is not used."); + if (inSwitch) { + if (!tok || _settings->isEnabled("warning")) + reportError(tok, Severity::warning, "unusedLabelSwitch", + "Label '" + (tok ? tok->str() : emptyString) + "' is not used. Should this be a 'case' of the enclosing switch()?"); + } else { + if (!tok || _settings->isEnabled("style")) + reportError(tok, Severity::style, "unusedLabel", + "Label '" + (tok ? tok->str() : emptyString) + "' is not used."); + } } diff --git a/lib/checkother.h b/lib/checkother.h index db8d0666a..7883e7108 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -254,7 +254,7 @@ private: void commaSeparatedReturnError(const Token *tok); void redundantPointerOpError(const Token* tok, const std::string& varname, bool inconclusive); void raceAfterInterlockedDecrementError(const Token* tok); - void unusedLabelError(const Token* tok); + void unusedLabelError(const Token* tok, bool inSwitch); void unknownEvaluationOrder(const Token* tok); void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const { @@ -309,7 +309,8 @@ private: c.nanInArithmeticExpressionError(0); c.commaSeparatedReturnError(0); c.redundantPointerOpError(0, "varname", false); - c.unusedLabelError(0); + c.unusedLabelError(0, true); + c.unusedLabelError(0, false); c.unknownEvaluationOrder(0); } diff --git a/test/testother.cpp b/test/testother.cpp index 1aee29424..f0def4c2c 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -6108,6 +6108,19 @@ private: " };\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("int test(char art) {\n" + " switch (art) {\n" + " caseZERO:\n" + " return 0;\n" + " case1:\n" + " return 1;\n" + " case 2:\n" + " return 2;\n" + " }\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (warning) Label 'caseZERO' is not used. Should this be a 'case' of the enclosing switch()?\n" + "[test.cpp:5]: (warning) Label 'case1' is not used. Should this be a 'case' of the enclosing switch()?\n", errout.str()); } void testEvaluationOrder() {