[clang-tools-extra] [clang-tidy] exclude CXXParenListInitExpr from RedundantCastingCheck (PR #109741)
Tommy Chen via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 25 07:51:35 PDT 2024
https://github.com/dl8sd11 updated https://github.com/llvm/llvm-project/pull/109741
>From 13bc00c2ffb4238903b57c0a3c77424ed35279cc Mon Sep 17 00:00:00 2001
From: dl8sd11 <gcchen at google.com>
Date: Mon, 23 Sep 2024 17:52:25 +0000
Subject: [PATCH 1/6] [clang-tidy] eclude CXXParenListInitExpr
Exclude CXXParenListInitExpr from RedundantCastingCheck because
there are false positive cases. Currently, we can't think of
positive cases for CXXParenListInitExpr. This can be improve
by following the initListExpr method if we can come up with
some positive cases.
---
.../clang-tidy/readability/RedundantCastingCheck.cpp | 5 +++++
.../checkers/readability/redundant-casting.cpp | 10 +++++++---
2 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp
index b9ff0e81cbc522..fb85eb1628afbd 100644
--- a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp
@@ -108,6 +108,10 @@ void RedundantCastingCheck::registerMatchers(MatchFinder *Finder) {
auto BitfieldMemberExpr = memberExpr(member(fieldDecl(isBitField())));
+ const ast_matchers::internal::VariadicDynCastAllOfMatcher<
+ Stmt, CXXParenListInitExpr>
+ cxxParenListInitExpr; // NOLINT(readability-identifier-naming)
+
Finder->addMatcher(
explicitCastExpr(
unless(hasCastKind(CK_ConstructorConversion)),
@@ -117,6 +121,7 @@ void RedundantCastingCheck::registerMatchers(MatchFinder *Finder) {
hasDestinationType(qualType().bind("dstType")),
hasSourceExpression(anyOf(
expr(unless(initListExpr()), unless(BitfieldMemberExpr),
+ unless(cxxParenListInitExpr()),
hasType(qualType().bind("srcType")))
.bind("source"),
initListExpr(unless(hasInit(1, expr())),
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 30cac6bd5cca06..d94452d84a5841 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
@@ -1,8 +1,8 @@
-// RUN: %check_clang_tidy -std=c++11-or-later %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing
-// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=,MACROS %s readability-redundant-casting %t -- \
+// RUN: %check_clang_tidy -std=c++20-or-later %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing
+// RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=,MACROS %s readability-redundant-casting %t -- \
// RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreMacros: false }}' \
// RUN: -- -fno-delayed-template-parsing
-// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=,ALIASES %s readability-redundant-casting %t -- \
+// RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=,ALIASES %s readability-redundant-casting %t -- \
// RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreTypeAliases: true }}' \
// RUN: -- -fno-delayed-template-parsing
@@ -57,6 +57,10 @@ void testDiffrentTypesCast(B& value) {
A& a7 = static_cast<A&>(value);
}
+void testParenListInitExpr(A value) {
+ B b = static_cast<B>(value);
+}
+
void testCastingWithAuto() {
auto a = getA();
A& a8 = static_cast<A&>(a);
>From cca8471cab0ceefb9bf1bf5eb40c3f25272ced16 Mon Sep 17 00:00:00 2001
From: dl8sd11 <gcchen at google.com>
Date: Tue, 24 Sep 2024 17:26:20 +0000
Subject: [PATCH 2/6] [clang-tidy] update ReleaseNotes.rst
---
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 8f7b0b5333f3a1..069230461e21b9 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -183,6 +183,10 @@ Changes in existing checks
by adding the option `UseUpperCaseLiteralSuffix` to select the
case of the literal suffix in fixes.
+- Improved :doc:`readability-redundant-casting
+ <clang-tidy/checks/readability/redundant-casting>` check to
+ exclude `CXXParenListInitExpr` for the source expression matcher.
+
- Improved :doc:`readability-redundant-smartptr-get
<clang-tidy/checks/readability/redundant-smartptr-get>` check to
remove `->`, when redundant `get()` is removed.
>From 09197edae72a6ce76aff713613d28d0a381100ac Mon Sep 17 00:00:00 2001
From: dl8sd11 <gcchen at google.com>
Date: Tue, 24 Sep 2024 17:28:55 +0000
Subject: [PATCH 3/6] amend! [clang-tidy] eclude CXXParenListInitExpr
[clang-tidy] exclude CXXParenListInitExpr
Exclude CXXParenListInitExpr from RedundantCastingCheck because
there are false positive cases. Currently, we can't think of
positive cases for CXXParenListInitExpr. This can be improve
by following the initListExpr method if we can come up with
some positive cases.
>From 40dcfa9754fe802ab60e63b8e89e6eaf74e47fad Mon Sep 17 00:00:00 2001
From: dl8sd11 <gcchen at google.com>
Date: Wed, 25 Sep 2024 04:36:35 +0000
Subject: [PATCH 4/6] Revert "[clang-tidy] update ReleaseNotes.rst"
This reverts commit cca8471cab0ceefb9bf1bf5eb40c3f25272ced16.
---
clang-tools-extra/docs/ReleaseNotes.rst | 4 ----
1 file changed, 4 deletions(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 069230461e21b9..8f7b0b5333f3a1 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -183,10 +183,6 @@ Changes in existing checks
by adding the option `UseUpperCaseLiteralSuffix` to select the
case of the literal suffix in fixes.
-- Improved :doc:`readability-redundant-casting
- <clang-tidy/checks/readability/redundant-casting>` check to
- exclude `CXXParenListInitExpr` for the source expression matcher.
-
- Improved :doc:`readability-redundant-smartptr-get
<clang-tidy/checks/readability/redundant-smartptr-get>` check to
remove `->`, when redundant `get()` is removed.
>From 46f73265adb9e70c65158a73ce837ced7e5d4c65 Mon Sep 17 00:00:00 2001
From: dl8sd11 <gcchen at google.com>
Date: Wed, 25 Sep 2024 04:40:56 +0000
Subject: [PATCH 5/6] [clang-tidy] update ReleaseNotes.rst
---
clang-tools-extra/docs/ReleaseNotes.rst | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 8f7b0b5333f3a1..e47fb8e8981191 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -183,6 +183,11 @@ Changes in existing checks
by adding the option `UseUpperCaseLiteralSuffix` to select the
case of the literal suffix in fixes.
+- Improved :doc:`readability-redundant-casting
+ <clang-tidy/checks/readability/redundant-casting>` check
+ by addressing a false positive in aggregate initialization through
+ parenthesized list.
+
- Improved :doc:`readability-redundant-smartptr-get
<clang-tidy/checks/readability/redundant-smartptr-get>` check to
remove `->`, when redundant `get()` is removed.
>From 13620f1e041f02dd5282957c29d4670b6029040e Mon Sep 17 00:00:00 2001
From: dl8sd11 <gcchen at google.com>
Date: Wed, 25 Sep 2024 14:50:55 +0000
Subject: [PATCH 6/6] [clang-tidy] keep tests for c++11,14,17
---
.../checkers/readability/redundant-casting.cpp | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)
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 d94452d84a5841..9c3c90bfaf4595 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
@@ -1,10 +1,18 @@
-// RUN: %check_clang_tidy -std=c++20-or-later %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing
-// RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=,MACROS %s readability-redundant-casting %t -- \
+// RUN: %check_clang_tidy -std=c++11,c++14,c++17 %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing
+// RUN: %check_clang_tidy -std=c++11,c++14,c++17 -check-suffix=,MACROS %s readability-redundant-casting %t -- \
// RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreMacros: false }}' \
// RUN: -- -fno-delayed-template-parsing
-// RUN: %check_clang_tidy -std=c++20-or-later -check-suffix=,ALIASES %s readability-redundant-casting %t -- \
+// RUN: %check_clang_tidy -std=c++11,c++14,c++17 -check-suffix=,ALIASES %s readability-redundant-casting %t -- \
// RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreTypeAliases: true }}' \
// RUN: -- -fno-delayed-template-parsing
+// RUN: %check_clang_tidy -std=c++20 %s readability-redundant-casting %t -- \
+// RUN: -- -fno-delayed-template-parsing -D CXX_20=1
+// RUN: %check_clang_tidy -std=c++20 -check-suffix=,MACROS %s readability-redundant-casting %t -- \
+// RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreMacros: false }}' \
+// RUN: -- -fno-delayed-template-parsing -D CXX_20=1
+// RUN: %check_clang_tidy -std=c++20 -check-suffix=,ALIASES %s readability-redundant-casting %t -- \
+// RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreTypeAliases: true }}' \
+// RUN: -- -fno-delayed-template-parsing -D CXX_20=1
struct A {};
struct B : A {};
@@ -57,9 +65,11 @@ void testDiffrentTypesCast(B& value) {
A& a7 = static_cast<A&>(value);
}
+#ifdef CXX_20
void testParenListInitExpr(A value) {
B b = static_cast<B>(value);
}
+#endif
void testCastingWithAuto() {
auto a = getA();
More information about the cfe-commits
mailing list