modernize-use-equals-default-copy.cpp
13.4 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// RUN: %check_clang_tidy %s modernize-use-equals-default %t -- \
// RUN: -config="{CheckOptions: [{key: modernize-use-equals-default.IgnoreMacros, value: 0}]}" \
// RUN: -- -fno-delayed-template-parsing -fexceptions
// Out of line definition.
struct OL {
OL(const OL &);
OL &operator=(const OL &);
int Field;
};
OL::OL(const OL &Other) : Field(Other.Field) {}
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use '= default' to define a trivial copy constructor [modernize-use-equals-default]
// CHECK-FIXES: OL::OL(const OL &Other) = default;
OL &OL::operator=(const OL &Other) {
Field = Other.Field;
return *this;
}
// CHECK-MESSAGES: :[[@LINE-4]]:9: warning: use '= default' to define a trivial copy-assignment operator [modernize-use-equals-default]
// CHECK-FIXES: OL &OL::operator=(const OL &Other) = default;
// Inline.
struct IL {
IL(const IL &Other) : Field(Other.Field) {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
// CHECK-FIXES: IL(const IL &Other) = default;
IL &operator=(const IL &Other) {
Field = Other.Field;
return *this;
}
// CHECK-MESSAGES: :[[@LINE-4]]:7: warning: use '= default'
// CHECK-FIXES: IL &operator=(const IL &Other) = default;
int Field;
};
// Wrong type.
struct WT {
WT(const IL &Other) {}
WT &operator=(const IL &);
};
WT &WT::operator=(const IL &Other) { return *this; }
// Qualifiers.
struct Qual {
Qual(const Qual &Other) : Field(Other.Field), Volatile(Other.Volatile),
Mutable(Other.Mutable), Reference(Other.Reference),
Const(Other.Const) {}
// CHECK-MESSAGES: :[[@LINE-3]]:3: warning: use '= default'
// CHECK-FIXES: Qual(const Qual &Other)
// CHECK-FIXES: = default;
int Field;
volatile char Volatile;
mutable bool Mutable;
const OL &Reference; // This makes this class non-assignable.
const IL Const; // This also makes this class non-assignable.
static int Static;
};
// Wrong init arguments.
struct WI {
WI(const WI &Other) : Field1(Other.Field1), Field2(Other.Field1) {}
WI &operator=(const WI &);
int Field1, Field2;
};
WI &WI::operator=(const WI &Other) {
Field1 = Other.Field1;
Field2 = Other.Field1;
return *this;
}
// Missing field.
struct MF {
MF(const MF &Other) : Field1(Other.Field1), Field2(Other.Field2) {}
MF &operator=(const MF &);
int Field1, Field2, Field3;
};
MF &MF::operator=(const MF &Other) {
Field1 = Other.Field1;
Field2 = Other.Field2;
return *this;
}
struct Comments {
Comments(const Comments &Other)
/* don't delete */ : /* this comment */ Field(Other.Field) {}
// CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use '= default'
// CHECK-FIXES: /* don't delete */ = default;
int Field;
};
struct MoreComments {
MoreComments(const MoreComments &Other) /* this comment is OK */
: Field(Other.Field) {}
// CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use '= default'
// CHECK-FIXES: MoreComments(const MoreComments &Other) /* this comment is OK */
// CHECK-FIXES-NEXT: = default;
int Field;
};
struct ColonInComment {
ColonInComment(const ColonInComment &Other) /* : */ : Field(Other.Field) {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
// CHECK-FIXES: ColonInComment(const ColonInComment &Other) /* : */ = default;
int Field;
};
// No members or bases (in particular, no colon).
struct Empty {
Empty(const Empty &Other) {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
// CHECK-FIXES: Empty(const Empty &Other) = default;
Empty &operator=(const Empty &);
};
Empty &Empty::operator=(const Empty &Other) { return *this; }
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use '= default'
// CHECK-FIXES: Empty &Empty::operator=(const Empty &Other) = default;
// Bit fields.
struct BF {
BF() = default;
BF(const BF &Other) : Field1(Other.Field1), Field2(Other.Field2), Field3(Other.Field3),
Field4(Other.Field4) {};
// CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use '= default'
// CHECK-FIXES: BF(const BF &Other) {{$}}
// CHECK-FIXES: = default;
BF &operator=(const BF &);
unsigned Field1 : 3;
int : 7;
char Field2 : 6;
int : 0;
int Field3 : 24;
unsigned char Field4;
};
BF &BF::operator=(const BF &Other) {
Field1 = Other.Field1;
Field2 = Other.Field2;
Field3 = Other.Field3;
Field4 = Other.Field4;
return *this;
}
// CHECK-MESSAGES: :[[@LINE-7]]:9: warning: use '= default'
// CHECK-FIXES: BF &BF::operator=(const BF &Other) = default;
// Base classes.
struct BC : IL, OL, BF {
BC(const BC &Other) : IL(Other), OL(Other), BF(Other) {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
// CHECK-FIXES: BC(const BC &Other) = default;
BC &operator=(const BC &Other);
};
BC &BC::operator=(const BC &Other) {
IL::operator=(Other);
OL::operator=(Other);
BF::operator=(Other);
return *this;
}
// CHECK-MESSAGES: :[[@LINE-6]]:9: warning: use '= default'
// CHECK-FIXES: BC &BC::operator=(const BC &Other) = default;
// Base classes with member.
struct BCWM : IL, OL {
BCWM(const BCWM &Other) : IL(Other), OL(Other), Bf(Other.Bf) {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
// CHECK-FIXES: BCWM(const BCWM &Other) = default;
BCWM &operator=(const BCWM &);
BF Bf;
};
BCWM &BCWM::operator=(const BCWM &Other) {
IL::operator=(Other);
OL::operator=(Other);
Bf = Other.Bf;
return *this;
}
// CHECK-MESSAGES: :[[@LINE-6]]:13: warning: use '= default'
// CHECK-FIXES: BCWM &BCWM::operator=(const BCWM &Other) = default;
// Missing base class.
struct MBC : IL, OL, BF {
MBC(const MBC &Other) : IL(Other), OL(Other) {}
MBC &operator=(const MBC &);
};
MBC &MBC::operator=(const MBC &Other) {
IL::operator=(Other);
OL::operator=(Other);
return *this;
}
// Base classes, incorrect parameter.
struct BCIP : BCWM, BF {
BCIP(const BCIP &Other) : BCWM(Other), BF(Other.Bf) {}
BCIP &operator=(const BCIP &);
};
BCIP &BCIP::operator=(const BCIP &Other) {
BCWM::operator=(Other);
BF::operator=(Other.Bf);
return *this;
}
// Virtual base classes.
struct VA : virtual OL {};
struct VB : virtual OL {};
struct VBC : VA, VB, virtual OL {
// OL is the first thing that is going to be initialized, despite the fact
// that it is the last in the list of bases, because it is virtual and there
// is a virtual OL at the beginning of VA (which is the same).
VBC(const VBC &Other) : OL(Other), VA(Other), VB(Other) {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
// CHECK-FIXES: VBC(const VBC &Other) = default;
VBC &operator=(const VBC &Other);
};
VBC &VBC::operator=(const VBC &Other) {
OL::operator=(Other);
VA::operator=(Other);
VB::operator=(Other);
return *this;
}
// CHECK-MESSAGES: :[[@LINE-6]]:11: warning: use '= default'
// CHECK-FIXES: VBC &VBC::operator=(const VBC &Other) = default;
// Indirect base.
struct IB : VBC {
IB(const IB &Other) : OL(Other), VBC(Other) {}
IB &operator=(const IB &);
};
IB &IB::operator=(const IB &Other) {
OL::operator=(Other);
VBC::operator=(Other);
return *this;
}
// Class template.
template <class T>
struct Template {
Template() = default;
Template(const Template &Other) : Field(Other.Field) {}
Template &operator=(const Template &Other);
void foo(const T &t);
int Field;
};
template <class T>
Template<T> &Template<T>::operator=(const Template<T> &Other) {
Field = Other.Field;
return *this;
}
Template<int> T1;
// Dependent types.
template <class T>
struct DT1 {
DT1() = default;
DT1(const DT1 &Other) : Field(Other.Field) {}
DT1 &operator=(const DT1 &);
T Field;
};
template <class T>
DT1<T> &DT1<T>::operator=(const DT1<T> &Other) {
Field = Other.Field;
return *this;
}
DT1<int> Dt1;
template <class T>
struct DT2 {
DT2() = default;
DT2(const DT2 &Other) : Field(Other.Field), Dependent(Other.Dependent) {}
DT2 &operator=(const DT2 &);
T Field;
typename T::TT Dependent;
};
template <class T>
DT2<T> &DT2<T>::operator=(const DT2<T> &Other) {
Field = Other.Field;
Dependent = Other.Dependent;
return *this;
}
struct T {
typedef int TT;
};
DT2<T> Dt2;
// Default arguments.
struct DA {
DA(int Int);
DA(const DA &Other = DA(0)) : Field1(Other.Field1), Field2(Other.Field2) {}
DA &operator=(const DA &);
int Field1;
char Field2;
};
// Overloaded operator= cannot have a default argument.
DA &DA::operator=(const DA &Other) {
Field1 = Other.Field1;
Field2 = Other.Field2;
return *this;
}
// CHECK-MESSAGES: :[[@LINE-5]]:9: warning: use '= default'
// CHECK-FIXES: DA &DA::operator=(const DA &Other) = default;
struct DA2 {
// Can be used as copy-constructor but cannot be explicitly defaulted.
DA2(const DA &Other, int Def = 0) {}
};
// Default initialization.
struct DI {
DI(const DI &Other) : Field1(Other.Field1), Field2(Other.Field2) {}
int Field1;
int Field2 = 0;
int Fiedl3;
};
// Statement inside body.
void foo();
struct SIB {
SIB(const SIB &Other) : Field(Other.Field) { foo(); }
SIB &operator=(const SIB &);
int Field;
};
SIB &SIB::operator=(const SIB &Other) {
Field = Other.Field;
foo();
return *this;
}
// Comment inside body.
struct CIB {
CIB(const CIB &Other) : Field(Other.Field) { /* Don't erase this */
}
// CHECK-MESSAGES: :[[@LINE-2]]:3: warning: use '= default'
CIB &operator=(const CIB &);
int Field;
};
CIB &CIB::operator=(const CIB &Other) {
Field = Other.Field;
// FIXME: don't erase this comment.
return *this;
}
// CHECK-MESSAGES: :[[@LINE-5]]:11: warning: use '= default'
// CHECK-FIXES: CIB &CIB::operator=(const CIB &Other) = default;
// Take non-const reference as argument.
struct NCRef {
NCRef(NCRef &Other) : Field1(Other.Field1), Field2(Other.Field2) {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default'
// CHECK-FIXES: NCRef(NCRef &Other) = default;
NCRef &operator=(NCRef &);
int Field1, Field2;
};
NCRef &NCRef::operator=(NCRef &Other) {
Field1 = Other.Field1;
Field2 = Other.Field2;
return *this;
}
// CHECK-MESSAGES: :[[@LINE-5]]:15: warning: use '= default'
// CHECK-FIXES: NCRef &NCRef::operator=(NCRef &Other) = default;
// Already defaulted.
struct IAD {
IAD(const IAD &Other) = default;
IAD &operator=(const IAD &Other) = default;
};
struct OAD {
OAD(const OAD &Other);
OAD &operator=(const OAD &);
};
OAD::OAD(const OAD &Other) = default;
OAD &OAD::operator=(const OAD &Other) = default;
// Deleted.
struct ID {
ID(const ID &Other) = delete;
ID &operator=(const ID &Other) = delete;
};
// Non-reference parameter.
struct NRef {
NRef &operator=(NRef Other);
int Field1;
};
NRef &NRef::operator=(NRef Other) {
Field1 = Other.Field1;
return *this;
}
// RValue reference parameter.
struct RVR {
RVR(RVR &&Other) {}
RVR &operator=(RVR &&);
};
RVR &RVR::operator=(RVR &&Other) { return *this; }
// Similar function.
struct SF {
SF &foo(const SF &);
int Field1;
};
SF &SF::foo(const SF &Other) {
Field1 = Other.Field1;
return *this;
}
// No return.
struct NR {
NR &operator=(const NR &);
};
NR &NR::operator=(const NR &Other) {}
// Return misplaced.
struct RM {
RM &operator=(const RM &);
int Field;
};
RM &RM::operator=(const RM &Other) {
return *this;
Field = Other.Field;
}
// Wrong return value.
struct WRV {
WRV &operator=(WRV &);
};
WRV &WRV::operator=(WRV &Other) {
return Other;
}
// Wrong return type.
struct WRT : IL {
IL &operator=(const WRT &);
};
IL &WRT::operator=(const WRT &Other) {
return *this;
}
// Try-catch.
struct ITC {
ITC(const ITC &Other)
try : Field(Other.Field) {
} catch (...) {
}
ITC &operator=(const ITC &Other) try {
Field = Other.Field;
} catch (...) {
}
int Field;
};
struct OTC {
OTC(const OTC &);
OTC &operator=(const OTC &);
int Field;
};
OTC::OTC(const OTC &Other) try : Field(Other.Field) {
} catch (...) {
}
OTC &OTC::operator=(const OTC &Other) try {
Field = Other.Field;
} catch (...) {
}
// FIXME: the check is not able to detect exception specification.
// noexcept(true).
struct NET {
// This is the default.
//NET(const NET &Other) noexcept {}
NET &operator=(const NET &Other) noexcept;
};
//NET &NET::operator=(const NET &Other) noexcept { return *this; }
// noexcept(false).
struct NEF {
// This is the default.
//NEF(const NEF &Other) noexcept(false) {}
NEF &operator=(const NEF &Other) noexcept(false);
};
//NEF &NEF::operator=(const NEF &Other) noexcept(false) { return *this; }
#define STRUCT_WITH_COPY_CONSTRUCT(_base, _type) \
struct _type { \
_type(const _type &v) : value(v.value) {} \
_base value; \
};
STRUCT_WITH_COPY_CONSTRUCT(unsigned char, Hex8CopyConstruct)
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use '= default' to define a trivial copy constructor
// CHECK-MESSAGES: :[[@LINE-6]]:44: note:
#define STRUCT_WITH_COPY_ASSIGN(_base, _type) \
struct _type { \
_type &operator=(const _type &rhs) { \
value = rhs.value; \
return *this; \
} \
_base value; \
};
STRUCT_WITH_COPY_ASSIGN(unsigned char, Hex8CopyAssign)
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use '= default' to define a trivial copy-assignment operator
// CHECK-MESSAGES: :[[@LINE-9]]:40: note:
// Use of braces
struct UOB{
UOB(const UOB &Other):j{Other.j}{}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use '= default' to define a trivial copy constructor [modernize-use-equals-default]
// CHECK-FIXES: UOB(const UOB &Other)= default;
int j;
};