[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
luporl wrote: It looks like this broke some Arm (32-bit) buildbots: - https://lab.llvm.org/buildbot/#/builders/135/builds/3657 - https://lab.llvm.org/buildbot/#/builders/79/builds/3253 Can you please take a look at it? I can help with Arm testing and provide additional error details if needed. https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
vbvictor wrote:
The correct diagnostic would could be (I didn't check proper regex):
"warning: redundant explicit casting to the same type 'size_t' (aka
'{{unsigned}}') as the sub-expression, remove this casting
[readability-redundant-casting]"
Some other tests may need correction as well, I checked only one
https://github.com/llvm/llvm-project/pull/191386
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
vbvictor wrote:
On line 199 in our tests we expect:
"warning: redundant explicit casting to the same type 'size_t' (aka 'unsigned
long{{( long)?}}') as the sub-expression, remove this casting
[readability-redundant-casting]"
But on arm buildbot it is:
"warning: redundant explicit casting to the same type 'size_t' (aka 'unsigned
int') as the sub-expression, remove this casting
[readability-redundant-casting]"
I think we should omit matching full "aka" part here or just look for `unsigned`
https://github.com/llvm/llvm-project/pull/191386
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
github-actions[bot] wrote: @gxyd Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our [build bots](https://lab.llvm.org/buildbot/). If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail [here](https://llvm.org/docs/MyFirstTypoFix.html#myfirsttypofix-issues-after-landing-your-pr). If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of [LLVM development](https://llvm.org/docs/DeveloperPolicy.html#patch-reversion-policy). You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
https://github.com/zeyi2 closed https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
https://github.com/zeyi2 approved this pull request. https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
@@ -518,6 +518,17 @@ Changes in existing checks - Fixed a false positive in array subscript expressions where the types are not yet resolved. +- Improved :doc:`readability-redundant-casting + ` check + + - Fixed a false negative where casts were not flagged as redundant when zeyi2 wrote: Also this PR LGTM after fixing it :) https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
https://github.com/zeyi2 edited https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
@@ -518,6 +518,17 @@ Changes in existing checks - Fixed a false positive in array subscript expressions where the types are not yet resolved. +- Improved :doc:`readability-redundant-casting + ` check + + - Fixed a false negative where casts were not flagged as redundant when zeyi2 wrote: Sorry for the push back, IMO we should just have one entry saying: `... check by adding `IgnoreImplictCasts` option to control .` WDYT? https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
https://github.com/gxyd updated https://github.com/llvm/llvm-project/pull/191386
>From f9ad8da3a37ba112600503e6bfe179b2948449a8 Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra
Date: Fri, 10 Apr 2026 16:14:58 +0530
Subject: [PATCH 01/13] [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(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
+
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(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(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(1 +
static_cast(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(1)) };
+}
+
void testBinaryOperator(char c) {
int a = int(c - 'C');
}
>From 92011b394b551f53df2997d48f1a76f152ab791a Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra
Date: Sat, 11 Apr 2026 12:47:51 +0530
Subject: [PATCH 02/13] add ReleaseNotes entry for the fixed issue
---
clang-tools-extra/docs/ReleaseNotes.rst | 6 ++
1 file change
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
https://github.com/gxyd updated https://github.com/llvm/llvm-project/pull/191386
>From f9ad8da3a37ba112600503e6bfe179b2948449a8 Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra
Date: Fri, 10 Apr 2026 16:14:58 +0530
Subject: [PATCH 01/12] [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(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
+
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(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(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(1 +
static_cast(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(1)) };
+}
+
void testBinaryOperator(char c) {
int a = int(c - 'C');
}
>From 92011b394b551f53df2997d48f1a76f152ab791a Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra
Date: Sat, 11 Apr 2026 12:47:51 +0530
Subject: [PATCH 02/12] add ReleaseNotes entry for the fixed issue
---
clang-tools-extra/docs/ReleaseNotes.rst | 6 ++
1 file change
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
github-actions[bot] wrote: :warning: C/C++ code formatter, clang-format found issues in your code. :warning: You can test this locally with the following command: ``bash git-clang-format --diff origin/main HEAD --extensions h,cpp -- clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.h clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp --diff_from_common_commit `` :warning: The reproduction instructions above might return results for more than one PR in a stack if you are using a stacked PR workflow. You can limit the results by changing `origin/main` to the base branch/commit you want to compare against. :warning: View the diff from clang-format here. ``diff diff --git a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp index 8cd943ceb..aba6b42a1 100644 --- a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp @@ -75,8 +75,9 @@ static bool binaryOperatorOperandsTypesEqualToOperatorResultType( // 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); +const bool castIsNeeded = IgnoreImplicitCasts + ? (!LHSMatches || !RHSMatches) + : (!LHSMatches && !RHSMatches); if (castIsNeeded) return false; } `` https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
@@ -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) {
zeyi2 wrote:
Thanks!
https://github.com/llvm/llvm-project/pull/191386
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
@@ -27,11 +27,24 @@ Options .. option:: IgnoreMacros - If set to `true`, the check will not give warnings inside macros. Default - is `true`. + If set to `true`, the check will not give warnings inside macros. Default + is `true`. .. option:: IgnoreTypeAliases - When set to `false`, the check will consider type aliases, and when set to - `true`, it will resolve all type aliases and operate on the underlying - types. Default is `false`. + When set to `false`, the check will consider type aliases, and when set to gxyd wrote: > Currently we already mix 2 whitespaces and 3 whitespaces indention in the > documentation. That's true. Until now I had mostly seen 2 whitespaces, but `redundant-qualified-alias.rst` etc. (there are more) use 3 whitespaces as well. https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
gxyd wrote: > Also for the ReleaseNotes, you can update the branch then fix the ordering :) I needed to have the commits from *main* branch for this, so instead of "merging" I did a "rebase" and I also updated the ReleaseNotes entry to reflect the newly added option as well. https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
@@ -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
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
https://github.com/gxyd updated https://github.com/llvm/llvm-project/pull/191386
>From f9ad8da3a37ba112600503e6bfe179b2948449a8 Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra
Date: Fri, 10 Apr 2026 16:14:58 +0530
Subject: [PATCH 01/11] [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(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
+
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(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(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(1 +
static_cast(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(1)) };
+}
+
void testBinaryOperator(char c) {
int a = int(c - 'C');
}
>From 92011b394b551f53df2997d48f1a76f152ab791a Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra
Date: Sat, 11 Apr 2026 12:47:51 +0530
Subject: [PATCH 02/11] add ReleaseNotes entry for the fixed issue
---
clang-tools-extra/docs/ReleaseNotes.rst | 6 ++
1 file change
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
https://github.com/zeyi2 edited https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
zeyi2 wrote: Also for the ReleaseNotes, you can update the branch then fix the ordering :) https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
@@ -27,11 +27,24 @@ Options .. option:: IgnoreMacros - If set to `true`, the check will not give warnings inside macros. Default - is `true`. + If set to `true`, the check will not give warnings inside macros. Default + is `true`. .. option:: IgnoreTypeAliases - When set to `false`, the check will consider type aliases, and when set to - `true`, it will resolve all type aliases and operate on the underlying - types. Default is `false`. + When set to `false`, the check will consider type aliases, and when set to zeyi2 wrote: I'm a bit unsure whether to change the formatting style of these options, Currently we already mix 2 whitespaces and 3 whitespaces indention in the documentation. Though tbh it's just a minor stylistic preference. I’m okay with keeping it as-is. https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
https://github.com/zeyi2 edited https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
@@ -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) {
zeyi2 wrote:
```cpp
if (!LHSMatches || (IgnoreImplicitCasts && !RHSMatches)) {
return false;
}
```
WDYT?
https://github.com/llvm/llvm-project/pull/191386
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
github-actions[bot] wrote: :warning: C/C++ code linter, clang-tidy found issues in your code. :warning: You can test this locally with the following command: ```bash git diff -U0 origin/main...HEAD -- clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.h | python3 clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py -path build -p1 -quiet ``` View the output from clang-tidy here. ``` clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp:53:13: warning: invalid case style for function 'BinaryOperatorOperandsTypesEqualToOperatorResultType' [readability-identifier-naming] 53 | static bool BinaryOperatorOperandsTypesEqualToOperatorResultType( | ^~~~ | binaryOperatorOperandsTypesEqualToOperatorResultType ``` https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
@@ -168,6 +171,33 @@ void testFunctionalCastWithInitExpr(unsigned a) {
unsigned c = unsigned{0};
}
+void testBinaryOperatorRedundantCasting() {
+ const auto diff_types_operands1 { static_cast(1.0f + 1) };
gxyd wrote:
I've just pushed a change for this with making the changes in this PR
accessible via an option. Kindly let me know how it looks.
I've made the changes, so that the user can "opt-out" by specifying
"IgnoreImplicitCasts" as `true`.
https://github.com/llvm/llvm-project/pull/191386
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
@@ -168,6 +171,33 @@ void testFunctionalCastWithInitExpr(unsigned a) {
unsigned c = unsigned{0};
}
+void testBinaryOperatorRedundantCasting() {
+ const auto diff_types_operands1 { static_cast(1.0f + 1) };
vbvictor wrote:
As for options, there are some for this check (like IgnoreTypeAliases).
So all you need is to add new option (look at how others are made) and pass it
to `anyBinaryOperatorOperandsTypesEqualToOperatorResultType`.
Option name could be `IgnoreImplicitTypePromotion` (I just asked AI), will be
false by default. If user make it `true` - they can get previous behavoir.
https://github.com/llvm/llvm-project/pull/191386
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
@@ -168,6 +171,33 @@ void testFunctionalCastWithInitExpr(unsigned a) {
unsigned c = unsigned{0};
}
+void testBinaryOperatorRedundantCasting() {
+ const auto diff_types_operands1 { static_cast(1.0f + 1) };
gxyd wrote:
How do you make something like this an "option"? Could you give me some
pointers for where to look at doing such a thing?
https://github.com/llvm/llvm-project/pull/191386
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
@@ -168,6 +171,33 @@ void testFunctionalCastWithInitExpr(unsigned a) {
unsigned c = unsigned{0};
}
+void testBinaryOperatorRedundantCasting() {
+ const auto diff_types_operands1 { static_cast(1.0f + 1) };
vbvictor wrote:
I have a mixed feeling about such diagnostics.
It could be quite useful if you have:
```cpp
float bigFunctionRetursSomething();
int main() {
// compiler-determined return type; could be complex lambda
auto l = [] () { return bigFunctionRetursSomething(); };
auto l2 =
// a lot of lines
// you don't know exactly what will l and l2 will return (could be
something different than float in near future)
auto a { static_cast(l() + l2()) };
return 0;
}
```
I'm leaning towards this becoming an option instead of hard-coded part.
We are on thin ice with this one. I can imagine a regression report where we
flagged a "valid case" as redundant.
https://github.com/llvm/llvm-project/pull/191386
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
https://github.com/gxyd updated https://github.com/llvm/llvm-project/pull/191386
>From 7d7cfaec6c4fa6897baa8e7f32a78dc33910fc6b Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra
Date: Fri, 10 Apr 2026 16:14:58 +0530
Subject: [PATCH 1/5] [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(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
+
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(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(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(1 +
static_cast(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(1)) };
+}
+
void testBinaryOperator(char c) {
int a = int(c - 'C');
}
>From a1a4ac783a29d71574e02c8f7bd7b0cbc8a5428a Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra
Date: Sat, 11 Apr 2026 12:47:51 +0530
Subject: [PATCH 2/5] add ReleaseNotes entry for the fixed issue
---
clang-tools-extra/docs/ReleaseNotes.rst | 6 ++
1 file changed, 6
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
https://github.com/zeyi2 approved this pull request. https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
https://github.com/gxyd updated https://github.com/llvm/llvm-project/pull/191386
>From 7d7cfaec6c4fa6897baa8e7f32a78dc33910fc6b Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra
Date: Fri, 10 Apr 2026 16:14:58 +0530
Subject: [PATCH 1/6] [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(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
+
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(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(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(1 +
static_cast(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(1)) };
+}
+
void testBinaryOperator(char c) {
int a = int(c - 'C');
}
>From a1a4ac783a29d71574e02c8f7bd7b0cbc8a5428a Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra
Date: Sat, 11 Apr 2026 12:47:51 +0530
Subject: [PATCH 2/6] add ReleaseNotes entry for the fixed issue
---
clang-tools-extra/docs/ReleaseNotes.rst | 6 ++
1 file changed, 6
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
@@ -491,14 +454,16 @@ Changes in existing checks implicit conversions of logical operator results (``&&``, ``||``, ``!``) to ``bool`` in C. -- Improved :doc:`readability-non-const-parameter - ` check: +- Improved :doc:`readability-redundant-casting gxyd wrote: Ah. Apologies, I honestly thought I explicitly already took care of that, my bad, I still made a mistake here. https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
github-actions[bot] wrote:
# :window: Windows x64 Test Results
* 3121 tests passed
* 30 tests skipped
* 2 tests failed
## Failed Tests
(click on a test name to see its output)
### Clang Tools
Clang
Tools.clang-tidy/checkers/readability/redundant-casting.cpp
```
Exit Code: 1
Command Output (stdout):
--
# RUN: at line 1
C:/Python312/python.exe
C:/_work/llvm-project/llvm-project/clang-tools-extra/test/../test\clang-tidy\check_clang_tidy.py
-std=c++11,c++14,c++17
C:\_work\llvm-project\llvm-project\clang-tools-extra\test\clang-tidy\checkers\readability\redundant-casting.cpp
readability-redundant-casting
C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\checkers\readability\Output\redundant-casting.cpp.tmp
-- -- -fno-delayed-template-parsing
# executed command: C:/Python312/python.exe
'C:/_work/llvm-project/llvm-project/clang-tools-extra/test/../test\clang-tidy\check_clang_tidy.py'
-std=c++11,c++14,c++17
'C:\_work\llvm-project\llvm-project\clang-tools-extra\test\clang-tidy\checkers\readability\redundant-casting.cpp'
readability-redundant-casting
'C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\checkers\readability\Output\redundant-casting.cpp.tmp'
-- -- -fno-delayed-template-parsing
# .---command stdout
# | Running ['clang-tidy', '--experimental-custom-checks',
'C:\\_work\\llvm-project\\llvm-project\\build\\tools\\clang\\tools\\extra\\test\\clang-tidy\\checkers\\readability\\Output\\redundant-casting.cpp.tmp.cpp',
'-fix', '--checks=-*,readability-redundant-casting', '--config={}', '--',
'-fno-delayed-template-parsing', '-std=c++11', '-nostdinc++', '-isystem',
'C:\\_work\\llvm-project\\llvm-project\\clang-tools-extra\\test\\clang-tidy\\checkers\\Inputs\\Headers\\std']...
# | clang-tidy output ---
# | 29 warnings generated.
# |
# |
C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\checkers\readability\Output\redundant-casting.cpp.tmp.cpp:25:11:
warning: redundant explicit casting to the same type 'A' as the
sub-expression, remove this casting [readability-redundant-casting]
# |25 | A& a1 = static_cast(value);
# | | ^~~~ ~
# |
C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\checkers\readability\Output\redundant-casting.cpp.tmp.cpp:25:11:
note: FIX-IT applied suggested code changes
# |
C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\checkers\readability\Output\redundant-casting.cpp.tmp.cpp:25:32:
note: FIX-IT applied suggested code changes
# |25 | A& a1 = static_cast(value);
# | |^
# |
C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\checkers\readability\Output\redundant-casting.cpp.tmp.cpp:24:36:
note: source type originates from referencing this parameter
# |24 | void testRedundantStaticCasting(A& value) {
# | | ~~ ^
# |
C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\checkers\readability\Output\redundant-casting.cpp.tmp.cpp:32:11:
warning: redundant explicit casting to the same type 'A' as the
sub-expression, remove this casting [readability-redundant-casting]
# |32 | A& a2 = const_cast(value);
# | | ^~~ ~
# |
C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\checkers\readability\Output\redundant-casting.cpp.tmp.cpp:32:11:
note: FIX-IT applied suggested code changes
# |
C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\checkers\readability\Output\redundant-casting.cpp.tmp.cpp:32:31:
note: FIX-IT applied suggested code changes
# |32 | A& a2 = const_cast(value);
# | | ^
# |
C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\checkers\readability\Output\redundant-casting.cpp.tmp.cpp:31:36:
note: source type originates from referencing this parameter
# |31 | void testRedundantConstCasting1(A& value) {
# | | ~~ ^
# |
C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\checkers\readability\Output\redundant-casting.cpp.tmp.cpp:39:17:
warning: redundant explicit casting to the same type 'const A' as the
sub-expression, remove this casting [readability-redundant-casting]
# |39 | const A& a3 = const_cast(value);
# | | ^ ~
# |
C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\checkers\readability\Output\redundant-casting.cpp.tmp.cpp:39:17:
note: FIX-IT applied suggested code changes
# |
C:\_work\llvm-project\llvm-project\build\tools\clang\tools\extra\test\clang-tidy\checkers\readability\Output\redundant-casting.cpp.tmp.cpp:39:43:
note: FIX-IT applied sugg
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
github-actions[bot] wrote: # :penguin: Linux x64 Test Results * 3186 tests passed * 7 tests skipped * 1 test failed ## Failed Tests (click on a test name to see its output) ### Clang Tools Clang Tools.clang-tidy/infrastructure/alphabetical-order.test ``` Exit Code: 1 Command Output (stdout): -- # RUN: at line 1 /usr/bin/python3 /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../clang-tidy/tool/check_alphabetical_order.py -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.clang-tidy-checks-list.rst # executed command: /usr/bin/python3 /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../clang-tidy/tool/check_alphabetical_order.py -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.clang-tidy-checks-list.rst # note: command had no output on stdout or stderr # RUN: at line 2 diff --strip-trailing-cr /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../docs/clang-tidy/checks/list.rst /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.clang-tidy-checks-list.rst # executed command: diff --strip-trailing-cr /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../docs/clang-tidy/checks/list.rst /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.clang-tidy-checks-list.rst # note: command had no output on stdout or stderr # RUN: at line 4 /usr/bin/python3 /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../clang-tidy/tool/check_alphabetical_order.py -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.ReleaseNotes.rst # executed command: /usr/bin/python3 /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../clang-tidy/tool/check_alphabetical_order.py -o /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.ReleaseNotes.rst # .---command stderr # | # | Entries in 'clang-tools-extra/docs/ReleaseNotes.rst' are not alphabetically sorted. # | Fix the ordering by applying diff printed below. # | # `- # RUN: at line 5 diff --strip-trailing-cr /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../docs/ReleaseNotes.rst /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.ReleaseNotes.rst # executed command: diff --strip-trailing-cr /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../docs/ReleaseNotes.rst /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.ReleaseNotes.rst # .---command stdout # | *** /home/gha/actions-runner/_work/llvm-project/llvm-project/clang-tools-extra/test/clang-tidy/infrastructure/../../../docs/ReleaseNotes.rst # | --- /home/gha/actions-runner/_work/llvm-project/llvm-project/build/tools/clang/tools/extra/test/clang-tidy/infrastructure/Output/alphabetical-order.test.tmp.ReleaseNotes.rst # | *** # | *** 495,515 # | implicit conversions of logical operator results (``&&``, ``||``, ``!``) # | to ``bool`` in C. # | # | - Improved :doc:`readability-redundant-casting # | ` check by fixing a false # | negative where casts were not flagged as redundant when at least one operand # | of a binary operation had the same type as the cast result type. For example, # | ``static_cast(1.0f + 1)`` is now correctly identified as redundant. # | # | - - Improved :doc:`readability-non-const-parameter # | - ` check: # | - # | - - Avoid false positives on parameters used in dependent expressions # | - (e.g. inside generic lambdas). # | - # | - - Fixed a false positive in array subscript expressions where the types are # | - not yet resolved. # | - # | - Improved :doc:`readability-redundant-preprocessor # | ` check by fixing a # | false positive for nested ``#if`` directives using different builtin # | --- 495,515 # | implicit conversions of logical operato
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
@@ -491,14 +454,16 @@ Changes in existing checks implicit conversions of logical operator results (``&&``, ``||``, ``!``) to ``bool`` in C. -- Improved :doc:`readability-non-const-parameter - ` check: +- Improved :doc:`readability-redundant-casting zeyi2 wrote: Please maintain alphabetical order :) The new entry should be placed between `readability-non-const-parameter` and `readability-redundant-preprocessor`. https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
gxyd wrote: > Overall looks good, but please add a ReleaseNotes entry :) Done, I've added a ReleaseNotes entry as well now. https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
@@ -168,6 +170,20 @@ void testFunctionalCastWithInitExpr(unsigned a) {
unsigned c = unsigned{0};
}
+void testBinaryOperatorRedundantCasting() {
gxyd wrote:
Done, I've added more test cases for these.
https://github.com/llvm/llvm-project/pull/191386
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
https://github.com/gxyd updated https://github.com/llvm/llvm-project/pull/191386
>From 7d7cfaec6c4fa6897baa8e7f32a78dc33910fc6b Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra
Date: Fri, 10 Apr 2026 16:14:58 +0530
Subject: [PATCH 1/3] [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(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
+
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(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(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(1 +
static_cast(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(1)) };
+}
+
void testBinaryOperator(char c) {
int a = int(c - 'C');
}
>From a1a4ac783a29d71574e02c8f7bd7b0cbc8a5428a Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra
Date: Sat, 11 Apr 2026 12:47:51 +0530
Subject: [PATCH 2/3] add ReleaseNotes entry for the fixed issue
---
clang-tools-extra/docs/ReleaseNotes.rst | 6 ++
1 file changed, 6
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
zeyi2 wrote: > Does this need a ReleaseNotes entry? Generally yes, unless when we want to backport a commit or fixing a new feature that has not been released before :) https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
gxyd wrote: Does this need a ReleaseNotes entry? (I'm not sure if every PR submitted again clang-tidy subproject requires one) https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
llvmbot wrote: @llvm/pr-subscribers-clang-tools-extra Author: Gaurav Dhingra (gxyd) Changes 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(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 + 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(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(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(1 + static_cast(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(1)) }; +} + void testBinaryOperator(char c) { int a = int(c - 'C'); } `` https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
llvmbot wrote: @llvm/pr-subscribers-clang-tidy Author: Gaurav Dhingra (gxyd) Changes 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(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 + 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(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(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(1 + static_cast(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(1)) }; +} + void testBinaryOperator(char c) { int a = int(c - 'C'); } `` https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
github-actions[bot] wrote: Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using `@` followed by their GitHub username. If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the [LLVM GitHub User Guide](https://llvm.org/docs/GitHub.html). You can also ask questions in a comment on this PR, on the [LLVM Discord](https://discord.com/invite/xS7Z362) or on the [forums](https://discourse.llvm.org/). https://github.com/llvm/llvm-project/pull/191386 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Improve redundant-casting check for binary operation (PR #191386)
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(1 + 2.0f)` // redundant cast
Fixes #182132
>From 7d7cfaec6c4fa6897baa8e7f32a78dc33910fc6b Mon Sep 17 00:00:00 2001
From: Gaurav Dhingra
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(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
+
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(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(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(1 +
static_cast(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(1)) };
+}
+
void testBinaryOperator(char c) {
int a = int(c - 'C');
}
___
cfe-commits
