1 // RUN: %check_clang_tidy %s readability-redundant-member-init %t
2 
3 struct S {
4   S() = default;
SS5   S(int i) : i(i) {}
6   int i = 1;
7 };
8 
9 struct T {
TT10   T(int i = 1) : i(i) {}
11   int i;
12 };
13 
14 struct U {
15   int i;
16 };
17 
18 union V {
19   int i;
20   double f;
21 };
22 
23 // Initializer calls default constructor
24 struct F1 {
F1F125   F1() : f() {}
26   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 'f' is redundant
27   // CHECK-FIXES: F1()  {}
28   S f;
29 };
30 
31 // Initializer calls default constructor with default argument
32 struct F2 {
F2F233   F2() : f() {}
34   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 'f' is redundant
35   // CHECK-FIXES: F2()  {}
36   T f;
37 };
38 
39 // Multiple redundant initializers for same constructor
40 struct F3 {
F3F341   F3() : f(), g(1), h() {}
42   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 'f' is redundant
43   // CHECK-MESSAGES: :[[@LINE-2]]:21: warning: initializer for member 'h' is redundant
44   // CHECK-FIXES: F3() :  g(1) {}
45   S f, g, h;
46 };
47 
48 // Templated class independent type
49 template <class V>
50 struct F4 {
F4F451   F4() : f() {}
52   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 'f' is redundant
53   // CHECK-FIXES: F4()  {}
54   S f;
55 };
56 F4<int> f4i;
57 F4<S> f4s;
58 
59 // Base class
60 struct F5 : S {
F5F561   F5() : S() {}
62   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for base class 'S' is redundant
63   // CHECK-FIXES: F5()  {}
64 };
65 
66 // Constructor call requires cleanup
67 struct Cleanup {
~CleanupCleanup68   ~Cleanup() {}
69 };
70 
71 struct UsesCleanup {
UsesCleanupUsesCleanup72   UsesCleanup(const Cleanup &c = Cleanup()) {}
73 };
74 
75 struct F6 {
F6F676   F6() : uc() {}
77   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 'uc' is redundant
78   // CHECK-FIXES: F6()  {}
79   UsesCleanup uc;
80 };
81 
82 // Multiple inheritance
83 struct F7 : S, T {
F7F784   F7() : S(), T() {}
85   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for base class 'S' is redundant
86   // CHECK-MESSAGES: :[[@LINE-2]]:15: warning: initializer for base class 'T' is redundant
87   // CHECK-FIXES: F7()  {}
88 };
89 
90 namespace Foo {
91 inline namespace Bar {
92 template <int N>
93 struct Template {
94   Template() = default;
95   int i = N;
96 };
97 }
98 }
99 
100 enum { N_THINGS = 5 };
101 
102 struct F8 : Foo::Template<N_THINGS> {
F8F8103   F8() : Template() {}
104   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for base class 'Foo::Template<N_THINGS>' is redundant
105   // CHECK-FIXES: F8()  {}
106 };
107 
108 // Anonymous struct
109 struct F9 {
F9F9110   F9() : s1() {}
111   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 's1' is redundant
112   // CHECK-FIXES: F9()  {}
113   struct {
114     S s1;
115     S s2;
116   };
117 };
118 
119 // Initializer not written
120 struct NF1 {
NF1NF1121   NF1() {}
122   S f;
123 };
124 
125 // Initializer doesn't call default constructor
126 struct NF2 {
NF2NF2127   NF2() : f(1) {}
128   S f;
129 };
130 
131 // Initializer calls default constructor without using default argument
132 struct NF3 {
NF3NF3133   NF3() : f(1) {}
134   T f;
135 };
136 
137 // Initializer calls default constructor without using default argument
138 struct NF4 {
NF4NF4139   NF4() : f(2) {}
140   T f;
141 };
142 
143 // Initializer is zero-initialization
144 struct NF5 {
NF5NF5145   NF5() : i() {}
146   int i;
147 };
148 
149 // Initializer is direct-initialization
150 struct NF6 {
NF6NF6151   NF6() : i(1) {}
152   int i;
153 };
154 
155 // Initializer is aggregate initialization of struct
156 struct NF7 {
NF7NF7157   NF7() : f{} {}
158   U f;
159 };
160 
161 // Initializer is zero-initialization of struct
162 struct NF7b {
NF7bNF7b163   NF7b() : f() {}
164   U f;
165 };
166 
167 // Initializer is aggregate initialization of array
168 struct NF8 {
NF8NF8169   NF8() : f{} {}
170   int f[2];
171 };
172 
173 struct NF9 {
NF9NF9174   NF9() : f{} {}
175   S f[2];
176 };
177 
178 // Initializing member of union
179 union NF10 {
NF10()180   NF10() : s() {}
181   int i;
182   S s;
183 };
184 
185 // Templated class dependent type
186 template <class V>
187 struct NF11 {
NF11NF11188   NF11() : f() {}
189   V f;
190 };
191 NF11<int> nf11i;
192 NF11<S> nf11s;
193 
194 // Delegating constructor
195 class NF12 {
196   NF12() = default;
NF12(int)197   NF12(int) : NF12() {}
198 };
199 
200 // Const member
201 struct NF13 {
NF13NF13202   NF13() : f() {}
203   const S f;
204 };
205 
206 // Union member
207 struct NF14 {
NF14NF14208   NF14() : f() {}
209   V f;
210 };
211 
212 // Anonymous union member
213 struct NF15 {
NF15NF15214   NF15() : s1() {}
215   union {
216     S s1;
217     S s2;
218   };
219 };
220