[clang-tools-extra] dc2963c - [clang-tidy] exclude CXXParenListInitExpr from RedundantCastingCheck (#109741)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 11 02:04:23 PST 2025
Author: Tommy Chen
Date: 2025-01-11T11:04:19+01:00
New Revision: dc2963c8d77229ca1b20663beddef2323cc69a88
URL: https://github.com/llvm/llvm-project/commit/dc2963c8d77229ca1b20663beddef2323cc69a88
DIFF: https://github.com/llvm/llvm-project/commit/dc2963c8d77229ca1b20663beddef2323cc69a88.diff
LOG: [clang-tidy] exclude CXXParenListInitExpr from RedundantCastingCheck (#109741)
Exclude CXXParenListInitExpr from RedundantCastingCheck because there
are false positive cases. Currently, we can't think of positive cases
for CXXParenListInitExpr. This can be improved by following the
initListExpr method if we can come up with some positive cases.
Fixes #108846
Added:
Modified:
clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp
index 4d5adbe02f5256..768540e05c7597 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/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 684ba77d8f0f53..9cdad8fb6b58f2 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -360,6 +360,11 @@ Changes in existing checks
case of the literal suffix in fixes and fixing false positive for implicit
conversion of comparison result in C23.
+- 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.
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..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++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++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++11-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,6 +65,12 @@ 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();
A& a8 = static_cast<A&>(a);
More information about the cfe-commits
mailing list