diff --git a/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantCastingCheck.cpp index b9ff0e81cbc52..fb85eb1628afb 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 8f7b0b5333f3a..e47fb8e898119 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 + ` check + by addressing a false positive in aggregate initialization through + parenthesized list. + - Improved :doc:`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 30cac6bd5cca0..9c3c90bfaf459 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(value); } +#ifdef CXX_20 +void testParenListInitExpr(A value) { + B b = static_cast(value); +} +#endif + void testCastingWithAuto() { auto a = getA(); A& a8 = static_cast(a);