| 1 | // RUN: %check_clang_tidy %s readability-redundant-member-init %t \ |
| 2 | // RUN: -config="{CheckOptions: \ |
| 3 | // RUN: {readability-redundant-member-init.IgnoreBaseInCopyConstructors: \ |
| 4 | // RUN: true} \ |
| 5 | // RUN: }" |
| 6 | |
| 7 | struct S { |
| 8 | S() = default; |
| 9 | S(int i) : i(i) {} |
| 10 | int i = 1; |
| 11 | }; |
| 12 | |
| 13 | struct T { |
| 14 | T(int i = 1) : i(i) {} |
| 15 | int i; |
| 16 | }; |
| 17 | |
| 18 | struct U { |
| 19 | int i; |
| 20 | }; |
| 21 | |
| 22 | union V { |
| 23 | int i; |
| 24 | double f; |
| 25 | }; |
| 26 | |
| 27 | // Initializer calls default constructor |
| 28 | struct F1 { |
| 29 | F1() : f() {} |
| 30 | // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 'f' is redundant |
| 31 | // CHECK-FIXES: F1() {} |
| 32 | S f; |
| 33 | }; |
| 34 | |
| 35 | // Initializer calls default constructor with default argument |
| 36 | struct F2 { |
| 37 | F2() : f() {} |
| 38 | // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 'f' is redundant |
| 39 | // CHECK-FIXES: F2() {} |
| 40 | T f; |
| 41 | }; |
| 42 | |
| 43 | // Multiple redundant initializers for same constructor |
| 44 | struct F3 { |
| 45 | F3() : f(), g(1), h() {} |
| 46 | // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 'f' is redundant |
| 47 | // CHECK-MESSAGES: :[[@LINE-2]]:21: warning: initializer for member 'h' is redundant |
| 48 | // CHECK-FIXES: F3() : g(1) {} |
| 49 | S f, g, h; |
| 50 | }; |
| 51 | |
| 52 | // Templated class independent type |
| 53 | template <class V> |
| 54 | struct F4 { |
| 55 | F4() : f() {} |
| 56 | // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 'f' is redundant |
| 57 | // CHECK-FIXES: F4() {} |
| 58 | S f; |
| 59 | }; |
| 60 | F4<int> f4i; |
| 61 | F4<S> f4s; |
| 62 | |
| 63 | // Base class |
| 64 | struct F5 : S { |
| 65 | F5() : S() {} |
| 66 | // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for base class 'S' is redundant |
| 67 | // CHECK-FIXES: F5() {} |
| 68 | }; |
| 69 | |
| 70 | // Constructor call requires cleanup |
| 71 | struct Cleanup { |
| 72 | ~Cleanup() {} |
| 73 | }; |
| 74 | |
| 75 | struct UsesCleanup { |
| 76 | UsesCleanup(const Cleanup &c = Cleanup()) {} |
| 77 | }; |
| 78 | |
| 79 | struct F6 { |
| 80 | F6() : uc() {} |
| 81 | // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 'uc' is redundant |
| 82 | // CHECK-FIXES: F6() {} |
| 83 | UsesCleanup uc; |
| 84 | }; |
| 85 | |
| 86 | // Multiple inheritance |
| 87 | struct F7 : S, T { |
| 88 | F7() : S(), T() {} |
| 89 | // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for base class 'S' is redundant |
| 90 | // CHECK-MESSAGES: :[[@LINE-2]]:15: warning: initializer for base class 'T' is redundant |
| 91 | // CHECK-FIXES: F7() {} |
| 92 | }; |
| 93 | |
| 94 | namespace Foo { |
| 95 | inline namespace Bar { |
| 96 | template <int N> |
| 97 | struct Template { |
| 98 | Template() = default; |
| 99 | int i = N; |
| 100 | }; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | enum { N_THINGS = 5 }; |
| 105 | |
| 106 | struct F8 : Foo::Template<N_THINGS> { |
| 107 | F8() : Template() {} |
| 108 | // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for base class 'Foo::Template<N_THINGS>' is redundant |
| 109 | // CHECK-FIXES: F8() {} |
| 110 | }; |
| 111 | |
| 112 | // Anonymous struct |
| 113 | struct F9 { |
| 114 | F9() : s1() {} |
| 115 | // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 's1' is redundant |
| 116 | // CHECK-FIXES: F9() {} |
| 117 | struct { |
| 118 | S s1; |
| 119 | S s2; |
| 120 | }; |
| 121 | }; |
| 122 | |
| 123 | // struct whose inline copy constructor default-initializes its base class |
| 124 | struct WithCopyConstructor1 : public T { |
| 125 | WithCopyConstructor1(const WithCopyConstructor1& other) : T(), |
| 126 | f(), |
| 127 | g() |
| 128 | {} |
| 129 | S f, g; |
| 130 | }; |
| 131 | // No warning in copy constructor about T since IgnoreBaseInCopyConstructors=1 |
| 132 | // CHECK-MESSAGES: :[[@LINE-6]]:5: warning: initializer for member 'f' is redundant |
| 133 | // CHECK-MESSAGES: :[[@LINE-6]]:5: warning: initializer for member 'g' is redundant |
| 134 | // CHECK-FIXES: WithCopyConstructor1(const WithCopyConstructor1& other) : T() |
| 135 | // CHECK-NEXT: |
| 136 | // CHECK-NEXT: |
| 137 | // CHECK-NEXT: {} |
| 138 | |
| 139 | // struct whose copy constructor default-initializes its base class |
| 140 | struct WithCopyConstructor2 : public T { |
| 141 | WithCopyConstructor2(const WithCopyConstructor2& other); |
| 142 | S a; |
| 143 | }; |
| 144 | WithCopyConstructor2::WithCopyConstructor2(const WithCopyConstructor2& other) |
| 145 | : T(), a() |
| 146 | {} |
| 147 | // No warning in copy constructor about T since IgnoreBaseInCopyConstructors=1 |
| 148 | // CHECK-MESSAGES: :[[@LINE-3]]:10: warning: initializer for member 'a' is redundant |
| 149 | // CHECK-FIXES: {{^}} : T() {{$}} |
| 150 | // CHECK-NEXT: {} |
| 151 | |
| 152 | // Initializer not written |
| 153 | struct NF1 { |
| 154 | NF1() {} |
| 155 | S f; |
| 156 | }; |
| 157 | |
| 158 | // Initializer doesn't call default constructor |
| 159 | struct NF2 { |
| 160 | NF2() : f(1) {} |
| 161 | S f; |
| 162 | }; |
| 163 | |
| 164 | // Initializer calls default constructor without using default argument |
| 165 | struct NF3 { |
| 166 | NF3() : f(1) {} |
| 167 | T f; |
| 168 | }; |
| 169 | |
| 170 | // Initializer calls default constructor without using default argument |
| 171 | struct NF4 { |
| 172 | NF4() : f(2) {} |
| 173 | T f; |
| 174 | }; |
| 175 | |
| 176 | // Initializer is zero-initialization |
| 177 | struct NF5 { |
| 178 | NF5() : i() {} |
| 179 | int i; |
| 180 | }; |
| 181 | |
| 182 | // Initializer is direct-initialization |
| 183 | struct NF6 { |
| 184 | NF6() : i(1) {} |
| 185 | int i; |
| 186 | }; |
| 187 | |
| 188 | // Initializer is aggregate initialization of struct |
| 189 | struct NF7 { |
| 190 | NF7() : f{} {} |
| 191 | U f; |
| 192 | }; |
| 193 | |
| 194 | // Initializer is zero-initialization of struct |
| 195 | struct NF7b { |
| 196 | NF7b() : f() {} |
| 197 | U f; |
| 198 | }; |
| 199 | |
| 200 | // Initializer is aggregate initialization of array |
| 201 | struct NF8 { |
| 202 | NF8() : f{} {} |
| 203 | int f[2]; |
| 204 | }; |
| 205 | |
| 206 | struct NF9 { |
| 207 | NF9() : f{} {} |
| 208 | S f[2]; |
| 209 | }; |
| 210 | |
| 211 | // Initializing member of union |
| 212 | union NF10 { |
| 213 | NF10() : s() {} |
| 214 | int i; |
| 215 | S s; |
| 216 | }; |
| 217 | |
| 218 | // Templated class dependent type |
| 219 | template <class V> |
| 220 | struct NF11 { |
| 221 | NF11() : f() {} |
| 222 | V f; |
| 223 | }; |
| 224 | NF11<int> nf11i; |
| 225 | NF11<S> nf11s; |
| 226 | |
| 227 | // Delegating constructor |
| 228 | class NF12 { |
| 229 | NF12() = default; |
| 230 | NF12(int) : NF12() {} |
| 231 | }; |
| 232 | |
| 233 | // Const member |
| 234 | struct NF13 { |
| 235 | NF13() : f() {} |
| 236 | const S f; |
| 237 | }; |
| 238 | |
| 239 | // Union member |
| 240 | struct NF14 { |
| 241 | NF14() : f() {} |
| 242 | V f; |
| 243 | }; |
| 244 | |
| 245 | // Anonymous union member |
| 246 | struct NF15 { |
| 247 | NF15() : s1() {} |
| 248 | union { |
| 249 | S s1; |
| 250 | S s2; |
| 251 | }; |
| 252 | }; |
| 253 | |
| 254 | // Direct in-class initialization with default constructor |
| 255 | struct D1 { |
| 256 | S f1 {}; |
| 257 | // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: initializer for member 'f1' is redundant |
| 258 | // CHECK-FIXES: S f1; |
| 259 | }; |
| 260 | |
| 261 | // Direct in-class initialization with constructor with default argument |
| 262 | struct D2 { |
| 263 | T f2 {}; |
| 264 | // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: initializer for member 'f2' is redundant |
| 265 | // CHECK-FIXES: T f2; |
| 266 | }; |
| 267 | |
| 268 | // Direct in-class initialization with default constructor (assign) |
| 269 | struct D3 { |
| 270 | S f3 = {}; |
| 271 | // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: initializer for member 'f3' is redundant |
| 272 | // CHECK-FIXES: S f3; |
| 273 | }; |
| 274 | |
| 275 | // Direct in-class initialization with constructor with default argument (assign) |
| 276 | struct D4 { |
| 277 | T f4 = {}; |
| 278 | // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: initializer for member 'f4' is redundant |
| 279 | // CHECK-FIXES: T f4; |
| 280 | }; |
| 281 | |
| 282 | // Templated class independent type |
| 283 | template <class V> |
| 284 | struct D5 { |
| 285 | S f5 /*comment*/ = S(); |
| 286 | // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: initializer for member 'f5' is redundant |
| 287 | // CHECK-FIXES: S f5 /*comment*/; |
| 288 | }; |
| 289 | D5<int> d5i; |
| 290 | D5<S> d5s; |
| 291 | |
| 292 | struct D6 { |
| 293 | UsesCleanup uc2{}; |
| 294 | // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: initializer for member 'uc2' is redundant |
| 295 | // CHECK-FIXES: UsesCleanup uc2; |
| 296 | }; |
| 297 | |
| 298 | template<typename V> |
| 299 | struct D7 { |
| 300 | V f7; |
| 301 | }; |
| 302 | |
| 303 | D7<int> d7i; |
| 304 | D7<S> d7s; |
| 305 | |
| 306 | struct SS { |
| 307 | SS() = default; |
| 308 | SS(S s) : s(s) {} |
| 309 | |
| 310 | S s; |
| 311 | }; |
| 312 | |
| 313 | struct D8 { |
| 314 | SS ss = S(); |
| 315 | }; |
| 316 | |
| 317 | struct D9 { |
| 318 | D9() : ss(S()) {} |
| 319 | SS ss; |
| 320 | }; |
| 321 | |