diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 254b4dd9c..862dc1036 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -51,6 +51,9 @@ static bool astIsFloat(const Token *tok, bool unknown) return Token::findmatch(tok->variable()->typeStartToken(), "float|double", tok->variable()->typeEndToken()->next(), 0) != nullptr; } + if (tok->isOp()) + return false; + return unknown; } @@ -133,11 +136,21 @@ bool isSameExpression(const Token *tok1, const Token *tok2, const std::setstr() == "{") return false; - if (!isSameExpression(tok1->astOperand1(), tok2->astOperand1(), constFunctions)) - return false; - if (!isSameExpression(tok1->astOperand2(), tok2->astOperand2(), constFunctions)) - return false; - return true; + bool noncommuative_equals = + isSameExpression(tok1->astOperand1(), tok2->astOperand1(), constFunctions); + noncommuative_equals = noncommuative_equals && + isSameExpression(tok1->astOperand2(), tok2->astOperand2(), constFunctions); + + if (noncommuative_equals) + return true; + + bool commutative = tok1->astOperand1() && tok1->astOperand2() && Token::Match(tok1, "+|*|%or%|%oror%|&|&&|^|==|!="); + bool commuative_equals = commutative && + isSameExpression(tok1->astOperand2(), tok2->astOperand1(), constFunctions); + commuative_equals = commuative_equals && + isSameExpression(tok1->astOperand1(), tok2->astOperand2(), constFunctions); + + return commuative_equals; } static bool isOppositeCond(const Token * const cond1, const Token * const cond2, const std::set &constFunctions) diff --git a/test/testother.cpp b/test/testother.cpp index 35e724bd8..56d24ec16 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4817,6 +4817,11 @@ private: "}"); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&'.\n", errout.str()); + check("void foo(int a, int b) {\n" + " if ((a | b) == (a | b)) {}\n" + "}"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '=='.\n", errout.str()); + check("void foo() {\n" " if (a1[a2[c & 0xff] & 0xff]) {}\n" "}"); @@ -4892,6 +4897,22 @@ private: " bool b = bar.isSet() && bar.isSet();\n" "}"); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:3]: (style) Same expression on both sides of '&&'.\n", errout.str()); + + + check("void foo() {\n" + " if ((b + a) | (a + b)) {}\n" + "}"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '|'.\n", errout.str()); + + check("void foo() {\n" + " if ((b > a) | (a > b)) {}\n" // > is not commutative + "}"); + ASSERT_EQUALS("", errout.str()); + + check("void foo() {\n" + " if ((b + a) > (a + b)) {}\n" + "}"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '>'.\n", errout.str()); } void duplicateExpression2() { // check if float is NaN or Inf