[clang-tools-extra] 5e9b88c - [clang-tidy] Keep side effects in readability-simplify-boolean-expr (#243303)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 16 21:13:18 PST 2026
Author: David Meng
Date: 2026-11-17T13:13:13+08:00
New Revision: 5e9b88cabc5e336ea59951d0cfa0d9df232c7637
URL: https://github.com/llvm/llvm-project/commit/5e9b88cabc5e336ea59951d0cfa0d9df232c7637
DIFF: https://github.com/llvm/llvm-project/commit/5e9b88cabc5e336ea59951d0cfa0d9df232c7637.diff
LOG: [clang-tidy] Keep side effects in readability-simplify-boolean-expr (#243303)
`f() || true` and `f() && false` were rewritten to the literal value,
which removed the call to `f()`. The check still warns in these cases
but if the left operand may have side effects it now doesn't give a fix.
Fixes #242838.
Added:
Modified:
clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
clang-tools-extra/docs/ReleaseNotes.md
clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
index 121c327dc3b583..7b9b60b84b0f89 100644
--- a/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
@@ -659,6 +659,22 @@ void SimplifyBooleanExprCheck::reportBinOp(const ASTContext &Context,
Replacement);
};
+ // `f() && false` and `f() || true` always call `f()`, so don't replace them
+ // with the literal if the left operand may have side effects.
+ const auto ReplaceWithBool = [this, &Context, Op, Bool,
+ &ReplaceWithExpression]() {
+ const Expr *Left = Op->getLHS();
+ if (!Left->HasSideEffects(Context)) {
+ ReplaceWithExpression(Bool, /*Negated=*/false);
+ return;
+ }
+ diag(Bool->getBeginLoc(), SimplifyOperatorDiagnostic);
+ diag(Left->getBeginLoc(),
+ "left operand may have side effects and cannot be removed",
+ DiagnosticIDs::Note)
+ << Left->getSourceRange();
+ };
+
switch (Op->getOpcode()) {
case BO_LAnd:
if (BoolValue)
@@ -666,12 +682,12 @@ void SimplifyBooleanExprCheck::reportBinOp(const ASTContext &Context,
ReplaceWithExpression(Other, /*Negated=*/false);
else
// expr && false -> false
- ReplaceWithExpression(Bool, /*Negated=*/false);
+ ReplaceWithBool();
break;
case BO_LOr:
if (BoolValue)
// expr || true -> true
- ReplaceWithExpression(Bool, /*Negated=*/false);
+ ReplaceWithBool();
else
// expr || false -> expr
ReplaceWithExpression(Other, /*Negated=*/false);
diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md
index bcada36e7259bb..2566c24544d706 100644
--- a/clang-tools-extra/docs/ReleaseNotes.md
+++ b/clang-tools-extra/docs/ReleaseNotes.md
@@ -457,9 +457,14 @@ infrastructure are described first, followed by tool-specific sections.
`decltype((x))` and in the initializers and return statements of
`decltype(auto)` declarations.
-- Fixed {doc}`readability-simplify-boolean-expr
- <clang-tidy/checks/readability/simplify-boolean-expr>` producing invalid
- fixes when applying De Morgan's theorem to overloaded comparison operators.
+- Improved {doc}`readability-simplify-boolean-expr
+ <clang-tidy/checks/readability/simplify-boolean-expr>` check:
+
+ - Fixed invalid fixes when applying De Morgan's theorem to overloaded
+ comparison operators.
+
+ - No longer offers fixes that remove a left operand with side effects, such
+ as `f()` in `f() || true`.
- Improved {doc}`readability-suspicious-call-argument
<clang-tidy/checks/readability/suspicious-call-argument>` check by fixing the
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr.cpp
index 0b99cb89262cdf..68605c648e2b48 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr.cpp
@@ -246,6 +246,30 @@ void operator_and() {
}
}
+bool sideEffect();
+__attribute__((pure)) bool noSideEffect();
+
+void side_effects(volatile bool v) {
+ bool r1 = sideEffect() || true;
+ // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: {{.*}} to boolean operator
+ // CHECK-MESSAGES: :[[@LINE-2]]:13: note: left operand may have side effects and cannot be removed
+ // CHECK-FIXES: bool r1 = sideEffect() || true;
+ bool r2 = sideEffect() && false;
+ // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: {{.*}} to boolean operator
+ // CHECK-MESSAGES: :[[@LINE-2]]:13: note: left operand may have side effects and cannot be removed
+ // CHECK-FIXES: bool r2 = sideEffect() && false;
+ bool r3 = v || true;
+ // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: {{.*}} to boolean operator
+ // CHECK-MESSAGES: :[[@LINE-2]]:13: note: left operand may have side effects and cannot be removed
+ // CHECK-FIXES: bool r3 = v || true;
+ bool r4 = true || sideEffect();
+ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: {{.*}} to boolean operator
+ // CHECK-FIXES: bool r4 = true;
+ bool r5 = noSideEffect() || true;
+ // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: {{.*}} to boolean operator
+ // CHECK-FIXES: bool r5 = true;
+}
+
void ternary_operator() {
int i = 0;
bool bd = (i > 20) ? true : false;
More information about the cfe-commits
mailing list