diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 3200c61a2..4e2783b4f 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1555,9 +1555,9 @@ bool isOppositeExpression(bool cpp, const Token * const tok1, const Token * cons return false; if (isOppositeCond(true, cpp, tok1, tok2, library, pure, followVar, errors)) return true; - if (tok1->isUnaryOp("-")) + if (tok1->isUnaryOp("-") && !(tok2->astParent() && tok2->astParent()->tokType() == Token::eBitOp)) return isSameExpression(cpp, true, tok1->astOperand1(), tok2, library, pure, followVar, errors); - if (tok2->isUnaryOp("-")) + if (tok2->isUnaryOp("-") && !(tok2->astParent() && tok2->astParent()->tokType() == Token::eBitOp)) return isSameExpression(cpp, true, tok2->astOperand1(), tok1, library, pure, followVar, errors); return false; } diff --git a/test/testother.cpp b/test/testother.cpp index 2134696b2..2b2380ff2 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -5773,6 +5773,15 @@ private: " if( *a == !(b) ) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Opposite expression on both sides of '=='.\n", errout.str()); + + check("void f(uint16_t u) {\n" // #9342 + " if (u != (u & -u))\n" + " return false;\n" + " if (u != (-u & u))\n" + " return false;\n" + " return true;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void duplicateVarExpression() {