[clang] [analyzer] tryRearrange: don't "cancel out" symbol in BO_Add (PR #237160)

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 30 08:04:03 PDT 2026


================
@@ -346,7 +346,7 @@ static std::optional<NonLoc> tryRearrange(ProgramStateRef State,
   if (BinaryOperator::isComparisonOp(Op) && !InBounds(LInt, RInt, LSym, RSym))
     return std::nullopt;
 
-  if (LSym == RSym)
+  if (LSym == RSym && Op != BO_Add)
----------------
NagyDonat wrote:

`tryRearrange` only handles expressions where the operator `Op` is additive (`BO_Add`, `BO_Sub`) or a comparison (`==`, `!=`, `>`, `<`, `>=`, `<=`). [1] This is guaranteed by a clear early return statement a few dozen lines above this change.

 Cancelling out is valid for all of these operator except for `BO_Add`.
 
 [1] Technically `BinaryOperator::isComparisonOperator` returns true for the spaceship operator `BO_Cmp` as well, but the analyze very quickly filters out that operator (IIRC returning `Unknown`), so it never reaches `tryRearrange`.
 
 I'll probably add an early return (or assertion failure?) for `BO_Cmp` to ensure correct behavior even if it will become somewhat supported in the future, but this belongs to a separate PR.

https://github.com/llvm/llvm-project/pull/237160


More information about the cfe-commits mailing list