[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
Fri Apr 10 03:51:50 PDT 2026
https://github.com/gxyd created https://github.com/llvm/llvm-project/pull/191386
Mark explicit casting as readabily redundant for a BinaryOperation with atleast one operand of the same type as the cast type in `RedundantCastingCheck`
E.g.; `static<float>(1 + 2.0f)` // redundant cast
Fixes #182132
>From 7d7cfaec6c4fa6897baa8e7f32a78dc33910fc6b Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra <gauravdhingra.gxyd at gmail.com>
Date: Fri, 10 Apr 2026 16:14:58 +0530
Subject: [PATCH] [clang-tidy] Improve redundant-casting check for binary
operation
Mark explicit casting as readabily redundant for a
BinaryOperation with atleast one operand of the same
type as the cast type in `RedundantCastingCheck`
E.g.; `static<float>(1 + 2.0f)` // redundant cast
Fixes #182132
---
.../readability/RedundantCastingCheck.cpp | 17 ++++++++++-------
.../checkers/readability/redundant-casting.cpp | 16 ++++++++++++++++
2 files changed, 26 insertions(+), 7 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp
index eae0b3f6d0e7d..9dc4cef139125 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp
@@ -50,7 +50,7 @@ static bool areTypesEqual(QualType TypeS, QualType TypeD,
TypeD.getLocalUnqualifiedType());
}
-static bool areBinaryOperatorOperandsTypesEqualToOperatorResultType(
+static bool anyBinaryOperatorOperandsTypesEqualToOperatorResultType(
const Expr *E, bool IgnoreTypeAliases) {
if (!E)
return true;
@@ -64,12 +64,15 @@ 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 (!LHSMatches && !RHSMatches)
+ // neither of the operand matches => casting is needed for readability
return false;
}
return true;
@@ -145,7 +148,7 @@ void RedundantCastingCheck::check(const MatchFinder::MatchResult &Result) {
if (!areTypesEqual(TypeS, TypeD, IgnoreTypeAliases))
return;
- if (!areBinaryOperatorOperandsTypesEqualToOperatorResultType(
+ if (!anyBinaryOperatorOperandsTypesEqualToOperatorResultType(
SourceExpr, IgnoreTypeAliases))
return;
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp
index 001057aeaa495..8a0f271d55e29 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp
@@ -14,6 +14,8 @@
// RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreTypeAliases: true }}' \
// RUN: -- -fno-delayed-template-parsing -D CXX_20=1
+#include <cstdint>
+
struct A {};
struct B : A {};
A getA();
@@ -168,6 +170,20 @@ void testFunctionalCastWithInitExpr(unsigned a) {
unsigned c = unsigned{0};
}
+void testBinaryOperatorRedundantCasting() {
+ const auto diff_types_operands1 { static_cast<float>(1.0f + 1) };
+ // CHECK-MESSAGES: :[[@LINE-1]]:37: warning: redundant explicit casting to the same type 'float' as the sub-expression, remove this casting [readability-redundant-casting]
+ // CHECK-FIXES: const auto diff_types_operands1 { (1.0f + 1) };
+
+ const auto diff_types_operands2 { static_cast<float>(2 + 3.0f) };
+ // CHECK-MESSAGES: :[[@LINE-1]]:37: warning: redundant explicit casting to the same type 'float' as the sub-expression, remove this casting [readability-redundant-casting]
+ // CHECK-FIXES: const auto diff_types_operands2 { (2 + 3.0f) };
+
+ const auto diff_types_operands3 { static_cast<int>(1 + static_cast<uint8_t>(1)) };
+ // CHECK-MESSAGES: :[[@LINE-1]]:37: warning: redundant explicit casting to the same type 'int' as the sub-expression, remove this casting [readability-redundant-casting]
+ // CHECK-FIXES: const auto diff_types_operands3 { (1 + static_cast<uint8_t>(1)) };
+}
+
void testBinaryOperator(char c) {
int a = int(c - 'C');
}
More information about the cfe-commits
mailing list