modernize-use-default-member-init.cpp
15.7 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
// RUN: %check_clang_tidy -std=c++11,c++14,c++17 %s modernize-use-default-member-init %t -- -- -fexceptions
// FIXME: Fix the checker to work in C++20 mode.
struct S {
};
struct PositiveValueChar {
PositiveValueChar() : c0(), c1()/*, c2(), c3()*/ {}
// CHECK-FIXES: PositiveValueChar() {}
const char c0;
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use default member initializer for 'c0' [modernize-use-default-member-init]
// CHECK-FIXES: const char c0{};
wchar_t c1;
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use default member initializer for 'c1'
// CHECK-FIXES: wchar_t c1{};
// FIXME: char16_t c2;
// C HECK-MESSAGES: :[[@LINE-1]]:12: warning: use default member initializer for 'c2'
// C HECK-FIXES: char16_t c2{};
// FIXME: char32_t c3;
// C HECK-MESSAGES: :[[@LINE-1]]:12: warning: use default member initializer for 'c3'
// C HECK-FIXES: char32_t c3{};
};
struct PositiveChar {
PositiveChar() : d('a') {}
// CHECK-FIXES: PositiveChar() {}
char d;
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use default member initializer for 'd'
// CHECK-FIXES: char d{'a'};
};
struct PositiveValueInt {
PositiveValueInt() : i() {}
// CHECK-FIXES: PositiveValueInt() {}
const int i;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use default member initializer for 'i'
// CHECK-FIXES: const int i{};
};
struct PositiveInt {
PositiveInt() : j(1) {}
// CHECK-FIXES: PositiveInt() {}
int j;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'j'
// CHECK-FIXES: int j{1};
};
struct PositiveUnaryMinusInt {
PositiveUnaryMinusInt() : j(-1) {}
// CHECK-FIXES: PositiveUnaryMinusInt() {}
int j;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'j'
// CHECK-FIXES: int j{-1};
};
struct PositiveUnaryPlusInt {
PositiveUnaryPlusInt() : j(+1) {}
// CHECK-FIXES: PositiveUnaryPlusInt() {}
int j;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'j'
// CHECK-FIXES: int j{+1};
};
struct PositiveValueComplexInt {
PositiveValueComplexInt() : i() {}
// CHECK-FIXES: PositiveValueComplexInt() {}
_Complex int i;
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use default member initializer for 'i'
// CHECK-FIXES: _Complex int i{};
};
struct PositiveValueFloat {
PositiveValueFloat() : f() {}
// CHECK-FIXES: PositiveValueFloat() {}
float f;
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use default member initializer for 'f'
// CHECK-FIXES: float f{};
};
struct PositiveValueDouble {
PositiveValueDouble() : d() {}
// CHECK-FIXES: PositiveValueDouble() {}
double d;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'd'
// CHECK-FIXES: double d{};
};
struct PositiveDouble {
PositiveDouble() : f(2.5463e43) {}
// CHECK-FIXES: PositiveDouble() {}
double f;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'f'
// CHECK-FIXES: double f{2.5463e43};
};
struct PositiveValueComplexFloat {
PositiveValueComplexFloat() : f() {}
// CHECK-FIXES: PositiveValueComplexFloat() {}
_Complex float f;
// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use default member initializer for 'f'
// CHECK-FIXES: _Complex float f{};
};
struct PositiveValueComplexDouble {
PositiveValueComplexDouble() : f() {}
// CHECK-FIXES: PositiveValueComplexDouble() {}
_Complex double f;
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use default member initializer for 'f'
// CHECK-FIXES: _Complex double f{};
};
struct PositiveUnaryMinusDouble {
PositiveUnaryMinusDouble() : f(-2.5463e43) {}
// CHECK-FIXES: PositiveUnaryMinusDouble() {}
double f;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'f'
// CHECK-FIXES: double f{-2.5463e43};
};
struct PositiveUnaryPlusDouble {
PositiveUnaryPlusDouble() : f(+2.5463e43) {}
// CHECK-FIXES: PositiveUnaryPlusDouble() {}
double f;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'f'
// CHECK-FIXES: double f{+2.5463e43};
};
struct PositiveValueBool {
PositiveValueBool() : b() {}
// CHECK-FIXES: PositiveValueBool() {}
bool b;
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use default member initializer for 'b'
// CHECK-FIXES: bool b{};
};
struct PositiveBool {
PositiveBool() : a(true) {}
// CHECK-FIXES: PositiveBool() {}
bool a;
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use default member initializer for 'a'
// CHECK-FIXES: bool a{true};
};
struct PositiveValuePointer {
PositiveValuePointer() : p() {}
// CHECK-FIXES: PositiveValuePointer() {}
int *p;
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use default member initializer for 'p'
// CHECK-FIXES: int *p{};
};
struct PositiveNullPointer {
PositiveNullPointer() : q(nullptr) {}
// CHECK-FIXES: PositiveNullPointer() {}
int *q;
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use default member initializer for 'q'
// CHECK-FIXES: int *q{nullptr};
};
enum Enum { Foo, Bar };
struct PositiveEnum {
PositiveEnum() : e(Foo) {}
// CHECK-FIXES: PositiveEnum() {}
Enum e;
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use default member initializer for 'e'
// CHECK-FIXES: Enum e{Foo};
};
struct PositiveValueEnum {
PositiveValueEnum() : e() {}
// CHECK-FIXES: PositiveValueEnum() {}
Enum e;
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use default member initializer for 'e'
// CHECK-FIXES: Enum e{};
};
struct PositiveString {
PositiveString() : s("foo") {}
// CHECK-FIXES: PositiveString() {}
const char *s;
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use default member initializer for 's'
// CHECK-FIXES: const char *s{"foo"};
};
struct PositiveStruct {
PositiveStruct() : s(7) {}
// CHECK-FIXES: PositiveStruct() {}
struct {
int s;
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use default member initializer for 's'
// CHECK-FIXES: int s{7};
};
};
template <typename T>
struct NegativeTemplate {
NegativeTemplate() : t() {}
T t;
};
NegativeTemplate<int> nti;
NegativeTemplate<double> ntd;
struct NegativeDefaultMember {
NegativeDefaultMember() {}
int i = 2;
};
struct NegativeClass : S {
NegativeClass() : s() {}
S s;
};
struct NegativeBase : S {
NegativeBase() : S() {}
};
struct NegativeDefaultOtherMember{
NegativeDefaultOtherMember() : i(3) {}
int i = 4;
};
struct NegativeUnion {
NegativeUnion() : d(5.0) {}
union {
int i;
double d;
};
};
struct NegativeBitField
{
NegativeBitField() : i(6) {}
int i : 5;
};
struct NegativeNotDefaultInt
{
NegativeNotDefaultInt(int) : i(7) {}
int i;
};
struct NegativeDefaultArg
{
NegativeDefaultArg(int i = 4) : i(i) {}
int i;
};
struct ExistingChar {
ExistingChar(short) : e1(), e2{}, e3(), e4() {}
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: member initializer for 'e1' is redundant [modernize-use-default-member-init]
// CHECK-MESSAGES: :[[@LINE-2]]:31: warning: member initializer for 'e2' is redundant
// CHECK-MESSAGES: :[[@LINE-3]]:37: warning: member initializer for 'e3' is redundant
// CHECK-FIXES: ExistingChar(short) : e4() {}
ExistingChar(int) : e1(0), e2{0}, e3(0), e4(0) {}
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: member initializer for 'e1' is redundant
// CHECK-MESSAGES: :[[@LINE-2]]:30: warning: member initializer for 'e2' is redundant
// CHECK-MESSAGES: :[[@LINE-3]]:37: warning: member initializer for 'e3' is redundant
// CHECK-FIXES: ExistingChar(int) : e4(0) {}
ExistingChar(long) : e1('\0'), e2{'\0'}, e3('\0'), e4('\0') {}
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: member initializer for 'e1' is redundant
// CHECK-MESSAGES: :[[@LINE-2]]:34: warning: member initializer for 'e2' is redundant
// CHECK-MESSAGES: :[[@LINE-3]]:44: warning: member initializer for 'e3' is redundant
// CHECK-FIXES: ExistingChar(long) : e4('\0') {}
ExistingChar(char) : e1('a'), e2{'a'}, e3('a'), e4('a') {}
// CHECK-MESSAGES: :[[@LINE-1]]:51: warning: member initializer for 'e4' is redundant
// CHECK-FIXES: ExistingChar(char) : e1('a'), e2{'a'}, e3('a') {}
char e1{};
char e2 = 0;
char e3 = '\0';
char e4 = 'a';
};
struct ExistingInt {
ExistingInt(short) : e1(), e2{}, e3(), e4(), e5(), e6() {}
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: member initializer for 'e1' is redundant [modernize-use-default-member-init]
// CHECK-MESSAGES: :[[@LINE-2]]:30: warning: member initializer for 'e2' is redundant
// CHECK-FIXES: ExistingInt(short) : e3(), e4(), e5(), e6() {}
ExistingInt(int) : e1(0), e2{0}, e3(0), e4(0), e5(0), e6(0) {}
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: member initializer for 'e1' is redundant
// CHECK-MESSAGES: :[[@LINE-2]]:29: warning: member initializer for 'e2' is redundant
// CHECK-FIXES: ExistingInt(int) : e3(0), e4(0), e5(0), e6(0) {}
ExistingInt(long) : e1(5), e2{5}, e3(5), e4(5), e5(5), e6(5) {}
// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: member initializer for 'e3' is redundant
// CHECK-MESSAGES: :[[@LINE-2]]:44: warning: member initializer for 'e4' is redundant
// CHECK-MESSAGES: :[[@LINE-3]]:58: warning: member initializer for 'e6' is redundant
// CHECK-FIXES: ExistingInt(long) : e1(5), e2{5}, e5(5) {}
ExistingInt(char) : e1(-5), e2{-5}, e3(-5), e4(-5), e5(-5), e6(-5) {}
// CHECK-MESSAGES: :[[@LINE-1]]:55: warning: member initializer for 'e5' is redundant
// CHECK-FIXES: ExistingInt(char) : e1(-5), e2{-5}, e3(-5), e4(-5), e6(-5) {}
int e1{};
int e2 = 0;
int e3 = {5};
int e4{5};
int e5 = -5;
int e6 = +5;
};
struct ExistingDouble {
ExistingDouble(short) : e1(), e2{}, e3(), e4(), e5() {}
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: member initializer for 'e1' is redundant
// CHECK-MESSAGES: :[[@LINE-2]]:33: warning: member initializer for 'e2' is redundant
// CHECK-FIXES: ExistingDouble(short) : e3(), e4(), e5() {}
ExistingDouble(int) : e1(0.0), e2{0.0}, e3(0.0), e4(0.0), e5(0.0) {}
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: member initializer for 'e1' is redundant
// CHECK-MESSAGES: :[[@LINE-2]]:34: warning: member initializer for 'e2' is redundant
// CHECK-FIXES: ExistingDouble(int) : e3(0.0), e4(0.0), e5(0.0) {}
ExistingDouble(long) : e1(5.0), e2{5.0}, e3(5.0), e4(5.0), e5(5.0) {}
// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: member initializer for 'e3' is redundant
// CHECK-MESSAGES: :[[@LINE-2]]:62: warning: member initializer for 'e5' is redundant
// CHECK-FIXES: ExistingDouble(long) : e1(5.0), e2{5.0}, e4(5.0) {}
ExistingDouble(char) : e1(-5.0), e2{-5.0}, e3(-5.0), e4(-5.0), e5(-5.0) {}
// CHECK-MESSAGES: :[[@LINE-1]]:56: warning: member initializer for 'e4' is redundant
// CHECK-FIXES: ExistingDouble(char) : e1(-5.0), e2{-5.0}, e3(-5.0), e5(-5.0) {}
double e1{};
double e2 = 0.0;
double e3 = 5.0;
double e4{-5.0};
double e5 = +5.0;
};
struct ExistingBool {
ExistingBool(short) : e1(), e2{}, e3() {}
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: member initializer for 'e1' is redundant
// CHECK-MESSAGES: :[[@LINE-2]]:31: warning: member initializer for 'e2' is redundant
// CHECK-FIXES: ExistingBool(short) : e3() {}
ExistingBool(int) : e1(false), e2{false}, e3(false) {}
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: member initializer for 'e1' is redundant
// CHECK-MESSAGES: :[[@LINE-2]]:34: warning: member initializer for 'e2' is redundant
// CHECK-FIXES: ExistingBool(int) : e3(false) {}
ExistingBool(long) : e1(true), e2{true}, e3(true) {}
// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: member initializer for 'e3' is redundant
// CHECK-FIXES: ExistingBool(long) : e1(true), e2{true} {}
bool e1{};
bool e2 = false;
bool e3{true};
};
struct ExistingEnum {
ExistingEnum(short) : e1(Foo), e2{Foo} {}
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: member initializer for 'e1' is redundant
// CHECK-FIXES: ExistingEnum(short) : e2{Foo} {}
ExistingEnum(int) : e1(Bar), e2{Bar} {}
// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: member initializer for 'e2' is redundant
// CHECK-FIXES: ExistingEnum(int) : e1(Bar) {}
Enum e1 = Foo;
Enum e2{Bar};
};
struct ExistingPointer {
ExistingPointer(short) : e1(), e2{}, e3(), e4() {}
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: member initializer for 'e1' is redundant
// CHECK-MESSAGES: :[[@LINE-2]]:34: warning: member initializer for 'e2' is redundant
// CHECK-MESSAGES: :[[@LINE-3]]:40: warning: member initializer for 'e3' is redundant
// CHECK-FIXES: ExistingPointer(short) : e4() {}
ExistingPointer(int) : e1(0), e2{0}, e3(0), e4(&e1) {}
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: member initializer for 'e1' is redundant
// CHECK-MESSAGES: :[[@LINE-2]]:33: warning: member initializer for 'e2' is redundant
// CHECK-MESSAGES: :[[@LINE-3]]:40: warning: member initializer for 'e3' is redundant
// CHECK-FIXES: ExistingPointer(int) : e4(&e1) {}
ExistingPointer(long) : e1(nullptr), e2{nullptr}, e3(nullptr), e4(&e2) {}
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: member initializer for 'e1' is redundant
// CHECK-MESSAGES: :[[@LINE-2]]:40: warning: member initializer for 'e2' is redundant
// CHECK-MESSAGES: :[[@LINE-3]]:53: warning: member initializer for 'e3' is redundant
// CHECK-FIXES: ExistingPointer(long) : e4(&e2) {}
int *e1{};
int *e2 = 0;
int *e3{nullptr};
int **e4 = &e1;
};
struct ExistingString {
ExistingString(short) : e1(), e2{}, e3(), e4() {}
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: member initializer for 'e1' is redundant [modernize-use-default-member-init]
// CHECK-MESSAGES: :[[@LINE-2]]:33: warning: member initializer for 'e2' is redundant
// CHECK-FIXES: ExistingString(short) : e3(), e4() {}
ExistingString(int) : e1(0), e2{0}, e3(0), e4(0) {}
// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: member initializer for 'e1' is redundant
// CHECK-MESSAGES: :[[@LINE-2]]:32: warning: member initializer for 'e2' is redundant
// CHECK-FIXES: ExistingString(int) : e3(0), e4(0) {}
ExistingString(long) : e1(nullptr), e2{nullptr}, e3(nullptr), e4(nullptr) {}
// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: member initializer for 'e1' is redundant
// CHECK-MESSAGES: :[[@LINE-2]]:39: warning: member initializer for 'e2' is redundant
// CHECK-FIXES: ExistingString(long) : e3(nullptr), e4(nullptr) {}
ExistingString(char) : e1("foo"), e2{"foo"}, e3("foo"), e4("foo") {}
// CHECK-MESSAGES: :[[@LINE-1]]:48: warning: member initializer for 'e3' is redundant
// CHECK-FIXES: ExistingString(char) : e1("foo"), e2{"foo"}, e4("foo") {}
const char *e1{};
const char *e2 = nullptr;
const char *e3 = "foo";
const char *e4 = "bar";
};
struct UnionExisting {
UnionExisting() : e(5.0) {}
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: member initializer for 'e' is redundant
// CHECK-FIXES: UnionExisting() {}
union {
int i;
double e = 5.0;
};
};
template <typename T>
struct NegativeTemplateExisting {
NegativeTemplateExisting(int) : t(0) {}
T t{};
};
NegativeTemplateExisting<int> ntei(0);
NegativeTemplateExisting<double> nted(0);
// This resulted in a warning by default.
#define MACRO() \
struct MacroS { \
void *P; \
MacroS() : P(nullptr) {} \
};
MACRO();
class FunctionTryBlock {
public:
FunctionTryBlock() try : i(5), k(8) {}
// CHECK-FIXES: FunctionTryBlock() try {}
catch (...) {}
private:
int i, k;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'i' [modernize-use-default-member-init]
// CHECK-MESSAGES: :[[@LINE-2]]:10: warning: use default member initializer for 'k' [modernize-use-default-member-init]
// CHECK-FIXES: int i{5}, k{8};
};
struct PR45363 {
// Ensure no warning is emitted here
PR45363(int i = 0) : m_i{i} {}
int m_i;
};
struct EmptyBracedIntDefault {
EmptyBracedIntDefault() : m_i{} {}
int m_i;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'm_i' [modernize-use-default-member-init]
// CHECK-FIXES: {{^ }}EmptyBracedIntDefault() {}
// CHECK-FIXES-NEXT: {{^ }}int m_i{};
};