CheckOther: Added check. 'if (condition) var=true; else var=false;' => 'var = (condition);'

This commit is contained in:
Daniel Marjamäki 2007-06-08 18:19:15 +00:00
parent b3a3097b66
commit 5b83f5d0cd
4 changed files with 20 additions and 4 deletions

View File

@ -259,11 +259,17 @@ void WarningIf()
parlevel--;
if (parlevel<=0)
{
// Don't handle "else" now
if ( strcmp(getstr(tok2,5), "else") == 0 )
break;
{
if ( match(tok2->next, "var = true ; else var = false ;") )
{
std::ostringstream ostr;
ostr << FileLine(tok) << ": Found \"if (condition) var=true; else var=false;\", it can be rewritten as \"var = (condition);\"";
ReportErr(ostr.str());
}
}
if ( match(tok2->next, "var = true ;") )
else if ( match(tok2->next, "var = true ;") )
{
std::ostringstream ostr;
ostr << FileLine(tok) << ": Found \"if (condition) var = true;\", it can be rewritten as \"var |= (condition);\"";
@ -273,7 +279,7 @@ void WarningIf()
else if ( match(tok2->next, "var = false ;") )
{
std::ostringstream ostr;
ostr << FileLine(tok) << ": Found \"if (condition) var = false;\", it can be rewritten as \"var &= (condition);\"";
ostr << FileLine(tok) << ": Found \"if (condition) var = false;\", it can be rewritten as \"var &= (!condition);\"";
ReportErr(ostr.str());
}

0
testif5/err.msg Normal file
View File

9
testif5/testif5.cpp Normal file
View File

@ -0,0 +1,9 @@
void f()
{
if ( strcmp(str, "abc") == 0 )
abc = true;
else
abc = false;
}

1
testif5/warn.msg Normal file
View File

@ -0,0 +1 @@
[testif5\testif5.cpp:5]: Found "if (condition) var=true; else var=false;", it can be rewritten as "var = (condition);"