[clang-tools-extra] 844a8d3 - Fix false positives in `fuchsia-trailing-return` check involving deduction guides

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 1 12:28:42 PST 2021


Author: Fabian Wolff
Date: 2021-12-01T15:28:01-05:00
New Revision: 844a8d3cecb4cc40e5d9694bcf111518910ea2ff

URL: https://github.com/llvm/llvm-project/commit/844a8d3cecb4cc40e5d9694bcf111518910ea2ff
DIFF: https://github.com/llvm/llvm-project/commit/844a8d3cecb4cc40e5d9694bcf111518910ea2ff.diff

LOG: Fix false positives in `fuchsia-trailing-return` check involving deduction guides

Fixes PR#47614. Deduction guides, implicit or user-defined, look like
function declarations in the AST. They aren't really functions, though,
and they always have a trailing return type, so it doesn't make sense
to issue this warning for them.

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/fuchsia/TrailingReturnCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/fuchsia-trailing-return.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/fuchsia/TrailingReturnCheck.cpp b/clang-tools-extra/clang-tidy/fuchsia/TrailingReturnCheck.cpp
index 4ade4767190ef..2902e34fe860f 100644
--- a/clang-tools-extra/clang-tidy/fuchsia/TrailingReturnCheck.cpp
+++ b/clang-tools-extra/clang-tidy/fuchsia/TrailingReturnCheck.cpp
@@ -17,12 +17,6 @@ namespace clang {
 namespace tidy {
 namespace fuchsia {
 
-namespace {
-AST_MATCHER(FunctionDecl, hasTrailingReturn) {
-  return Node.getType()->castAs<FunctionProtoType>()->hasTrailingReturn();
-}
-} // namespace
-
 void TrailingReturnCheck::registerMatchers(MatchFinder *Finder) {
   // Functions that have trailing returns are disallowed, except for those
   // using decltype specifiers and lambda with otherwise unutterable
@@ -30,15 +24,16 @@ void TrailingReturnCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(
       functionDecl(hasTrailingReturn(),
                    unless(anyOf(returns(decltypeType()),
-                                hasParent(cxxRecordDecl(isLambda())))))
+                                hasParent(cxxRecordDecl(isLambda())),
+                                cxxDeductionGuideDecl())))
           .bind("decl"),
       this);
 }
 
 void TrailingReturnCheck::check(const MatchFinder::MatchResult &Result) {
-  if (const auto *D = Result.Nodes.getNodeAs<Decl>("decl"))
+  if (const auto *D = Result.Nodes.getNodeAs<FunctionDecl>("decl"))
     diag(D->getBeginLoc(),
-         "a trailing return type is disallowed for this type of declaration");
+         "a trailing return type is disallowed for this function declaration");
 }
 
 } // namespace fuchsia

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index fa03d7eac99e0..8991bac7c1f8a 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -140,6 +140,9 @@ Changes in existing checks
   <clang-tidy/checks/google-readability-casting>` to diagnose and fix functional
   casts, to achieve feature parity with the corresponding ``cpplint.py`` check.
 
+- Fixed a false positive in :doc:`fuchsia-trailing-return
+  <clang-tidy/checks/fuchsia-trailing-return>` for C++17 deduction guides.
+
 Removed checks
 ^^^^^^^^^^^^^^
 

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/fuchsia-trailing-return.cpp b/clang-tools-extra/test/clang-tidy/checkers/fuchsia-trailing-return.cpp
index f6c943ac873e7..14c4452e4fdc2 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/fuchsia-trailing-return.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/fuchsia-trailing-return.cpp
@@ -1,9 +1,9 @@
-// RUN: %check_clang_tidy %s fuchsia-trailing-return %t
+// RUN: %check_clang_tidy -std=c++17-or-later %s fuchsia-trailing-return %t
 
 int add_one(const int arg) { return arg; }
 
 auto get_add_one() -> int (*)(const int) {
-  // CHECK-MESSAGES: [[@LINE-1]]:1: warning: a trailing return type is disallowed for this type of declaration
+  // CHECK-MESSAGES: [[@LINE-1]]:1: warning: a trailing return type is disallowed for this function declaration
   // CHECK-NEXT: auto get_add_one() -> int (*)(const int) {
   return add_one;
 }
@@ -21,3 +21,31 @@ template <typename T1, typename T2>
 auto fn(const T1 &lhs, const T2 &rhs) -> decltype(lhs + rhs) {
   return lhs + rhs;
 }
+
+// Now check that implicit and explicit C++17 deduction guides don't trigger this warning (PR#47614).
+
+template <typename T>
+struct ImplicitDeductionGuides {
+  ImplicitDeductionGuides(const T &);
+};
+
+template <typename A, typename B>
+struct pair {
+  A first;
+  B second;
+};
+
+template <typename T>
+struct UserDefinedDeductionGuides {
+  UserDefinedDeductionGuides(T);
+  template <typename T1, typename T2>
+  UserDefinedDeductionGuides(T1, T2);
+};
+
+template <typename T1, typename T2>
+UserDefinedDeductionGuides(T1, T2) -> UserDefinedDeductionGuides<pair<T1, T2>>;
+
+void foo() {
+  ImplicitDeductionGuides X(42);
+  UserDefinedDeductionGuides s(1, "abc");
+}


        


More information about the cfe-commits mailing list