[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 10 03:52:40 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tools-extra
Author: Gaurav Dhingra (gxyd)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/191386.diff
2 Files Affected:
- (modified) clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp (+10-7)
- (modified) clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp (+16)
``````````diff
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');
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/191386
More information about the cfe-commits
mailing list