diff --git a/CheckOther.cpp b/CheckOther.cpp index 03e5b3503..5225a6952 100644 --- a/CheckOther.cpp +++ b/CheckOther.cpp @@ -242,6 +242,47 @@ void WarningIf() } ReportErr(ostr.str()); } + + // Search for 'if (condition) flag = true;' + for (TOKEN *tok = tokens; tok; tok = tok->next) + { + if (strcmp(tok->str,"if")) + continue; + + int parlevel = 0; + for (TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next) + { + if (tok2->str[0]=='(') + parlevel++; + else if (tok2->str[0]==')') + { + parlevel--; + if (parlevel<=0) + { + // Don't handle "else" now + if ( strcmp(getstr(tok2,5), "else") == 0 ) + break; + + if ( match(tok2->next, "var = true ;") ) + { + std::ostringstream ostr; + ostr << FileLine(tok) << ": Found \"if (condition) var = true;\", it can be rewritten as \"var |= (condition);\""; + ReportErr(ostr.str()); + } + + 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);\""; + ReportErr(ostr.str()); + } + + break; + } + } + } + } + } //--------------------------------------------------------------------------- @@ -392,6 +433,7 @@ void WarningStrTok() std::ostringstream ostr; ostr << FileLine(tok) << ": Possible bug. Both '" << (*it1)->str << "' and '" << (*it2)->str << "' uses strtok."; ReportErr(ostr.str()); + break; } } } diff --git a/testfunc6/err.msg b/testfunc6/err.msg new file mode 100644 index 000000000..e2d8204f9 --- /dev/null +++ b/testfunc6/err.msg @@ -0,0 +1 @@ +[testfunc6\testfunc6.cpp:6]: Found "if (condition) var = true;", it can be rewritten as "var |= (condition);" diff --git a/testfunc6/testfunc6.cpp b/testfunc6/testfunc6.cpp new file mode 100644 index 000000000..9e5c65a35 --- /dev/null +++ b/testfunc6/testfunc6.cpp @@ -0,0 +1,8 @@ + + + +void f() +{ + if (condition) + flag = true; +} \ No newline at end of file