diff --git a/CheckOther.cpp b/CheckOther.cpp index 944ee0651..ca1a53218 100644 --- a/CheckOther.cpp +++ b/CheckOther.cpp @@ -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()); } diff --git a/testif5/err.msg b/testif5/err.msg new file mode 100644 index 000000000..e69de29bb diff --git a/testif5/testif5.cpp b/testif5/testif5.cpp new file mode 100644 index 000000000..e1d8c62b2 --- /dev/null +++ b/testif5/testif5.cpp @@ -0,0 +1,9 @@ + + +void f() +{ + if ( strcmp(str, "abc") == 0 ) + abc = true; + else + abc = false; +} \ No newline at end of file diff --git a/testif5/warn.msg b/testif5/warn.msg new file mode 100644 index 000000000..178a505a4 --- /dev/null +++ b/testif5/warn.msg @@ -0,0 +1 @@ +[testif5\testif5.cpp:5]: Found "if (condition) var=true; else var=false;", it can be rewritten as "var = (condition);"