File: noexcept-move-constructor.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (381 lines) | stat: -rw-r--r-- 11,563 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// RUN: %check_clang_tidy %s performance-noexcept-move-constructor %t -- -- -fexceptions

struct Empty
{};

struct IntWrapper {
  int value;
};

template <typename T>
struct FalseT {
  static constexpr bool value = false;
};

template <typename T>
struct TrueT {
  static constexpr bool value = true;
};

struct ThrowOnAnything {
  ThrowOnAnything() noexcept(false);
  ThrowOnAnything(ThrowOnAnything&&) noexcept(false);
  ThrowOnAnything& operator=(ThrowOnAnything &&) noexcept(false);
  ~ThrowOnAnything() noexcept(false);
};

class A {
  A(A &&);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept [performance-noexcept-move-constructor]
  // CHECK-FIXES: A(A &&) noexcept ;
  A &operator=(A &&);
  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: move assignment operators should be marked noexcept [performance-noexcept-move-constructor]
  // CHECK-FIXES: A &operator=(A &&) noexcept ;
};

struct B {
  static constexpr bool kFalse = false;
  B(B &&) noexcept(kFalse);
  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor]
  B &operator=(B &&) noexcept(kFalse);
  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor]
};

template <typename>
struct C {
  C(C &&);
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept [performance-noexcept-move-constructor]
  // CHECK-FIXES: C(C &&) noexcept ;
  C& operator=(C &&);
  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: move assignment operators should be marked noexcept [performance-noexcept-move-constructor]
  // CHECK-FIXES: C& operator=(C &&) noexcept ;
};

struct D {
  static constexpr bool kFalse = false;
  D(D &&) noexcept(kFalse) = default;
  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor]
  D& operator=(D &&) noexcept(kFalse) = default;
  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor]
};

template <typename>
struct E {
  static constexpr bool kFalse = false;
  E(E &&) noexcept(kFalse);
  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor]
  E& operator=(E &&) noexcept(kFalse);
  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: noexcept specifier on the move assignment operator evaluates to 'false'
};

template <typename>
struct F {
  static constexpr bool kFalse = false;
  F(F &&) noexcept(kFalse) = default;
  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor]
  F& operator=(F &&) noexcept(kFalse) = default;
  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor]
};

struct G {
  G(G &&) = default;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept [performance-noexcept-move-constructor]
  // CHECK-FIXES: G(G &&)  noexcept = default;
  G& operator=(G &&) = default;
  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: move assignment operators should be marked noexcept [performance-noexcept-move-constructor]
  // CHECK-FIXES: G& operator=(G &&)  noexcept = default;

  ThrowOnAnything field;
};

void throwing_function() noexcept(false) {}

struct H {
  H(H &&) noexcept(noexcept(throwing_function()));
  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor]
  H &operator=(H &&) noexcept(noexcept(throwing_function()));
  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor]
};

template <typename>
struct I {
  I(I &&) noexcept(noexcept(throwing_function()));
  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor]
  I &operator=(I &&) noexcept(noexcept(throwing_function()));
  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor]
};

template <typename T> struct TemplatedType {
  static void f() {}
};

template <> struct TemplatedType<int> {
  static void f() noexcept {}
};

struct J {
  J(J &&) noexcept(noexcept(TemplatedType<double>::f()));
  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: noexcept specifier on the move constructor evaluates to 'false' [performance-noexcept-move-constructor]
  J &operator=(J &&) noexcept(noexcept(TemplatedType<double>::f()));
  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: noexcept specifier on the move assignment operator evaluates to 'false' [performance-noexcept-move-constructor]
};

struct K : public ThrowOnAnything {
  K(K &&) = default;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept [performance-noexcept-move-constructor]
  // CHECK-FIXES: K(K &&)  noexcept = default;
  K &operator=(K &&) = default;
  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: move assignment operators should be marked noexcept [performance-noexcept-move-constructor]
  // CHECK-FIXES: K &operator=(K &&)  noexcept = default;
};

struct InheritFromThrowOnAnything : public ThrowOnAnything
{};

struct L {
  L(L &&) = default;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept [performance-noexcept-move-constructor]
  // CHECK-FIXES: L(L &&)  noexcept = default;
  L &operator=(L &&) = default;
  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: move assignment operators should be marked noexcept [performance-noexcept-move-constructor]
  // CHECK-FIXES: L &operator=(L &&)  noexcept = default;

  InheritFromThrowOnAnything IFF;
};

struct M : public InheritFromThrowOnAnything {
  M(M &&) = default;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept [performance-noexcept-move-constructor]
  // CHECK-FIXES: M(M &&)  noexcept = default;
  M &operator=(M &&) = default;
  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: move assignment operators should be marked noexcept [performance-noexcept-move-constructor]
  // CHECK-FIXES: M &operator=(M &&)  noexcept = default;
};

struct N : public IntWrapper, ThrowOnAnything {
  N(N &&) = default;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept [performance-noexcept-move-constructor]
  // CHECK-FIXES: N(N &&)  noexcept = default;
  N &operator=(N &&) = default;
  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: move assignment operators should be marked noexcept [performance-noexcept-move-constructor]
  // CHECK-FIXES: N &operator=(N &&)  noexcept = default;
};

struct O : virtual IntWrapper, ThrowOnAnything {
  O(O &&) = default;
  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: move constructors should be marked noexcept [performance-noexcept-move-constructor]
  // CHECK-FIXES: O(O &&)  noexcept = default;
  O &operator=(O &&) = default;
  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: move assignment operators should be marked noexcept [performance-noexcept-move-constructor]
  // CHECK-FIXES: O &operator=(O &&)  noexcept = default;
};

class OK {};

void f() {
  OK a;
  a = OK();
}

struct OK1 {
  OK1(const OK1 &);
  OK1(OK1 &&) noexcept;
  OK1 &operator=(OK1 &&) noexcept;
  void f();
  void g() noexcept;
};

struct OK2 {
  static constexpr bool kTrue = true;

  OK2(OK2 &&) noexcept(true) {}
  OK2 &operator=(OK2 &&) noexcept(kTrue) { return *this; }
};

struct OK4 {
  OK4(OK4 &&) noexcept(false) {}
  OK4 &operator=(OK4 &&) = delete;
};

struct OK3 {
  OK3(OK3 &&) noexcept = default;
  OK3 &operator=(OK3 &&) noexcept = default;
};

struct OK5 {
  OK5(OK5 &&) noexcept(true) = default;
  OK5 &operator=(OK5 &&) noexcept(true) = default;
};

struct OK6 {
  OK6(OK6 &&) = default;
  OK6& operator=(OK6 &&) = default;
};

template <typename>
struct OK7 {
  OK7(OK7 &&) = default;
  OK7& operator=(OK7 &&) = default;
};

template <typename>
struct OK8 {
  OK8(OK8 &&) noexcept = default;
  OK8& operator=(OK8 &&) noexcept = default;
};

template <typename>
struct OK9 {
  OK9(OK9 &&) noexcept(true) = default;
  OK9& operator=(OK9 &&) noexcept(true) = default;
};

template <typename>
struct OK10 {
  OK10(OK10 &&) noexcept(false) = default;
  OK10& operator=(OK10 &&) noexcept(false) = default;
};

template <typename>
struct OK11 {
  OK11(OK11 &&) = delete;
  OK11& operator=(OK11 &&) = delete;
};

void noexcept_function() noexcept {}

struct OK12 {
  OK12(OK12 &&) noexcept(noexcept(noexcept_function()));
  OK12 &operator=(OK12 &&) noexcept(noexcept(noexcept_function));
};

struct OK13 {
  OK13(OK13 &&) noexcept(noexcept(noexcept_function)) = default;
  OK13 &operator=(OK13 &&) noexcept(noexcept(noexcept_function)) = default;
};

template <typename>
struct OK14 {
  OK14(OK14 &&) noexcept(noexcept(TemplatedType<int>::f()));
  OK14 &operator=(OK14 &&) noexcept(noexcept(TemplatedType<int>::f()));
};

struct OK15 {
  OK15(OK15 &&) = default;
  OK15 &operator=(OK15 &&) = default;

  int member;
};

template <typename>
struct OK16 {
  OK16(OK16 &&) = default;
  OK16 &operator=(OK16 &&) = default;

  int member;
};

struct OK17 {
  OK17(OK17 &&) = default;
  OK17 &operator=(OK17 &&) = default;

  OK empty_field;
};

template <typename>
struct OK18 {
  OK18(OK18 &&) = default;
  OK18 &operator=(OK18 &&) = default;

  OK empty_field;
};

struct OK19 : public OK {
  OK19(OK19 &&) = default;
  OK19 &operator=(OK19 &&) = default;
};

struct OK20 : virtual OK {
  OK20(OK20 &&) = default;
  OK20 &operator=(OK20 &&) = default;
};

template <typename T>
struct OK21 : public T {
  OK21() = default;
  OK21(OK21 &&) = default;
  OK21 &operator=(OK21 &&) = default;
};

template <typename T>
struct OK22 : virtual T {
  OK22() = default;
  OK22(OK22 &&) = default;
  OK22 &operator=(OK22 &&) = default;
};

template <typename T>
struct OK23 {
  OK23() = default;
  OK23(OK23 &&) = default;
  OK23 &operator=(OK23 &&) = default;

  T member;
};

void testTemplates() {
  OK21<Empty> value(OK21<Empty>{});
  value = OK21<Empty>{};

  OK22<Empty> value2{OK22<Empty>{}};
  value2 = OK22<Empty>{};

  OK23<Empty> value3{OK23<Empty>{}};
  value3 =OK23<Empty>{};
}

struct OK24 : public Empty, OK1 {
  OK24(OK24 &&) = default;
  OK24 &operator=(OK24 &&) = default;
};

struct OK25 : virtual Empty, OK1 {
  OK25(OK25 &&) = default;
  OK25 &operator=(OK25 &&) = default;
};

struct OK26 : public Empty, IntWrapper {
  OK26(OK26 &&) = default;
  OK26 &operator=(OK26 &&) = default;
};

template <typename T>
struct OK27 : public T {
  OK27(OK27 &&) = default;
  OK27 &operator=(OK27 &&) = default;
};

template <typename T>
struct OK28 : virtual T {
  OK28(OK28 &&) = default;
  OK28 &operator=(OK28 &&) = default;
};

template <typename T>
struct OK29 {
  OK29(OK29 &&) = default;
  OK29 &operator=(OK29 &&) = default;

  T member;
};

struct OK30 {
  OK30(OK30 &&) noexcept(TrueT<OK30>::value) = default;
  OK30& operator=(OK30 &&) noexcept(TrueT<OK30>::value) = default;
};

template <typename>
struct OK31 {
  OK31(OK31 &&) noexcept(TrueT<int>::value) = default;
  OK31& operator=(OK31 &&) noexcept(TrueT<int>::value) = default;
};