| 1 | // RUN: %check_clang_tidy %s bugprone-forwarding-reference-overload %t |
| 2 | |
| 3 | namespace std { |
| 4 | template <bool B, class T = void> struct enable_if { typedef T type; }; |
| 5 | |
| 6 | template <class T> struct enable_if<true, T> { typedef T type; }; |
| 7 | |
| 8 | template <bool B, class T = void> |
| 9 | using enable_if_t = typename enable_if<B, T>::type; |
| 10 | |
| 11 | template <class T> struct enable_if_nice { typedef T type; }; |
| 12 | } // namespace std |
| 13 | |
| 14 | namespace foo { |
| 15 | template <class T> struct enable_if { typedef T type; }; |
| 16 | } // namespace foo |
| 17 | |
| 18 | template <typename T> constexpr bool just_true = true; |
| 19 | |
| 20 | class Test1 { |
| 21 | public: |
| 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 here |
| 25 | // CHECK-NOTES: 49:3: note: copy constructor declared here |
| 26 | // CHECK-NOTES: 50:3: note: move constructor declared here |
| 27 | |
| 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 constructors |
| 30 | // CHECK-NOTES: 48:3: note: copy constructor declared here |
| 31 | // CHECK-NOTES: 49:3: note: copy constructor declared here |
| 32 | // CHECK-NOTES: 50:3: note: move constructor declared here |
| 33 | |
| 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 constructors |
| 37 | // CHECK-NOTES: 48:3: note: copy constructor declared here |
| 38 | // CHECK-NOTES: 49:3: note: copy constructor declared here |
| 39 | // CHECK-NOTES: 50:3: note: move constructor declared here |
| 40 | |
| 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 constructors |
| 44 | // CHECK-NOTES: 48:3: note: copy constructor declared here |
| 45 | // CHECK-NOTES: 49:3: note: copy constructor declared here |
| 46 | // CHECK-NOTES: 50:3: note: move constructor declared here |
| 47 | |
| 48 | Test1(const Test1 &other) {} |
| 49 | Test1(Test1 &other) {} |
| 50 | Test1(Test1 &&other) {} |
| 51 | }; |
| 52 | |
| 53 | template <typename U> class Test2 { |
| 54 | public: |
| 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. |
| 90 | class Test3 { |
| 91 | public: |
| 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 | |
| 98 | private: |
| 99 | Test3(Test3 &&rhs); |
| 100 | }; |
| 101 | |
| 102 | // Both the copy and the (compiler generated) move constructors can be hidden. |
| 103 | class Test4 { |
| 104 | public: |
| 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 constructors |
| 107 | |
| 108 | Test4(const Test4 &rhs); |
| 109 | // CHECK-NOTES: :[[@LINE-1]]:3: note: copy constructor declared here |
| 110 | }; |
| 111 | |
| 112 | // Nothing can be hidden, the copy constructor is implicitly deleted. |
| 113 | class Test5 { |
| 114 | public: |
| 115 | template <typename T> Test5(T &&n); |
| 116 | |
| 117 | Test5(Test5 &&rhs) = delete; |
| 118 | }; |
| 119 | |
| 120 | // Only the move constructor can be hidden. |
| 121 | class Test6 { |
| 122 | public: |
| 123 | template <typename T> Test6(T &&n); |
| 124 | // CHECK-NOTES: :[[@LINE-1]]:25: warning: constructor accepting a forwarding reference can hide the move constructor |
| 125 | |
| 126 | Test6(Test6 &&rhs); |
| 127 | // CHECK-NOTES: :[[@LINE-1]]:3: note: move constructor declared here |
| 128 | private: |
| 129 | Test6(const Test6 &rhs); |
| 130 | }; |
| 131 | |
| 132 | // Do not dereference a null BaseType. |
| 133 | template <class _Callable> class result_of; |
| 134 | template <class _Fp, class ..._Args> class result_of<_Fp(_Args...)> { }; |
| 135 | template <class _Tp> using result_of_t = typename result_of<_Tp>::type; |
| 136 | |
| 137 | template <class... _Types> struct __overload; |
| 138 | template <class _Tp, class... _Types> |
| 139 | struct __overload<_Tp, _Types...> : __overload<_Types...> { |
| 140 | using __overload<_Types...>::operator(); |
| 141 | }; |
| 142 | |
| 143 | template <class _Tp, class... _Types> |
| 144 | using __best_match_t = typename result_of_t<__overload<_Types...>(_Tp&&)>::type; |
| 145 | |
| 146 | template <class... _Types> |
| 147 | class variant { |
| 148 | public: |
| 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 constructors |
| 152 | }; |
| 153 | |
| 154 | namespace std { |
| 155 | template <class T, class U> struct is_same { static constexpr bool value = false; }; |
| 156 | template <class T> struct is_same<T, T> { static constexpr bool value = true; }; |
| 157 | template <class T, class U> constexpr bool is_same_v = is_same<T, U>::value; |
| 158 | template <class T> struct remove_reference { using type = T; }; |
| 159 | template <class T> struct remove_reference<T&> { using type = T; }; |
| 160 | template <class T> struct remove_reference<T&&> { using type = T; }; |
| 161 | template <class T> using remove_reference_t = typename remove_reference<T>::type; |
| 162 | template <class T> struct remove_cv { using type = T; }; |
| 163 | template <class T> struct remove_cv<const T> { using type = T; }; |
| 164 | template <class T> struct remove_cv<volatile T> { using type = T; }; |
| 165 | template <class T> struct remove_cv<const volatile T> { using type = T; }; |
| 166 | template <class T> using remove_cv_t = typename remove_cv<T>::type; |
| 167 | template <class T> struct remove_cvref { using type = remove_cv_t<remove_reference_t<T>>; }; |
| 168 | template <class T> using remove_cvref_t = typename remove_cvref<T>::type; |
| 169 | } // namespace std |
| 170 | |
| 171 | // Handle enable_if when used as a non-type template parameter. |
| 172 | class Test7 { |
| 173 | public: |
| 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 following |
| 191 | // a variadic template parameter pack. |
| 192 | class Test8 { |
| 193 | public: |
| 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. |
| 206 | class Test9 { |
| 207 | public: |
| 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 constructors |
| 213 | // CHECK-NOTES: 240:3: note: copy constructor declared here |
| 214 | // CHECK-NOTES: 241:3: note: move constructor declared here |
| 215 | |
| 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 constructors |
| 221 | // CHECK-NOTES: 240:3: note: copy constructor declared here |
| 222 | // CHECK-NOTES: 241:3: note: move constructor declared here |
| 223 | |
| 224 | // Only std::enable_if or std::enable_if_t are supported |
| 225 | 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 constructors |
| 229 | // CHECK-NOTES: 240:3: note: copy constructor declared here |
| 230 | // CHECK-NOTES: 241:3: note: move constructor declared here |
| 231 | |
| 232 | // Only std::enable_if or std::enable_if_t are supported |
| 233 | 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 constructors |
| 237 | // CHECK-NOTES: 240:3: note: copy constructor declared here |
| 238 | // CHECK-NOTES: 241:3: note: move constructor declared here |
| 239 | |
| 240 | Test9(const Test9 &other) = default; |
| 241 | Test9(Test9 &&other) = default; |
| 242 | }; |
| 243 | |
| 244 | |
| 245 | template <typename T> |
| 246 | class Test10 { |
| 247 | public: |
| 248 | enum E {}; |
| 249 | E e; |
| 250 | |
| 251 | Test10(T &&Item, E e) |
| 252 | : e(e){} |
| 253 | }; |
| 254 | |
| 255 | // A deleted ctor cannot hide anything |
| 256 | class Test11 { |
| 257 | public: |
| 258 | template <typename T> |
| 259 | Test11(T&&) = delete; |
| 260 | |
| 261 | Test11(const Test11 &) = default; |
| 262 | Test11(Test11 &&) = default; |
| 263 | }; |
| 264 | |
| 265 | template <template <class> typename T, typename U> |
| 266 | struct gh106333 |
| 267 | { |
| 268 | gh106333(U && arg1, T<int> arg2) {} |
| 269 | }; |
| 270 | |