1// RUN: %check_clang_tidy -std=c++17 %s modernize-use-designated-initializers %t \
2// RUN: -- \
3// RUN: -- -fno-delayed-template-parsing
4// RUN: %check_clang_tidy -check-suffixes=,SINGLE-ELEMENT -std=c++17 %s modernize-use-designated-initializers %t \
5// RUN: -- -config="{CheckOptions: {modernize-use-designated-initializers.IgnoreSingleElementAggregates: false}}" \
6// RUN: -- -fno-delayed-template-parsing
7// RUN: %check_clang_tidy -check-suffixes=POD -std=c++17 %s modernize-use-designated-initializers %t \
8// RUN: -- -config="{CheckOptions: {modernize-use-designated-initializers.RestrictToPODTypes: true}}" \
9// RUN: -- -fno-delayed-template-parsing
10// RUN: %check_clang_tidy -check-suffixes=,MACROS -std=c++17 %s modernize-use-designated-initializers %t \
11// RUN: -- -config="{CheckOptions: {modernize-use-designated-initializers.IgnoreMacros: false}}" \
12// RUN: -- -fno-delayed-template-parsing
13
14struct S1 {};
15
16S1 s11{};
17S1 s12 = {};
18S1 s13();
19S1 s14;
20
21struct S2 { int i, j; };
22
23S2 s21{.i=1, .j =2};
24
25S2 s22 = {.i: 1, .j: 2};
26// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use designated initializer list to initialize 'S2' [modernize-use-designated-initializers]
27// CHECK-MESSAGES: :[[@LINE-6]]:1: note: aggregate type is defined here
28// CHECK-MESSAGES-POD: :[[@LINE-3]]:10: warning: use designated initializer list to initialize 'S2' [modernize-use-designated-initializers]
29// CHECK-MESSAGES-POD: :[[@LINE-8]]:1: note: aggregate type is defined here
30// CHECK-FIXES: S2 s22 = {.i=1, .j=2};
31
32S2 s23{.i: 1};
33// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use designated initializer list to initialize 'S2' [modernize-use-designated-initializers]
34// CHECK-MESSAGES: :[[@LINE-13]]:1: note: aggregate type is defined here
35// CHECK-MESSAGES-POD: :[[@LINE-3]]:7: warning: use designated initializer list to initialize 'S2' [modernize-use-designated-initializers]
36// CHECK-MESSAGES-POD: :[[@LINE-15]]:1: note: aggregate type is defined here
37// CHECK-FIXES: S2 s23{.i=1};
38
39S2 s24{.i = 1};
40
41S2 s25 = {.i=1, .j: 2};
42// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use designated init expression to initialize field 'j' [modernize-use-designated-initializers]
43// CHECK-MESSAGES-POD: :[[@LINE-2]]:17: warning: use designated init expression to initialize field 'j' [modernize-use-designated-initializers]
44// CHECK-FIXES: S2 s25 = {.i=1, .j=2};
45
46class S3 {
47 public:
48 S2 s2;
49 double d;
50};
51
52S3 s31 = {.s2 = 1, .s2.j: 2, .d: 3.1};
53// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use designated init expression to initialize field 's2.j' [modernize-use-designated-initializers]
54// CHECK-MESSAGES: :[[@LINE-2]]:23: warning: use designated init expression to initialize field 'd' [modernize-use-designated-initializers]
55// CHECK-MESSAGES-POD: :[[@LINE-3]]:20: warning: use designated init expression to initialize field 's2.j' [modernize-use-designated-initializers]
56// CHECK-MESSAGES-POD: :[[@LINE-4]]:23: warning: use designated init expression to initialize field 'd' [modernize-use-designated-initializers]
57// CHECK-FIXES: S3 s31 = {.s2 = 1, .s2.j=2, .d=3.1};
58
59S3 s32 = {.s2: {.i = 1, .j: 2}};
60// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use designated initializer list to initialize 'S3' [modernize-use-designated-initializers]
61// CHECK-MESSAGES: :[[@LINE-15]]:1: note: aggregate type is defined here
62// CHECK-MESSAGES: :[[@LINE-3]]:20: warning: use designated init expression to initialize field 'j' [modernize-use-designated-initializers]
63// CHECK-MESSAGES-POD: :[[@LINE-4]]:10: warning: use designated initializer list to initialize 'S3' [modernize-use-designated-initializers]
64// CHECK-MESSAGES-POD: :[[@LINE-18]]:1: note: aggregate type is defined here
65// CHECK-MESSAGES-POD: :[[@LINE-6]]:20: warning: use designated init expression to initialize field 'j' [modernize-use-designated-initializers]
66// CHECK-FIXES: S3 s32 = {.s2={.i = 1, .j=2}};
67
68S3 s33 = {.s2: {.i: 2}, .d=3.1};
69// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use designated init expression to initialize field 's2' [modernize-use-designated-initializers]
70// CHECK-MESSAGES: :[[@LINE-2]]:11: warning: use designated initializer list to initialize 'S2' [modernize-use-designated-initializers]
71// CHECK-MESSAGES: :[[@LINE-50]]:1: note: aggregate type is defined here
72// CHECK-MESSAGES-POD: :[[@LINE-4]]:11: warning: use designated init expression to initialize field 's2' [modernize-use-designated-initializers]
73// CHECK-MESSAGES-POD: :[[@LINE-5]]:11: warning: use designated initializer list to initialize 'S2' [modernize-use-designated-initializers]
74// CHECK-MESSAGES-POD: :[[@LINE-53]]:1: note: aggregate type is defined here
75// CHECK-FIXES: S3 s33 = {.s2={.i=2}, .d=3.1};
76
77struct S4 {
78 double d;
79 private: static int i;
80};
81
82S4 s41 {.d: 2.2};
83// CHECK-MESSAGES-SINGLE-ELEMENT: :[[@LINE-1]]:8: warning: use designated initializer list to initialize 'S4' [modernize-use-designated-initializers]
84// CHECK-MESSAGES-SINGLE-ELEMENT: :[[@LINE-7]]:1: note: aggregate type is defined here
85// CHECK-FIXES-SINGLE-ELEMENT: S4 s41 {.d=2.2};
86
87S4 s42 = {.d: {}};
88// CHECK-MESSAGES-SINGLE-ELEMENT: :[[@LINE-1]]:10: warning: use designated initializer list to initialize 'S4' [modernize-use-designated-initializers]
89// CHECK-MESSAGES-SINGLE-ELEMENT: :[[@LINE-12]]:1: note: aggregate type is defined here
90// CHECK-FIXES-SINGLE-ELEMENT: S4 s42 = {.d={}};
91
92template<typename S> S template1() { return {10, 11}; }
93
94S2 s26 = template1<S2>();
95
96template<typename S> S template2() { return {}; }
97
98S2 s27 = template2<S2>();
99
100struct S5: S2 { int x, y; };
101
102S5 s51 {1, 2, .x = 3, .y = 4};
103
104struct S6 {
105 int i;
106 struct { int j; } s;
107};
108
109S6 s61 {.i: 1, .s.j: 2};
110// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use designated initializer list to initialize 'S6' [modernize-use-designated-initializers]
111// CHECK-MESSAGES: :[[@LINE-7]]:1: note: aggregate type is defined here
112// CHECK-MESSAGES-POD: :[[@LINE-3]]:8: warning: use designated initializer list to initialize 'S6' [modernize-use-designated-initializers]
113// CHECK-MESSAGES-POD: :[[@LINE-9]]:1: note: aggregate type is defined here
114// CHECK-FIXES: S6 s61 {.i=1, .s.j=2};
115
116struct S7 {
117 union {
118 int k;
119 double d;
120 } u;
121};
122
123S7 s71 {.u.k: 1};
124// CHECK-MESSAGES-SINGLE-ELEMENT: :[[@LINE-1]]:8: warning: use designated initializer list to initialize 'S7' [modernize-use-designated-initializers]
125// CHECK-MESSAGES-SINGLE-ELEMENT: :[[@LINE-9]]:1: note: aggregate type is defined here
126// CHECK-FIXES-SINGLE-ELEMENT: S7 s71 {.u.k=1};
127
128struct S8: S7 { int i; };
129
130S8 s81{1, .i: 2};
131
132struct S9 {
133 int i, j;
134 S9 &operator=(S9);
135};
136
137S9 s91{.i: 1, .j: 2};
138// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use designated initializer list to initialize 'S9' [modernize-use-designated-initializers]
139// CHECK-MESSAGES: :[[@LINE-7]]:1: note: aggregate type is defined here
140// CHECK-FIXES: S9 s91{.i=1, .j=2};
141
142struct S10 { int i = 1, j = 2; };
143
144S10 s101 {.i: 1, .j=2};
145// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use designated init expression to initialize field 'i' [modernize-use-designated-initializers]
146// CHECK-FIXES: S10 s101 {.i=1, .j=2};
147
148struct S11 { int i; S10 s10; };
149
150S11 s111 { .i = 1 };
151S11 s112 { .i: 1 };
152// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use designated initializer list to initialize 'S11' [modernize-use-designated-initializers]
153// CHECK-MESSAGES: :[[@LINE-5]]:1: note: aggregate type is defined here
154// CHECK-FIXES: S11 s112 { .i=1 };
155
156S11 s113 { .i=1, .s10: {}};
157// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use designated init expression to initialize field 's10' [modernize-use-designated-initializers]
158// CHECK-FIXES: S11 s113 { .i=1, .s10={}};
159
160S11 s114 { .i=1, .s10={.i: 1, .j=2}};
161// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use designated init expression to initialize field 'i' [modernize-use-designated-initializers]
162// CHECK-FIXES: S11 s114 { .i=1, .s10={.i=1, .j=2}};
163
164struct S12 {
165 int i;
166 struct { int j; };
167};
168
169S12 s121 {.i: 1, .j: 2};
170// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use designated initializer list to initialize 'S12' [modernize-use-designated-initializers]
171// CHECK-MESSAGES: :[[@LINE-7]]:1: note: aggregate type is defined here
172// CHECK-MESSAGES-POD: :[[@LINE-3]]:10: warning: use designated initializer list to initialize 'S12' [modernize-use-designated-initializers]
173// CHECK-MESSAGES-POD: :[[@LINE-9]]:1: note: aggregate type is defined here
174// CHECK-FIXES: S12 s121 {.i=1, .j=2};
175
176struct S13 {
177 union {
178 int k;
179 double d;
180 };
181 int i;
182};
183
184S13 s131 {.k: 1, .i: 2};
185// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use designated initializer list to initialize 'S13' [modernize-use-designated-initializers]
186// CHECK-MESSAGES: :[[@LINE-10]]:1: note: aggregate type is defined here
187// CHECK-MESSAGES-POD: :[[@LINE-3]]:10: warning: use designated initializer list to initialize 'S13' [modernize-use-designated-initializers]
188// CHECK-MESSAGES-POD: :[[@LINE-12]]:1: note: aggregate type is defined here
189// CHECK-FIXES: S13 s131 {.k=1, .i=2};
190
191#define A (3+2)
192#define B .j=1
193
194S9 s92 {A, B};
195// CHECK-MESSAGES-MACROS: :[[@LINE-1]]:9: warning: use designated init expression to initialize field 'i' [modernize-use-designated-initializers]
196// CHECK-MESSAGES-MACROS: :[[@LINE-5]]:11: note: expanded from macro 'A'
197
198#define DECLARE_S93 S9 s93 {1, 2}
199
200DECLARE_S93;
201// CHECK-MESSAGES-MACROS: :[[@LINE-1]]:1: warning: use designated initializer list to initialize 'S9' [modernize-use-designated-initializers]
202// CHECK-MESSAGES-MACROS: :[[@LINE-4]]:28: note: expanded from macro 'DECLARE_S93'
203// CHECK-MESSAGES-MACROS: :[[@LINE-71]]:1: note: aggregate type is defined here
204

source code of clang-tools-extra/test/clang-tidy/checkers/modernize/use-designated-initializers.cpp