Skip to content

[clang-tidy] exclude CXXParenListInitExpr from RedundantCastingCheck #109741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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())),
Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {};
Expand Down Expand Up @@ -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);
Expand Down
Loading