brintos

brintos / llvm-project-archived public Read only

0
0
Text · 9.4 KiB · 2731519 Raw
270 lines · cpp
1// RUN: %check_clang_tidy %s bugprone-forwarding-reference-overload %t2 3namespace std {4template <bool B, class T = void> struct enable_if { typedef T type; };5 6template <class T> struct enable_if<true, T> { typedef T type; };7 8template <bool B, class T = void>9using enable_if_t = typename enable_if<B, T>::type;10 11template <class T> struct enable_if_nice { typedef T type; };12} // namespace std13 14namespace foo {15template <class T> struct enable_if { typedef T type; };16} // namespace foo17 18template <typename T> constexpr bool just_true = true;19 20class Test1 {21public:22  template <typename T> Test1(T &&n);23  // CHECK-NOTES: [[@LINE-1]]:25: warning: constructor accepting a forwarding reference can hide the copy and move constructors [bugprone-forwarding-reference-overload]24  // CHECK-NOTES: 48:3: note: copy constructor declared here25  // CHECK-NOTES: 49:3: note: copy constructor declared here26  // CHECK-NOTES: 50:3: note: move constructor declared here27 28  template <typename T> Test1(T &&n, int i = 5, ...);29  // CHECK-NOTES: :[[@LINE-1]]:25: warning: constructor accepting a forwarding reference can hide the copy and move constructors30  // CHECK-NOTES: 48:3: note: copy constructor declared here31  // CHECK-NOTES: 49:3: note: copy constructor declared here32  // CHECK-NOTES: 50:3: note: move constructor declared here33 34  template <typename T, typename U = typename std::enable_if_nice<T>::type>35  Test1(T &&n);36  // CHECK-NOTES: :[[@LINE-1]]:3: warning: constructor accepting a forwarding reference can hide the copy and move constructors37  // CHECK-NOTES: 48:3: note: copy constructor declared here38  // CHECK-NOTES: 49:3: note: copy constructor declared here39  // CHECK-NOTES: 50:3: note: move constructor declared here40 41  template <typename T>42  Test1(T &&n, typename foo::enable_if<long>::type i = 5, ...);43  // CHECK-NOTES: :[[@LINE-1]]:3: warning: constructor accepting a forwarding reference can hide the copy and move constructors44  // CHECK-NOTES: 48:3: note: copy constructor declared here45  // CHECK-NOTES: 49:3: note: copy constructor declared here46  // CHECK-NOTES: 50:3: note: move constructor declared here47 48  Test1(const Test1 &other) {}49  Test1(Test1 &other) {}50  Test1(Test1 &&other) {}51};52 53template <typename U> class Test2 {54public:55  // Two parameters without default value, can't act as copy / move constructor.56  template <typename T, class V> Test2(T &&n, V &&m, int i = 5, ...);57 58  // Guarded with enable_if.59  template <typename T>60  Test2(T &&n, int i = 5,61        std::enable_if_t<sizeof(int) < sizeof(long), int> a = 5, ...);62 63  // Guarded with enable_if.64  template <typename T, typename X = typename std::enable_if<65                            sizeof(int) < sizeof(long), double>::type &>66  Test2(T &&n);67 68  // Guarded with enable_if.69  template <typename T>70  Test2(T &&n, typename std::enable_if<just_true<T>>::type **a = nullptr);71 72  // Guarded with enable_if.73  template <typename T, typename X = std::enable_if_t<just_true<T>> *&&>74  Test2(T &&n, double d = 0.0);75 76  // Not a forwarding reference parameter.77  template <typename T> Test2(const T &&n);78 79  // Not a forwarding reference parameter.80  Test2(int &&x);81 82  // Two parameters without default value, can't act as copy / move constructor.83  template <typename T> Test2(T &&n, int x);84 85  // Not a forwarding reference parameter.86  template <typename T> Test2(U &&n);87};88 89// The copy and move constructors are both disabled.90class Test3 {91public:92  template <typename T> Test3(T &&n);93 94  template <typename T> Test3(T &&n, int I = 5, ...);95 96  Test3(const Test3 &rhs) = delete;97 98private:99  Test3(Test3 &&rhs);100};101 102// Both the copy and the (compiler generated) move constructors can be hidden.103class Test4 {104public:105  template <typename T> Test4(T &&n);106  // CHECK-NOTES: :[[@LINE-1]]:25: warning: constructor accepting a forwarding reference can hide the copy and move constructors107 108  Test4(const Test4 &rhs);109  // CHECK-NOTES: :[[@LINE-1]]:3: note: copy constructor declared here110};111 112// Nothing can be hidden, the copy constructor is implicitly deleted.113class Test5 {114public:115  template <typename T> Test5(T &&n);116 117  Test5(Test5 &&rhs) = delete;118};119 120// Only the move constructor can be hidden.121class Test6 {122public:123  template <typename T> Test6(T &&n);124  // CHECK-NOTES: :[[@LINE-1]]:25: warning: constructor accepting a forwarding reference can hide the move constructor125 126  Test6(Test6 &&rhs);127  // CHECK-NOTES: :[[@LINE-1]]:3: note: move constructor declared here128private:129  Test6(const Test6 &rhs);130};131 132// Do not dereference a null BaseType.133template <class _Callable> class result_of;134template <class _Fp, class ..._Args> class result_of<_Fp(_Args...)> { };135template <class _Tp> using result_of_t = typename result_of<_Tp>::type;136 137template <class... _Types> struct __overload;138template <class _Tp, class... _Types>139struct __overload<_Tp, _Types...> : __overload<_Types...> {140  using __overload<_Types...>::operator();141};142 143template <class _Tp, class... _Types>144using __best_match_t = typename result_of_t<__overload<_Types...>(_Tp&&)>::type;145 146template <class... _Types>147class variant {148public:149  template <class _Arg, class _Tp = __best_match_t<_Arg, _Types...> >150  constexpr variant(_Arg&& __arg) {}151  // CHECK-NOTES: :[[@LINE-1]]:13: warning: constructor accepting a forwarding reference can hide the copy and move constructors152};153 154namespace std {155template <class T, class U> struct is_same { static constexpr bool value = false; };156template <class T> struct is_same<T, T> { static constexpr bool value = true; };157template <class T, class U> constexpr bool is_same_v = is_same<T, U>::value;158template <class T> struct remove_reference { using type = T; };159template <class T> struct remove_reference<T&> { using type = T; };160template <class T> struct remove_reference<T&&> { using type = T; };161template <class T> using remove_reference_t = typename remove_reference<T>::type;162template <class T> struct remove_cv { using type = T; };163template <class T> struct remove_cv<const T> { using type = T; };164template <class T> struct remove_cv<volatile T> { using type = T; };165template <class T> struct remove_cv<const volatile T> { using type = T; };166template <class T> using remove_cv_t = typename remove_cv<T>::type;167template <class T> struct remove_cvref { using type = remove_cv_t<remove_reference_t<T>>; };168template <class T> using remove_cvref_t = typename remove_cvref<T>::type;169} // namespace std170 171// Handle enable_if when used as a non-type template parameter.172class Test7 {173public:174  // Guarded with enable_if.175  template <class T,176    typename std::enable_if_t<std::is_same_v<std::remove_cvref_t<T>, int>, int>::type = 0>177  Test7(T &&t);178 179  // Guarded with enable_if.180  template <class T,181    std::enable_if_t<182      !std::is_same_v<std::remove_cvref_t<T>, Test7>183      && !std::is_same_v<std::remove_cvref_t<T>, bool>, int> = true>184  Test7(T &&t);185 186  Test7(const Test7 &other) = default;187  Test7(Test7 &&other) = default;188};189 190// Handle enable_if when used as a non-type template parameter following191// a variadic template parameter pack.192class Test8 {193public:194  // Guarded with enable_if.195  template <class T, class... A,196    std::enable_if_t<197      !std::is_same_v<std::remove_cvref_t<T>, Test8>198      || (sizeof...(A) > 0)>* = nullptr>199  Test8(T &&t, A &&... a);200 201  Test8(const Test8 &other) = default;202  Test8(Test8 &&other) = default;203};204 205// Non-type template parameter failure cases.206class Test9 {207public:208  // Requires a default argument (such as a literal, implicit cast expression, etc.)209  template <class T,210    std::enable_if_t<std::is_same_v<std::remove_cvref_t<T>, bool>, int>>211  Test9(T &&t);212  // CHECK-NOTES: :[[@LINE-1]]:3: warning: constructor accepting a forwarding reference can hide the copy and move constructors213  // CHECK-NOTES: 240:3: note: copy constructor declared here214  // CHECK-NOTES: 241:3: note: move constructor declared here215 216  // Requires a default argument (such as a literal, implicit cast expression, etc.)217  template <class T,218    std::enable_if_t<std::is_same_v<std::remove_cvref_t<T>, long>>*>219  Test9(T &&t);220  // CHECK-NOTES: :[[@LINE-1]]:3: warning: constructor accepting a forwarding reference can hide the copy and move constructors221  // CHECK-NOTES: 240:3: note: copy constructor declared here222  // CHECK-NOTES: 241:3: note: move constructor declared here223 224  // Only std::enable_if or std::enable_if_t are supported225  template <class T,226    typename std::enable_if_nice<T>::type* = nullptr>227  Test9(T &&t);228  // CHECK-NOTES: :[[@LINE-1]]:3: warning: constructor accepting a forwarding reference can hide the copy and move constructors229  // CHECK-NOTES: 240:3: note: copy constructor declared here230  // CHECK-NOTES: 241:3: note: move constructor declared here231 232  // Only std::enable_if or std::enable_if_t are supported233  template <class T,234    typename foo::enable_if<T>::type = 0>235  Test9(T &&t);236  // CHECK-NOTES: :[[@LINE-1]]:3: warning: constructor accepting a forwarding reference can hide the copy and move constructors237  // CHECK-NOTES: 240:3: note: copy constructor declared here238  // CHECK-NOTES: 241:3: note: move constructor declared here239 240  Test9(const Test9 &other) = default;241  Test9(Test9 &&other) = default;242};243 244 245template <typename T>246class Test10 {247public:248  enum E {};249  E e;250 251  Test10(T &&Item, E e)252      : e(e){}253};254 255// A deleted ctor cannot hide anything256class Test11 {257public:258  template <typename T>259  Test11(T&&) = delete;260 261  Test11(const Test11 &) = default;262  Test11(Test11 &&) = default;263};264 265template <template <class> typename T, typename U>266struct gh106333267{268    gh106333(U && arg1, T<int> arg2) {}269};270