[PATCH] D113828: [clang-tidy] Fix false positives in `fuchsia-trailing-return` check involving deduction guides

Fabian Wolff via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 13 09:06:21 PST 2021


fwolff created this revision.
fwolff added reviewers: alexfh, aaron.ballman, mizvekov.
fwolff added a project: clang-tools-extra.
Herald added subscribers: carlosgalvezp, abrachet, phosek, xazax.hun.
fwolff requested review of this revision.
Herald added a subscriber: cfe-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D113828

Files:
  clang-tools-extra/clang-tidy/fuchsia/TrailingReturnCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/fuchsia-trailing-return.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/fuchsia-trailing-return.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/fuchsia-trailing-return.cpp
+++ 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 @@
 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");
+}
Index: clang-tools-extra/clang-tidy/fuchsia/TrailingReturnCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/fuchsia/TrailingReturnCheck.cpp
+++ clang-tools-extra/clang-tidy/fuchsia/TrailingReturnCheck.cpp
@@ -17,12 +17,6 @@
 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 @@
   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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113828.387027.patch
Type: text/x-patch
Size: 3146 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211113/868abb9c/attachment.bin>


More information about the cfe-commits mailing list