[clang-tools-extra] [clang-tidy] Fix handling of functional cast in google-readability-casting (PR #71650)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 8 02:35:50 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
Author: Piotr Zegar (PiotrZSL)
<details>
<summary>Changes</summary>
Fix issue with constructor call being interpreted as functional cast and considered for a replacement
with static cast or being removed as redundant.
Closes #<!-- -->57959
---
Full diff: https://github.com/llvm/llvm-project/pull/71650.diff
4 Files Affected:
- (modified) clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp (+8-5)
- (modified) clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.h (+3)
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4)
- (modified) clang-tools-extra/test/clang-tidy/checkers/google/readability-casting.cpp (+5-10)
``````````diff
diff --git a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
index 714ac0ee54dafb3..3afb93ac7f7dd15 100644
--- a/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/google/AvoidCStyleCastsCheck.cpp
@@ -18,19 +18,22 @@ namespace clang::tidy::google::readability {
void AvoidCStyleCastsCheck::registerMatchers(
ast_matchers::MatchFinder *Finder) {
+
Finder->addMatcher(
cStyleCastExpr(
// 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 485640d230280ee..4267b896b6992c6 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<TraversalKind> 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 fe8c7175d554c7b..f02f4bba8916bb6 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -290,6 +290,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
+ <clang-tidy/checks/google/readability-casting>` check to ignore constructor
+ calls disguised as functional casts.
+
- Improved :doc:`llvm-namespace-comment
<clang-tidy/checks/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 e25463cf451b741..fdc71167cd82b07 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 <class T>
-T functional_cast_template_used_by_class(float i) {
+T functional_cast_template(float i) {
return T(i);
}
-template <class T>
-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<T>(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<S2>(x);
- functional_cast_template_used_by_int<int>(x);
+ functional_cast_template<S2>(x);
+ functional_cast_template<int>(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);
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/71650
More information about the cfe-commits
mailing list