From c8a5bd16a171a12aa36d8ab3277b3830a72c9a7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 13 Jan 2009 17:56:45 +0000 Subject: [PATCH] errmsg: Added 'assignment in if-condition' --- src/checkfunctionusage.cpp | 1 - src/checkother.cpp | 4 +--- src/cppcheck.cpp | 7 ++----- src/errormessage.h | 9 +++++++++ tools/errmsg.cpp | 31 ++++++++++++++++--------------- 5 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/checkfunctionusage.cpp b/src/checkfunctionusage.cpp index d58758d40..429c4ff6f 100644 --- a/src/checkfunctionusage.cpp +++ b/src/checkfunctionusage.cpp @@ -21,7 +21,6 @@ #include "checkfunctionusage.h" #include "errormessage.h" #include "tokenize.h" -#include //--------------------------------------------------------------------------- diff --git a/src/checkother.cpp b/src/checkother.cpp index b98158438..768efcebb 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -377,9 +377,7 @@ void CheckOther::CheckIfAssignment() Token::Match(tok, "if ( %var% = %str% )") || Token::Match(tok, "if ( %var% = %var% )")) { - std::ostringstream ostr; - ostr << _tokenizer->fileLine(tok) << ": Possible bug. Should it be '==' instead of '='?"; - _errorLogger->reportErr(ostr.str()); + _errorLogger->reportErr(ErrorMessage::ifAssignment(_tokenizer, tok)); } } } diff --git a/src/cppcheck.cpp b/src/cppcheck.cpp index ffd447265..153f46866 100644 --- a/src/cppcheck.cpp +++ b/src/cppcheck.cpp @@ -280,12 +280,9 @@ void CppCheck::checkFile(const std::string &code, const char FileName[]) if (ErrorMessage::arrayIndexOutOfBounds(_settings) && ErrorMessage::bufferOverrun(_settings)) checkBufferOverrun.bufferOverrun(); - if (_settings._showAll) - { - - // Check for "if (a=b)" + // Check for "if (a=b)" + if (ErrorMessage::ifAssignment(_settings)) checkOther.CheckIfAssignment(); - } // Dangerous functions, such as 'gets' and 'scanf' checkBufferOverrun.dangerousFunctions(); diff --git a/src/errormessage.h b/src/errormessage.h index 157003d82..b6295bd29 100644 --- a/src/errormessage.h +++ b/src/errormessage.h @@ -309,5 +309,14 @@ public: return false; } + static std::string ifAssignment(const Tokenizer *tokenizer, const Token *Location) + { + return msg1(tokenizer, Location) + "Assignment in if-condition"; + } + static bool ifAssignment(const Settings &s) + { + return s._checkCodingStyle; + } + }; #endif diff --git a/tools/errmsg.cpp b/tools/errmsg.cpp index c5d8d11c0..2f2335eb0 100644 --- a/tools/errmsg.cpp +++ b/tools/errmsg.cpp @@ -92,6 +92,7 @@ int main() err.push_back(Message("charArrayIndex", Message::style, "Warning - using char variable as array index")); err.push_back(Message("charBitOp", Message::style, "Warning - using char variable in bit operation")); err.push_back(Message("variableScope", Message::never, "The scope of the variable %1 can be limited", "varname")); + err.push_back(Message("ifAssignment", Message::style, "Assignment in if-condition")); // Generate code.. std::cout << "Generate code.." << std::endl; @@ -224,21 +225,21 @@ void Message::generateCode(std::ostream &ostr) const ostr << " return "; switch (_settings) { - case std: - ostr << "true"; - break; - case all: - ostr << "s._showAll"; - break; - case style: - ostr << "s._checkCodingStyle"; - break; - case style_all: - ostr << "s._showAll & s._checkCodingStyle"; - break; - case never: - ostr << "false"; - break; + case std: + ostr << "true"; + break; + case all: + ostr << "s._showAll"; + break; + case style: + ostr << "s._checkCodingStyle"; + break; + case style_all: + ostr << "s._showAll & s._checkCodingStyle"; + break; + case never: + ostr << "false"; + break; } ostr << ";\n"; ostr << " }\n\n";