diff --git a/src/checkother.cpp b/src/checkother.cpp index b77d20a99..6fa235005 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -783,13 +783,12 @@ void CheckOther::CheckIncompleteStatement() if ((tok->str() != "#") && Token::Match(tok->next(), "; %str%") && !Token::Match(tok->tokAt(3), ",")) { - std::ostringstream errmsg; - errmsg << _tokenizer->fileLine(tok->next()) << ": Redundant code: Found a statement that begins with string constant"; - _errorLogger->reportErr(errmsg.str()); + _errorLogger->reportErr(ErrorMessage::constStatement(_tokenizer, tok->next(), "string")); } if (!Token::Match(tok, "#") && Token::Match(tok->next(), "; %num%") && !Token::Match(tok->tokAt(3), ",")) { + _errorLogger->reportErr(ErrorMessage::constStatement(_tokenizer, tok->next(), "numeric")); std::ostringstream errmsg; errmsg << _tokenizer->fileLine(tok->next()) << ": Redundant code: Found a statement that begins with numeric constant"; _errorLogger->reportErr(errmsg.str()); diff --git a/src/cppcheck.cpp b/src/cppcheck.cpp index 12a04ef4c..fa2d2b135 100644 --- a/src/cppcheck.cpp +++ b/src/cppcheck.cpp @@ -339,11 +339,12 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) { // Variable scope (check if the scope could be limited) //CheckVariableScope(); - - // Check for various types of incomplete statements that could for example - // mean that an ';' has been added by accident - checkOther.CheckIncompleteStatement(); } + + // Check for various types of incomplete statements that could for example + // mean that an ';' has been added by accident + if (ErrorMessage::constStatement(_settings)) + checkOther.CheckIncompleteStatement(); } //--------------------------------------------------------------------------- diff --git a/src/errormessage.h b/src/errormessage.h index 19c2d3af7..45186bc26 100644 --- a/src/errormessage.h +++ b/src/errormessage.h @@ -273,5 +273,14 @@ public: return s._checkCodingStyle; } + static std::string constStatement(const Tokenizer *tokenizer, const Token *Location, const std::string &type) + { + return msg1(tokenizer, Location) + "Redundant code: Found a statement that begins with " + type + " constant"; + } + static bool constStatement(const Settings &s) + { + return s._checkCodingStyle; + } + }; #endif diff --git a/tools/errmsg.cpp b/tools/errmsg.cpp index 008af9ec3..bd12dbb4d 100644 --- a/tools/errmsg.cpp +++ b/tools/errmsg.cpp @@ -89,6 +89,7 @@ int main() err.push_back(Message("unusedVariable", Message::STYLE, "Unused variable '%1'", "varname")); err.push_back(Message("unreadVariable", Message::STYLE, "Variable '%1' is assigned a value that is never used", "varname")); err.push_back(Message("unassignedVariable", Message::STYLE, "Variable '%1' is not assigned a value", "varname")); + err.push_back(Message("constStatement", Message::STYLE, "Redundant code: Found a statement that begins with %1 constant", "type")); // Generate code.. std::cout << "Generate code.." << std::endl;