321 lines · cpp
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 7struct S {8 S() = default;9 S(int i) : i(i) {}10 int i = 1;11};12 13struct T {14 T(int i = 1) : i(i) {}15 int i;16};17 18struct U {19 int i;20};21 22union V {23 int i;24 double f;25};26 27// Initializer calls default constructor28struct F1 {29 F1() : f() {}30 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 'f' is redundant31 // CHECK-FIXES: F1() {}32 S f;33};34 35// Initializer calls default constructor with default argument36struct F2 {37 F2() : f() {}38 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 'f' is redundant39 // CHECK-FIXES: F2() {}40 T f;41};42 43// Multiple redundant initializers for same constructor44struct F3 {45 F3() : f(), g(1), h() {}46 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 'f' is redundant47 // CHECK-MESSAGES: :[[@LINE-2]]:21: warning: initializer for member 'h' is redundant48 // CHECK-FIXES: F3() : g(1) {}49 S f, g, h;50};51 52// Templated class independent type53template <class V>54struct F4 {55 F4() : f() {}56 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 'f' is redundant57 // CHECK-FIXES: F4() {}58 S f;59};60F4<int> f4i;61F4<S> f4s;62 63// Base class64struct F5 : S {65 F5() : S() {}66 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for base class 'S' is redundant67 // CHECK-FIXES: F5() {}68};69 70// Constructor call requires cleanup71struct Cleanup {72 ~Cleanup() {}73};74 75struct UsesCleanup {76 UsesCleanup(const Cleanup &c = Cleanup()) {}77};78 79struct F6 {80 F6() : uc() {}81 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 'uc' is redundant82 // CHECK-FIXES: F6() {}83 UsesCleanup uc;84};85 86// Multiple inheritance87struct F7 : S, T {88 F7() : S(), T() {}89 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for base class 'S' is redundant90 // CHECK-MESSAGES: :[[@LINE-2]]:15: warning: initializer for base class 'T' is redundant91 // CHECK-FIXES: F7() {}92};93 94namespace Foo {95inline namespace Bar {96template <int N>97struct Template {98 Template() = default;99 int i = N;100};101}102}103 104enum { N_THINGS = 5 };105 106struct F8 : Foo::Template<N_THINGS> {107 F8() : Template() {}108 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for base class 'Foo::Template<N_THINGS>' is redundant109 // CHECK-FIXES: F8() {}110};111 112// Anonymous struct113struct F9 {114 F9() : s1() {}115 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: initializer for member 's1' is redundant116 // CHECK-FIXES: F9() {}117 struct {118 S s1;119 S s2;120 };121};122 123// struct whose inline copy constructor default-initializes its base class124struct 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=1132// CHECK-MESSAGES: :[[@LINE-6]]:5: warning: initializer for member 'f' is redundant133// CHECK-MESSAGES: :[[@LINE-6]]:5: warning: initializer for member 'g' is redundant134// 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 class140struct WithCopyConstructor2 : public T {141 WithCopyConstructor2(const WithCopyConstructor2& other);142 S a;143};144WithCopyConstructor2::WithCopyConstructor2(const WithCopyConstructor2& other)145 : T(), a()146{}147// No warning in copy constructor about T since IgnoreBaseInCopyConstructors=1148// CHECK-MESSAGES: :[[@LINE-3]]:10: warning: initializer for member 'a' is redundant149// CHECK-FIXES: : T()150// CHECK-NEXT: {}151 152// Initializer not written153struct NF1 {154 NF1() {}155 S f;156};157 158// Initializer doesn't call default constructor159struct NF2 {160 NF2() : f(1) {}161 S f;162};163 164// Initializer calls default constructor without using default argument165struct NF3 {166 NF3() : f(1) {}167 T f;168};169 170// Initializer calls default constructor without using default argument171struct NF4 {172 NF4() : f(2) {}173 T f;174};175 176// Initializer is zero-initialization177struct NF5 {178 NF5() : i() {}179 int i;180};181 182// Initializer is direct-initialization183struct NF6 {184 NF6() : i(1) {}185 int i;186};187 188// Initializer is aggregate initialization of struct189struct NF7 {190 NF7() : f{} {}191 U f;192};193 194// Initializer is zero-initialization of struct195struct NF7b {196 NF7b() : f() {}197 U f;198};199 200// Initializer is aggregate initialization of array201struct NF8 {202 NF8() : f{} {}203 int f[2];204};205 206struct NF9 {207 NF9() : f{} {}208 S f[2];209};210 211// Initializing member of union212union NF10 {213 NF10() : s() {}214 int i;215 S s;216};217 218// Templated class dependent type219template <class V>220struct NF11 {221 NF11() : f() {}222 V f;223};224NF11<int> nf11i;225NF11<S> nf11s;226 227// Delegating constructor228class NF12 {229 NF12() = default;230 NF12(int) : NF12() {}231};232 233// Const member234struct NF13 {235 NF13() : f() {}236 const S f;237};238 239// Union member240struct NF14 {241 NF14() : f() {}242 V f;243};244 245// Anonymous union member246struct NF15 {247 NF15() : s1() {}248 union {249 S s1;250 S s2;251 };252};253 254// Direct in-class initialization with default constructor255struct D1 {256 S f1 {};257 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: initializer for member 'f1' is redundant258 // CHECK-FIXES: S f1;259};260 261// Direct in-class initialization with constructor with default argument262struct D2 {263 T f2 {};264 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: initializer for member 'f2' is redundant265 // CHECK-FIXES: T f2;266};267 268// Direct in-class initialization with default constructor (assign)269struct D3 {270 S f3 = {};271 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: initializer for member 'f3' is redundant272 // CHECK-FIXES: S f3;273};274 275// Direct in-class initialization with constructor with default argument (assign)276struct D4 {277 T f4 = {};278 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: initializer for member 'f4' is redundant279 // CHECK-FIXES: T f4;280};281 282// Templated class independent type283template <class V>284struct D5 {285 S f5 /*comment*/ = S();286 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: initializer for member 'f5' is redundant287 // CHECK-FIXES: S f5 /*comment*/;288};289D5<int> d5i;290D5<S> d5s;291 292struct D6 {293 UsesCleanup uc2{};294 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: initializer for member 'uc2' is redundant295 // CHECK-FIXES: UsesCleanup uc2;296};297 298template<typename V>299struct D7 {300 V f7;301};302 303D7<int> d7i;304D7<S> d7s;305 306struct SS {307 SS() = default;308 SS(S s) : s(s) {}309 310 S s;311};312 313struct D8 {314 SS ss = S();315};316 317struct D9 {318 D9() : ss(S()) {}319 SS ss;320};321