errmsg: added message 'unreachable code below return'

This commit is contained in:
Daniel Marjamäki 2009-01-12 17:28:04 +00:00
parent a0ef840138
commit df689b454c
4 changed files with 16 additions and 6 deletions

View File

@ -828,9 +828,7 @@ void CheckOther::unreachableCode()
// If there is a statement below the return it is unreachable
if (!Token::Match(tok, "; case|default|}|#") && !Token::Match(tok, "; %var% :"))
{
std::ostringstream errmsg;
errmsg << _tokenizer->fileLine(tok->next()) << ": Unreachable code below a 'return'";
_errorLogger->reportErr(errmsg.str());
_errorLogger->reportErr(ErrorMessage::unreachableCode(_tokenizer, tok->next()));
}
// Find the next 'return' statement

View File

@ -323,6 +323,11 @@ void CppCheck::checkFile(const std::string &code, const char FileName[])
if (ErrorMessage::unusedStructMember(_settings))
checkOther.CheckStructMemberUsage();
// Unreachable code below a 'return' statement
if (ErrorMessage::unreachableCode(_settings))
checkOther.unreachableCode();
if (_settings._checkCodingStyle)
{
@ -335,9 +340,6 @@ void CppCheck::checkFile(const std::string &code, const char FileName[])
// Check for various types of incomplete statements that could for example
// mean that an ';' has been added by accident
checkOther.CheckIncompleteStatement();
// Unreachable code below a 'return' statement
checkOther.unreachableCode();
}
}
//---------------------------------------------------------------------------

View File

@ -228,5 +228,14 @@ public:
return s._checkCodingStyle;
}
static std::string unreachableCode(const Tokenizer *tokenizer, const Token *Location)
{
return msg1(tokenizer, Location) + "Unreachable code below a 'return'";
}
static bool unreachableCode(const Settings &s)
{
return true;
}
};
#endif

View File

@ -84,6 +84,7 @@ int main()
err.push_back(Message("udivError", 0, "Unsigned division. The result will be wrong."));
err.push_back(Message("udivWarning", Message::STYLE | Message::ALL, "Warning: Division with signed and unsigned operators"));
err.push_back(Message("unusedStructMember", Message::STYLE, "struct or union member '%1::%2' is never used", "structname", "varname"));
err.push_back(Message("unreachableCode", 0, "Unreachable code below a 'return'"));
// Generate code..
std::cout << "Generate code.." << std::endl;