diff --git a/lib/checkassignif.cpp b/lib/checkassignif.cpp index e34d2dc03..fc8a3a987 100644 --- a/lib/checkassignif.cpp +++ b/lib/checkassignif.cpp @@ -138,7 +138,7 @@ void CheckAssignIf::comparisonError(const Token *tok, bool result) -void CheckAssignIf::multicompare() +void CheckAssignIf::multiCondition() { if (!_settings->_checkCodingStyle) return; @@ -167,7 +167,7 @@ void CheckAssignIf::multicompare() tok2 = tok2->next()->link(); // check condition.. - if (Token::Match(opar, "( %varid% == %num% &&|%oror%|)", varid)) + if (Token::Match(opar, "( %varid% ==|& %num% &&|%oror%|)", varid)) { const MathLib::bigint num2 = MathLib::toLongNumber(opar->strAt(3)); if (num2 < 0) @@ -175,7 +175,7 @@ void CheckAssignIf::multicompare() if ((num1 & num2) == num2) { - multicompareError(opar, tok->linenr()); + multiConditionError(opar, tok->linenr()); } } } @@ -183,12 +183,11 @@ void CheckAssignIf::multicompare() } } -void CheckAssignIf::multicompareError(const Token *tok, unsigned int line1) +void CheckAssignIf::multiConditionError(const Token *tok, unsigned int line1) { std::ostringstream errmsg; - errmsg << "Comparison is always false because otherwise the condition at line " - << line1 - << " is not false"; + errmsg << "'else if' condition matches previous condition at line " + << line1; - reportError(tok, Severity::style, "multicompare", errmsg.str()); + reportError(tok, Severity::information, "multiCondition", errmsg.str()); } diff --git a/lib/checkassignif.h b/lib/checkassignif.h index 3b7d47c9c..ee6f6e801 100644 --- a/lib/checkassignif.h +++ b/lib/checkassignif.h @@ -52,7 +52,7 @@ public: CheckAssignIf checkAssignIf(tokenizer, settings, errorLogger); checkAssignIf.assignIf(); checkAssignIf.comparison(); - checkAssignIf.multicompare(); + checkAssignIf.multiCondition(); } /** mismatching assignment / comparison */ @@ -62,20 +62,20 @@ public: void comparison(); /** match 'if' and 'else if' conditions */ - void multicompare(); + void multiCondition(); private: void assignIfError(const Token *tok, bool result); void comparisonError(const Token *tok, bool result); - void multicompareError(const Token *tok, unsigned int line1); + void multiConditionError(const Token *tok, unsigned int line1); void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) { CheckAssignIf c(0, settings, errorLogger); c.assignIfError(0, false); c.comparisonError(0, false); - c.multicompareError(0,1); + c.multiConditionError(0,1); } std::string myName() const @@ -88,7 +88,7 @@ private: return "Match assignments and conditions:\n" " * Mismatching assignment and comparison => comparison is always true/false\n" " * Mismatching lhs and rhs in comparison => comparison is always true/false\n" - " * Match 'if' and 'else if' conditions"; + " * Detect matching 'if' and 'else if' conditions"; } }; /// @} diff --git a/test/testassignif.cpp b/test/testassignif.cpp index 28810b187..176ca1036 100644 --- a/test/testassignif.cpp +++ b/test/testassignif.cpp @@ -59,7 +59,7 @@ private: CheckAssignIf checkAssignIf(&tokenizer, &settings, this); checkAssignIf.assignIf(); checkAssignIf.comparison(); - checkAssignIf.multicompare(); + checkAssignIf.multiCondition(); } void assignAndCompare() @@ -107,8 +107,14 @@ private: " if (x & 7);\n" " else if (x == 1);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (style) Comparison is always false because otherwise the condition at line 3 is not false\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (information) 'else if' condition matches previous condition at line 3\n", errout.str()); + check("void foo(int x)\n" + "{\n" + " if (x & 7);\n" + " else if (x & 1);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (information) 'else if' condition matches previous condition at line 3\n", errout.str()); } };