Izaron created this revision.
Izaron added reviewers: hokein, aaron.ballman, LegalizeAdulthood, njames93.
Herald added subscribers: carlosgalvezp, xazax.hun.
Herald added a project: All.
Izaron requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Ignore constrained perfect forwarding constructors.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135476

Files:
  clang-tools-extra/clang-tidy/bugprone/ForwardingReferenceOverloadCheck.cpp
  
clang-tools-extra/docs/clang-tidy/checks/bugprone/forwarding-reference-overload.rst
  
clang-tools-extra/test/clang-tidy/checkers/bugprone/forwarding-reference-overload.cpp


Index: 
clang-tools-extra/test/clang-tidy/checkers/bugprone/forwarding-reference-overload.cpp
===================================================================
--- 
clang-tools-extra/test/clang-tidy/checkers/bugprone/forwarding-reference-overload.cpp
+++ 
clang-tools-extra/test/clang-tidy/checkers/bugprone/forwarding-reference-overload.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy %s bugprone-forwarding-reference-overload %t
+// RUN: %check_clang_tidy -std=c++20 %s bugprone-forwarding-reference-overload 
%t
 
 namespace std {
 template <bool B, class T = void> struct enable_if { typedef T type; };
@@ -251,3 +251,20 @@
   Test10(T &&Item, E e)
       : e(e){}
 };
+
+class Test11 {
+public:
+  // Guarded with requires expression.
+  template <typename T>
+  requires requires { just_true<T>; }
+  Test11(T &&n);
+};
+
+template<typename T>
+concept JustTrueConcept = requires { just_true<T>; };
+
+class Test12 {
+public:
+  // Guarded with concept requirement.
+  template <JustTrueConcept T> Test12(T &&n);
+};
Index: 
clang-tools-extra/docs/clang-tidy/checks/bugprone/forwarding-reference-overload.rst
===================================================================
--- 
clang-tools-extra/docs/clang-tidy/checks/bugprone/forwarding-reference-overload.rst
+++ 
clang-tools-extra/docs/clang-tidy/checks/bugprone/forwarding-reference-overload.rst
@@ -34,6 +34,15 @@
         enable_if_t<is_constructible_v<tuple<string, int>, A&&...>, int> = 0>
       explicit Person(A&&... a) {}
 
+      // C5: perfect forwarding ctor guarded with requires expression
+      template<typename T>
+      requires requires { is_special<T>; }
+      explicit Person(T&& n) {}
+
+      // C6: perfect forwarding ctor guarded with concept requirement
+      template<Special T>
+      explicit Person(T&& n) {}
+
       // (possibly compiler generated) copy ctor
       Person(const Person& rhs);
     };
@@ -42,7 +51,7 @@
 constructors. We suppress warnings if the copy and the move constructors are 
both
 disabled (deleted or private), because there is nothing the perfect forwarding
 constructor could hide in this case. We also suppress warnings for constructors
-like C3 and C4 that are guarded with an ``enable_if``, assuming the programmer 
was
+like C3-C6 that are guarded with an ``enable_if`` or a concept, assuming the 
programmer was
 aware of the possible hiding.
 
 Background
Index: 
clang-tools-extra/clang-tidy/bugprone/ForwardingReferenceOverloadCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/ForwardingReferenceOverloadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/ForwardingReferenceOverloadCheck.cpp
@@ -58,6 +58,9 @@
   return Node.hasDefaultArgument() &&
          TypeMatcher.matches(Node.getDefaultArgument(), Finder, Builder);
 }
+AST_MATCHER(TemplateDecl, hasAssociatedConstraints) {
+  return Node.hasAssociatedConstraints();
+}
 } // namespace
 
 void ForwardingReferenceOverloadCheck::registerMatchers(MatchFinder *Finder) {
@@ -76,6 +79,9 @@
               // No warning: enable_if as constructor parameter.
               parmVarDecl(hasType(isEnableIf())))),
           unless(hasParent(functionTemplateDecl(anyOf(
+              // No warning: has associated constraints (like requires
+              // expression).
+              hasAssociatedConstraints(),
               // No warning: enable_if as type parameter.
               has(templateTypeParmDecl(hasDefaultArgument(isEnableIf()))),
               // No warning: enable_if as non-type template parameter.


Index: clang-tools-extra/test/clang-tidy/checkers/bugprone/forwarding-reference-overload.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/bugprone/forwarding-reference-overload.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/bugprone/forwarding-reference-overload.cpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy %s bugprone-forwarding-reference-overload %t
+// RUN: %check_clang_tidy -std=c++20 %s bugprone-forwarding-reference-overload %t
 
 namespace std {
 template <bool B, class T = void> struct enable_if { typedef T type; };
@@ -251,3 +251,20 @@
   Test10(T &&Item, E e)
       : e(e){}
 };
+
+class Test11 {
+public:
+  // Guarded with requires expression.
+  template <typename T>
+  requires requires { just_true<T>; }
+  Test11(T &&n);
+};
+
+template<typename T>
+concept JustTrueConcept = requires { just_true<T>; };
+
+class Test12 {
+public:
+  // Guarded with concept requirement.
+  template <JustTrueConcept T> Test12(T &&n);
+};
Index: clang-tools-extra/docs/clang-tidy/checks/bugprone/forwarding-reference-overload.rst
===================================================================
--- clang-tools-extra/docs/clang-tidy/checks/bugprone/forwarding-reference-overload.rst
+++ clang-tools-extra/docs/clang-tidy/checks/bugprone/forwarding-reference-overload.rst
@@ -34,6 +34,15 @@
         enable_if_t<is_constructible_v<tuple<string, int>, A&&...>, int> = 0>
       explicit Person(A&&... a) {}
 
+      // C5: perfect forwarding ctor guarded with requires expression
+      template<typename T>
+      requires requires { is_special<T>; }
+      explicit Person(T&& n) {}
+
+      // C6: perfect forwarding ctor guarded with concept requirement
+      template<Special T>
+      explicit Person(T&& n) {}
+
       // (possibly compiler generated) copy ctor
       Person(const Person& rhs);
     };
@@ -42,7 +51,7 @@
 constructors. We suppress warnings if the copy and the move constructors are both
 disabled (deleted or private), because there is nothing the perfect forwarding
 constructor could hide in this case. We also suppress warnings for constructors
-like C3 and C4 that are guarded with an ``enable_if``, assuming the programmer was
+like C3-C6 that are guarded with an ``enable_if`` or a concept, assuming the programmer was
 aware of the possible hiding.
 
 Background
Index: clang-tools-extra/clang-tidy/bugprone/ForwardingReferenceOverloadCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/bugprone/ForwardingReferenceOverloadCheck.cpp
+++ clang-tools-extra/clang-tidy/bugprone/ForwardingReferenceOverloadCheck.cpp
@@ -58,6 +58,9 @@
   return Node.hasDefaultArgument() &&
          TypeMatcher.matches(Node.getDefaultArgument(), Finder, Builder);
 }
+AST_MATCHER(TemplateDecl, hasAssociatedConstraints) {
+  return Node.hasAssociatedConstraints();
+}
 } // namespace
 
 void ForwardingReferenceOverloadCheck::registerMatchers(MatchFinder *Finder) {
@@ -76,6 +79,9 @@
               // No warning: enable_if as constructor parameter.
               parmVarDecl(hasType(isEnableIf())))),
           unless(hasParent(functionTemplateDecl(anyOf(
+              // No warning: has associated constraints (like requires
+              // expression).
+              hasAssociatedConstraints(),
               // No warning: enable_if as type parameter.
               has(templateTypeParmDecl(hasDefaultArgument(isEnableIf()))),
               // No warning: enable_if as non-type template parameter.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to