errmsg: Added 'assignment in if-condition'

This commit is contained in:
Daniel Marjamäki 2009-01-13 17:56:45 +00:00
parent aa592387e6
commit c8a5bd16a1
5 changed files with 28 additions and 24 deletions

View File

@ -21,7 +21,6 @@
#include "checkfunctionusage.h"
#include "errormessage.h"
#include "tokenize.h"
#include <sstream>
//---------------------------------------------------------------------------

View File

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

View File

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

View File

@ -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

View File

@ -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";