Fix #9342 FP oppositeExpression - negated value is not opposite for bitwise logical operators (#3615)

This commit is contained in:
chrchr-github 2021-12-10 18:06:45 +01:00 committed by GitHub
parent e7b6920cf4
commit f64097465f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View File

@ -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;
}

View File

@ -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() {