diff --git a/src/checkother.cpp b/src/checkother.cpp index 21b073d2b..b77d20a99 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -938,23 +938,17 @@ void CheckOther::functionVariableUsage() if (usage == USAGE_DECLARE) { - std::ostringstream errmsg; - errmsg << _tokenizer->fileLine(tok1->next()) << ": Unused variable '" << varname << "'"; - _errorLogger->reportErr(errmsg.str()); + _errorLogger->reportErr(ErrorMessage::unusedVariable(_tokenizer, tok1->next(), varname)); } else if (!(usage & USAGE_READ)) { - std::ostringstream errmsg; - errmsg << _tokenizer->fileLine(tok1->next()) << ": Variable '" << varname << "' is assigned a value that is never used"; - _errorLogger->reportErr(errmsg.str()); + _errorLogger->reportErr(ErrorMessage::unreadVariable(_tokenizer, tok1->next(), varname)); } else if (!(usage & USAGE_WRITE)) { - std::ostringstream errmsg; - errmsg << _tokenizer->fileLine(tok1->next()) << ": Variable '" << varname << "' is not assigned a value"; - _errorLogger->reportErr(errmsg.str()); + _errorLogger->reportErr(ErrorMessage::unassignedVariable(_tokenizer, tok1->next(), varname)); } } } diff --git a/src/cppcheck.cpp b/src/cppcheck.cpp index 67771aa96..12a04ef4c 100644 --- a/src/cppcheck.cpp +++ b/src/cppcheck.cpp @@ -249,10 +249,13 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) // Give warning when using char variable as array index checkOther.CheckCharVariable(); - // Usage of local variables - checkOther.functionVariableUsage(); } + // Usage of local variables + if (ErrorMessage::unusedVariable(_settings)) + checkOther.functionVariableUsage(); + + _tokenizer.simplifyTokenList(); diff --git a/src/errormessage.h b/src/errormessage.h index c04f5b738..19c2d3af7 100644 --- a/src/errormessage.h +++ b/src/errormessage.h @@ -246,5 +246,32 @@ public: return true; } + static std::string unusedVariable(const Tokenizer *tokenizer, const Token *Location, const std::string &varname) + { + return msg1(tokenizer, Location) + "Unused variable '" + varname + "'"; + } + static bool unusedVariable(const Settings &s) + { + return s._checkCodingStyle; + } + + static std::string unreadVariable(const Tokenizer *tokenizer, const Token *Location, const std::string &varname) + { + return msg1(tokenizer, Location) + "Variable '" + varname + "' is assigned a value that is never used"; + } + static bool unreadVariable(const Settings &s) + { + return s._checkCodingStyle; + } + + static std::string unassignedVariable(const Tokenizer *tokenizer, const Token *Location, const std::string &varname) + { + return msg1(tokenizer, Location) + "Variable '" + varname + "' is not assigned a value"; + } + static bool unassignedVariable(const Settings &s) + { + return s._checkCodingStyle; + } + }; #endif diff --git a/tools/errmsg.cpp b/tools/errmsg.cpp index 394d0db4c..008af9ec3 100644 --- a/tools/errmsg.cpp +++ b/tools/errmsg.cpp @@ -86,6 +86,9 @@ int main() 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'")); err.push_back(Message("passedByValue", 0, "Function parameter '%1' is passed by value. It could be passed by reference instead.", "parname")); + 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")); // Generate code.. std::cout << "Generate code.." << std::endl;