From 218650dc85274532f97ab0a96aeb0724b4e01c91 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 7 Jul 2023 23:30:59 +0200 Subject: [PATCH] Fix #11820 FP duplicateExpression with double negation (#5227) --- lib/astutils.cpp | 4 ++-- test/testother.cpp | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 268b9535b..7dea9a460 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1458,10 +1458,10 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2 tok2 = tok2->astOperand2(); } // Skip double not - if (Token::simpleMatch(tok1, "!") && Token::simpleMatch(tok1->astOperand1(), "!") && !Token::simpleMatch(tok1->astParent(), "=")) { + if (Token::simpleMatch(tok1, "!") && Token::simpleMatch(tok1->astOperand1(), "!") && !Token::simpleMatch(tok1->astParent(), "=") && astIsBoolLike(tok2)) { return isSameExpression(cpp, macro, tok1->astOperand1()->astOperand1(), tok2, library, pure, followVar, errors); } - if (Token::simpleMatch(tok2, "!") && Token::simpleMatch(tok2->astOperand1(), "!") && !Token::simpleMatch(tok2->astParent(), "=")) { + if (Token::simpleMatch(tok2, "!") && Token::simpleMatch(tok2->astOperand1(), "!") && !Token::simpleMatch(tok2->astParent(), "=") && astIsBoolLike(tok1)) { return isSameExpression(cpp, macro, tok1, tok2->astOperand1()->astOperand1(), library, pure, followVar, errors); } const bool tok_str_eq = tok1->str() == tok2->str(); diff --git a/test/testother.cpp b/test/testother.cpp index 1c0fb8b9f..91d1beef5 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -6570,6 +6570,15 @@ private: " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (style) The comparison 'p == 0' is always true.\n", errout.str()); + + // #11820 + check("unsigned f(unsigned x) {\n" + " return x - !!x;\n" + "}\n" + "unsigned g(unsigned x) {\n" + " return !!x - x;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void duplicateExpression8() {