1// RUN: %check_clang_tidy %s modernize-use-default-member-init %t -- \
2// RUN: -config="{CheckOptions: {modernize-use-default-member-init.UseAssignment: true}}"
3
4struct S {
5};
6
7struct PositiveValueChar {
8 PositiveValueChar() : c0(), c1()/*, c2(), c3()*/ {}
9 // CHECK-FIXES: PositiveValueChar() {}
10 const char c0;
11 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use default member initializer for 'c0' [modernize-use-default-member-init]
12 // CHECK-FIXES: const char c0 = '\0';
13 wchar_t c1;
14 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use default member initializer for 'c1'
15 // CHECK-FIXES: wchar_t c1 = L'\0';
16 // FIXME: char16_t c2;
17 // C HECK-MESSAGES: :[[@LINE-1]]:12: warning: use default member initializer for 'c2'
18 // C HECK-FIXES: char16_t c2 = u'\0';
19 // FIXME: char32_t c3;
20 // C HECK-MESSAGES: :[[@LINE-1]]:12: warning: use default member initializer for 'c3'
21 // C HECK-FIXES: char32_t c3 = U'\0';
22};
23
24struct PositiveChar {
25 PositiveChar() : d('a') {}
26 // CHECK-FIXES: PositiveChar() {}
27 char d;
28 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use default member initializer for 'd'
29 // CHECK-FIXES: char d = 'a';
30};
31
32struct PositiveValueInt {
33 PositiveValueInt() : i() {}
34 // CHECK-FIXES: PositiveValueInt() {}
35 const int i;
36 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use default member initializer for 'i'
37 // CHECK-FIXES: const int i = 0;
38};
39
40struct PositiveInt {
41 PositiveInt() : j(1) {}
42 // CHECK-FIXES: PositiveInt() {}
43 int j;
44 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'j'
45 // CHECK-FIXES: int j = 1;
46};
47
48struct PositiveUnaryMinusInt {
49 PositiveUnaryMinusInt() : j(-1) {}
50 // CHECK-FIXES: PositiveUnaryMinusInt() {}
51 int j;
52 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'j'
53 // CHECK-FIXES: int j = -1;
54};
55
56struct PositiveUnaryPlusInt {
57 PositiveUnaryPlusInt() : j(+1) {}
58 // CHECK-FIXES: PositiveUnaryPlusInt() {}
59 int j;
60 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'j'
61 // CHECK-FIXES: int j = +1;
62};
63
64struct PositiveValueComplexInt {
65 PositiveValueComplexInt() : i() {}
66 // CHECK-FIXES: PositiveValueComplexInt() {}
67 _Complex int i;
68 // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use default member initializer for 'i'
69 // CHECK-FIXES: _Complex int i = 0;
70};
71
72struct PositiveValueFloat {
73 PositiveValueFloat() : f() {}
74 // CHECK-FIXES: PositiveValueFloat() {}
75 float f;
76 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use default member initializer for 'f'
77 // CHECK-FIXES: float f = 0.0f;
78};
79
80struct PositiveValueDouble {
81 PositiveValueDouble() : d() {}
82 // CHECK-FIXES: PositiveValueDouble() {}
83 double d;
84 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'd'
85 // CHECK-FIXES: double d = 0.0;
86};
87
88struct PositiveDouble {
89 PositiveDouble() : f(2.5463e43) {}
90 // CHECK-FIXES: PositiveDouble() {}
91 double f;
92 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'f'
93 // CHECK-FIXES: double f = 2.5463e43;
94};
95
96struct PositiveValueComplexFloat {
97 PositiveValueComplexFloat() : f() {}
98 // CHECK-FIXES: PositiveValueComplexFloat() {}
99 _Complex float f;
100 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use default member initializer for 'f'
101 // CHECK-FIXES: _Complex float f = 0.0f;
102};
103
104struct PositiveValueComplexDouble {
105 PositiveValueComplexDouble() : f() {}
106 // CHECK-FIXES: PositiveValueComplexDouble() {}
107 _Complex double f;
108 // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use default member initializer for 'f'
109 // CHECK-FIXES: _Complex double f = 0.0;
110};
111
112struct PositiveUnaryMinusDouble {
113 PositiveUnaryMinusDouble() : f(-2.5463e43) {}
114 // CHECK-FIXES: PositiveUnaryMinusDouble() {}
115 double f;
116 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'f'
117 // CHECK-FIXES: double f = -2.5463e43;
118};
119
120struct PositiveUnaryPlusDouble {
121 PositiveUnaryPlusDouble() : f(+2.5463e43) {}
122 // CHECK-FIXES: PositiveUnaryPlusDouble() {}
123 double f;
124 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'f'
125 // CHECK-FIXES: double f = +2.5463e43;
126};
127
128struct PositiveValueBool {
129 PositiveValueBool() : b() {}
130 // CHECK-FIXES: PositiveValueBool() {}
131 bool b;
132 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use default member initializer for 'b'
133 // CHECK-FIXES: bool b = false;
134};
135
136struct PositiveBool {
137 PositiveBool() : a(true) {}
138 // CHECK-FIXES: PositiveBool() {}
139 bool a;
140 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use default member initializer for 'a'
141 // CHECK-FIXES: bool a = true;
142};
143
144struct PositiveValuePointer {
145 PositiveValuePointer() : p() {}
146 // CHECK-FIXES: PositiveValuePointer() {}
147 int *p;
148 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use default member initializer for 'p'
149 // CHECK-FIXES: int *p = nullptr;
150};
151
152struct PositiveNullPointer {
153 PositiveNullPointer() : q(nullptr) {}
154 // CHECK-FIXES: PositiveNullPointer() {}
155 int *q;
156 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use default member initializer for 'q'
157 // CHECK-FIXES: int *q = nullptr;
158};
159
160enum Enum { Foo };
161struct PositiveEnum {
162 PositiveEnum() : e(Foo) {}
163 // CHECK-FIXES: PositiveEnum() {}
164 Enum e;
165 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use default member initializer for 'e'
166 // CHECK-FIXES: Enum e = Foo;
167};
168
169struct PositiveValueEnum {
170 PositiveValueEnum() : e() {}
171 // CHECK-FIXES: PositiveValueEnum() {}
172 Enum e;
173 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use default member initializer for 'e'
174 // CHECK-FIXES: Enum e{};
175};
176
177struct PositiveString {
178 PositiveString() : s("foo") {}
179 // CHECK-FIXES: PositiveString() {}
180 const char *s;
181 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use default member initializer for 's'
182 // CHECK-FIXES: const char *s = "foo";
183};
184
185template <typename T>
186struct NegativeTemplate {
187 NegativeTemplate() : t() {}
188 T t;
189};
190
191NegativeTemplate<int> nti;
192NegativeTemplate<double> ntd;
193
194namespace PR63285 {
195
196class ArrayValueInit {
197 ArrayValueInit() : m_array() {}
198 double m_array[1];
199 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'm_array' [modernize-use-default-member-init]
200 // CHECK-FIXES: {{^ }}ArrayValueInit() {}
201 // CHECK-FIXES-NEXT: {{^ }}double m_array[1] = {};
202};
203
204class ArrayBraceInit {
205 ArrayBraceInit() : m_array{} {}
206 double m_array[1];
207 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'm_array' [modernize-use-default-member-init]
208 // CHECK-FIXES: {{^ }}ArrayBraceInit() {}
209 // CHECK-FIXES-NEXT: {{^ }}double m_array[1] = {};
210};
211
212class ArrayBraceInitWithValue {
213 ArrayBraceInitWithValue() : m_array{3.14} {}
214 double m_array[1];
215 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'm_array' [modernize-use-default-member-init]
216 // CHECK-FIXES: {{^ }}ArrayBraceInitWithValue() {}
217 // CHECK-FIXES-NEXT: {{^ }}double m_array[1] = {3.14};
218};
219
220class ArrayBraceInitMultipleValues {
221 ArrayBraceInitMultipleValues() : m_array{1.0, 2.0, 3.0} {}
222 double m_array[3];
223 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'm_array' [modernize-use-default-member-init]
224 // CHECK-FIXES: {{^ }}ArrayBraceInitMultipleValues() {}
225 // CHECK-FIXES-NEXT: {{^ }}double m_array[3] = {1.0, 2.0, 3.0};
226};
227
228} // namespace PR63285
229

source code of clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init-assignment.cpp