https://github.com/LUX-ETVERITAS created https://github.com/llvm/llvm-project/pull/246921
When `d` is NaN, both `d >= 5.` and `d < 5.` are false. Keep the explicit negation when applying De Morgan's theorem to floating-point comparisons. Fixes #243399. >From 73e9ec45f6658e614a498443bd1ae80e982809b9 Mon Sep 17 00:00:00 2001 From: zibuyu <[email protected]> Date: Sun, 29 Nov 2026 11:49:43 +0800 Subject: [PATCH] [clang-tidy] Preserve NaN semantics in De Morgan fixes Keep the negation of a floating-point relational comparison explicit. Flipping the operator changes the result when either operand is NaN. Assisted-by: Codex --- .../readability/SimplifyBooleanExprCheck.cpp | 24 ++++++++++--------- clang-tools-extra/docs/ReleaseNotes.md | 3 +++ .../simplify-boolean-expr-demorgan.cpp | 12 ++++++++++ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp index 7b9b60b84b0f89..91c0ed57ddad07 100644 --- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp @@ -231,6 +231,10 @@ static std::string replacementExpression(const ASTContext &Context, return asBool(getText(Context, *E), NeedsStaticCast); } +static bool isFloatingPointRelationalOp(const BinaryOperator *Op) { + return Op->isRelationalOp() && Op->getLHS()->getType()->isRealFloatingType(); +} + static bool containsDiscardedTokens(const ASTContext &Context, CharSourceRange CharRange) { std::string ReplacementText = @@ -548,7 +552,7 @@ class SimplifyBooleanExprCheck::Visitor : public RecursiveASTVisitor<Visitor> { case BO_GE: case BO_EQ: case BO_NE: - return true; + return !isFloatingPointRelationalOp(BO); case BO_LAnd: case BO_LOr: return checkEitherSide( @@ -904,17 +908,15 @@ flipDemorganBinaryOperator(SmallVectorImpl<FixItHint> &Fixes, case BO_GE: case BO_EQ: case BO_NE: - // For comparison operators, just negate the comparison. - if (BinOp->getOperatorLoc().isMacroID()) - return true; - Fixes.push_back(FixItHint::CreateReplacement( - BinOp->getOperatorLoc(), - BinaryOperator::getOpcodeStr( - BinaryOperator::negateComparisonOp(BinOp->getOpcode())))); - return false; + if (!isFloatingPointRelationalOp(BinOp)) { + if (BinOp->getOperatorLoc().isMacroID()) + return true; + Fixes.push_back(FixItHint::CreateReplacement(BinOp->getOperatorLoc(), + negatedOperator(BinOp))); + return false; + } + [[fallthrough]]; default: - // for any other binary operator, just use logical not and wrap in - // parens. if (Parens) { if (Parens->getBeginLoc().isMacroID()) return true; diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md index 15c76ccab6b0d7..a4d38aac705abf 100644 --- a/clang-tools-extra/docs/ReleaseNotes.md +++ b/clang-tools-extra/docs/ReleaseNotes.md @@ -488,6 +488,9 @@ infrastructure are described first, followed by tool-specific sections. - Fixed invalid fixes when applying De Morgan's theorem to overloaded comparison operators. + - Preserved the result for NaN when applying De Morgan's theorem to + floating-point comparisons. + - No longer offers fixes that remove a left operand with side effects, such as `f()` in `f() || true`. diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr-demorgan.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr-demorgan.cpp index e194bb4eb1745c..8828d07ded1b94 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr-demorgan.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr-demorgan.cpp @@ -163,3 +163,15 @@ struct Entry { // CHECK-FIXES-NEXT: units < rhs.units || entry_type >= rhs.entry_type; }; } // namespace overloaded_comparisons + +bool outside(double d) { + return !(d >= 5. && d <= 7.); + // CHECK-MESSAGES-RELAXED: :[[@LINE-1]]:10: warning: boolean expression can be simplified + // CHECK-FIXES-RELAXED: return !(d >= 5.) || !(d <= 7.); +} + +bool mixed(int i, double d, bool flag) { + return !(i < d && !flag); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: boolean expression can be simplified + // CHECK-FIXES: return !(i < d) || flag; +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
