[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)

Gaurav Dhingra via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 16 03:06:02 PDT 2026


================
@@ -64,13 +64,20 @@ static bool areBinaryOperatorOperandsTypesEqualToOperatorResultType(
 
     const QualType NonReferenceType = Type.getNonReferenceType();
     const QualType LHSType = B->getLHS()->IgnoreImplicit()->getType();
-    if (LHSType.isNull() || !areTypesEqual(LHSType.getNonReferenceType(),
-                                           NonReferenceType, IgnoreTypeAliases))
-      return false;
     const QualType RHSType = B->getRHS()->IgnoreImplicit()->getType();
-    if (RHSType.isNull() || !areTypesEqual(RHSType.getNonReferenceType(),
-                                           NonReferenceType, IgnoreTypeAliases))
+    const bool LHSMatches =
+        !LHSType.isNull() && areTypesEqual(LHSType.getNonReferenceType(),
+                                           NonReferenceType, IgnoreTypeAliases);
+    const bool RHSMatches =
+        !RHSType.isNull() && areTypesEqual(RHSType.getNonReferenceType(),
+                                           NonReferenceType, IgnoreTypeAliases);
+    if (!IgnoreImplicitCasts) {
----------------
gxyd wrote:

I've instead changed it to:
```cpp
    // Explicit Cast is needed if:
    // IgnoreImplicitCasts = false: neither of operands type matches cast type
    // IgnoreImplicitCasts = true: at least one operand type doesn't match cast
    //                             type
    const bool castIsNeeded = IgnoreImplicitCasts ? (!LHSMatches || !RHSMatches)
                                                  : (!LHSMatches && !RHSMatches);
    if (castIsNeeded)
      return false;
```

does that look good? This one felt more readable to me.

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


More information about the cfe-commits mailing list