diff --git a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp index 995961b077480..3109bbb3724c7 100644 --- a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp @@ -23,14 +23,16 @@ void AvoidCStyleCastsCheck::registerMatchers( // Filter out (EnumType)IntegerLiteral construct, which is generated // for non-type template arguments of enum types. // FIXME: Remove this once this is fixed in the AST. - unless(hasParent(substNonTypeTemplateParmExpr())), - // Avoid matches in template instantiations. - unless(isInTemplateInstantiation())) + unless(hasParent(substNonTypeTemplateParmExpr()))) .bind("cast"), this); + Finder->addMatcher( - cxxFunctionalCastExpr(unless(hasDescendant(cxxConstructExpr())), - unless(hasDescendant(initListExpr()))) + cxxFunctionalCastExpr( + hasDestinationType(hasCanonicalType(anyOf( + builtinType(), references(qualType()), pointsTo(qualType())))), + unless( + hasSourceExpression(anyOf(cxxConstructExpr(), initListExpr())))) .bind("cast"), this); } diff --git a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.h b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.h index 485640d230280..4267b896b6992 100644 --- a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.h +++ b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.h @@ -31,6 +31,9 @@ class AvoidCStyleCastsCheck : public ClangTidyCheck { : ClangTidyCheck(Name, Context) {} void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + std::optional getCheckTraversalKind() const override { + return TK_IgnoreUnlessSpelledInSource; + } }; } // namespace clang::tidy::google::readability diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index bdbdee31f39d1..2822b17b43514 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -370,6 +370,10 @@ Changes in existing checks to ignore unused parameters when they are marked as unused and parameters of deleted functions and constructors. +- Improved :doc:`google-readability-casting + ` check to ignore constructor + calls disguised as functional casts. + - Improved :doc:`llvm-namespace-comment ` check to provide fixes for ``inline`` namespaces in the same format as :program:`clang-format`. diff --git a/clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp b/clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp index e25463cf451b7..fdc71167cd82b 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp @@ -322,17 +322,10 @@ void conversions() { } template -T functional_cast_template_used_by_class(float i) { +T functional_cast_template(float i) { return T(i); } -template -T functional_cast_template_used_by_int(float i) { - return T(i); - // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: C-style casts are discouraged; use static_cast - // CHECK-FIXES: return static_cast(i); -} - struct S2 { S2(float); }; @@ -356,8 +349,8 @@ void functional_casts() { auto s = S(str); // Functional casts in template functions - functional_cast_template_used_by_class(x); - functional_cast_template_used_by_int(x); + functional_cast_template(x); + functional_cast_template(x); // New expressions are not functional casts auto w = new int(x); @@ -366,4 +359,6 @@ void functional_casts() { S2 t = T(x); // OK, constructor call S2 u = U(x); // NOK, it's a reinterpret_cast in disguise // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: C-style casts are discouraged; use static_cast/const_cast/reinterpret_cast + + throw S2(5.0f); }