Improved CheckOther::checkUnusedLabel(): Write a warning instead of a style message if it happens inside a switch()

This commit is contained in:
PKEuS 2016-01-31 12:04:28 +01:00
parent cdf68353ae
commit 9d2258677d
3 changed files with 28 additions and 7 deletions

View File

@ -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.");
}
}

View File

@ -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);
}

View File

@ -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() {