Skip to content

Commit dc2963c

Browse files
authored
[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
1 parent 30bb186 commit dc2963c

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ void RedundantCastingCheck::registerMatchers(MatchFinder *Finder) {
108108

109109
auto BitfieldMemberExpr = memberExpr(member(fieldDecl(isBitField())));
110110

111+
const ast_matchers::internal::VariadicDynCastAllOfMatcher<
112+
Stmt, CXXParenListInitExpr>
113+
cxxParenListInitExpr; // NOLINT(readability-identifier-naming)
114+
111115
Finder->addMatcher(
112116
explicitCastExpr(
113117
unless(hasCastKind(CK_ConstructorConversion)),
@@ -117,6 +121,7 @@ void RedundantCastingCheck::registerMatchers(MatchFinder *Finder) {
117121
hasDestinationType(qualType().bind("dstType")),
118122
hasSourceExpression(anyOf(
119123
expr(unless(initListExpr()), unless(BitfieldMemberExpr),
124+
unless(cxxParenListInitExpr()),
120125
hasType(qualType().bind("srcType")))
121126
.bind("source"),
122127
initListExpr(unless(hasInit(1, expr())),

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,11 @@ Changes in existing checks
360360
case of the literal suffix in fixes and fixing false positive for implicit
361361
conversion of comparison result in C23.
362362

363+
- Improved :doc:`readability-redundant-casting
364+
<clang-tidy/checks/readability/redundant-casting>` check
365+
by addressing a false positive in aggregate initialization through
366+
parenthesized list.
367+
363368
- Improved :doc:`readability-redundant-smartptr-get
364369
<clang-tidy/checks/readability/redundant-smartptr-get>` check to
365370
remove `->`, when redundant `get()` is removed.

clang-tools-extra/test/clang-tidy/checkers/readability/redundant-casting.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
// RUN: %check_clang_tidy -std=c++11-or-later %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing
2-
// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=,MACROS %s readability-redundant-casting %t -- \
1+
// RUN: %check_clang_tidy -std=c++11,c++14,c++17 %s readability-redundant-casting %t -- -- -fno-delayed-template-parsing
2+
// RUN: %check_clang_tidy -std=c++11,c++14,c++17 -check-suffix=,MACROS %s readability-redundant-casting %t -- \
33
// RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreMacros: false }}' \
44
// RUN: -- -fno-delayed-template-parsing
5-
// RUN: %check_clang_tidy -std=c++11-or-later -check-suffix=,ALIASES %s readability-redundant-casting %t -- \
5+
// RUN: %check_clang_tidy -std=c++11,c++14,c++17 -check-suffix=,ALIASES %s readability-redundant-casting %t -- \
66
// RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreTypeAliases: true }}' \
77
// RUN: -- -fno-delayed-template-parsing
8+
// RUN: %check_clang_tidy -std=c++20 %s readability-redundant-casting %t -- \
9+
// RUN: -- -fno-delayed-template-parsing -D CXX_20=1
10+
// RUN: %check_clang_tidy -std=c++20 -check-suffix=,MACROS %s readability-redundant-casting %t -- \
11+
// RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreMacros: false }}' \
12+
// RUN: -- -fno-delayed-template-parsing -D CXX_20=1
13+
// RUN: %check_clang_tidy -std=c++20 -check-suffix=,ALIASES %s readability-redundant-casting %t -- \
14+
// RUN: -config='{CheckOptions: { readability-redundant-casting.IgnoreTypeAliases: true }}' \
15+
// RUN: -- -fno-delayed-template-parsing -D CXX_20=1
816

917
struct A {};
1018
struct B : A {};
@@ -57,6 +65,12 @@ void testDiffrentTypesCast(B& value) {
5765
A& a7 = static_cast<A&>(value);
5866
}
5967

68+
#ifdef CXX_20
69+
void testParenListInitExpr(A value) {
70+
B b = static_cast<B>(value);
71+
}
72+
#endif
73+
6074
void testCastingWithAuto() {
6175
auto a = getA();
6276
A& a8 = static_cast<A&>(a);

0 commit comments

Comments
 (0)