1// RUN: %check_clang_tidy -std=c++14-or-later %s modernize-return-braced-init-list %t
2
3namespace std {
4typedef decltype(sizeof(int)) size_t;
5
6// libc++'s implementation
7template <class _E>
8class initializer_list {
9 const _E *__begin_;
10 size_t __size_;
11
12 initializer_list(const _E *__b, size_t __s)
13 : __begin_(__b),
14 __size_(__s) {}
15
16public:
17 typedef _E value_type;
18 typedef const _E &reference;
19 typedef const _E &const_reference;
20 typedef size_t size_type;
21
22 typedef const _E *iterator;
23 typedef const _E *const_iterator;
24
25 initializer_list() : __begin_(nullptr), __size_(0) {}
26
27 size_t size() const { return __size_; }
28 const _E *begin() const { return __begin_; }
29 const _E *end() const { return __begin_ + __size_; }
30};
31
32template <typename T>
33struct allocator {};
34
35template <typename T, typename Allocator = ::std::allocator<T>>
36class vector {
37public:
38 vector(T);
39 vector(size_t, T, const Allocator &alloc = Allocator());
40 vector(std::initializer_list<T>);
41};
42} // namespace std
43
44class Bar {};
45
46Bar b0;
47
48class Foo {
49public:
50 Foo(Bar) {}
51 explicit Foo(Bar, unsigned int) {}
52 Foo(unsigned int) {}
53};
54
55class Baz {
56public:
57 Foo m() {
58 Bar bm;
59 return Foo(bm);
60 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: avoid repeating the return type from the declaration; use a braced initializer list instead [modernize-return-braced-init-list]
61 // CHECK-FIXES: return {bm};
62 }
63};
64
65class Quux : public Foo {
66public:
67 Quux(Bar bar) : Foo(bar) {}
68 Quux(unsigned, unsigned, unsigned k = 0) : Foo(k) {}
69};
70
71Foo f() {
72 Bar b1;
73 return Foo(b1);
74 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
75 // CHECK-FIXES: return {b1};
76}
77
78Foo f2() {
79 Bar b2;
80 return {b2};
81}
82
83auto f3() {
84 Bar b3;
85 return Foo(b3);
86}
87
88#define A(b) Foo(b)
89
90Foo f4() {
91 Bar b4;
92 return A(b4);
93}
94
95Foo f5() {
96 Bar b5;
97 return Quux(b5);
98}
99
100Foo f6() {
101 Bar b6;
102 return Foo(b6, 1);
103}
104
105std::vector<int> vectorWithOneParameter() {
106 int i7 = 1;
107 return std::vector<int>(i7);
108 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
109}
110
111std::vector<int> vectorIntWithTwoParameter() {
112 return std::vector<int>(1, 2);
113}
114
115std::vector<double> vectorDoubleWithTwoParameter() {
116 return std::vector<double>(1, 2.1);
117}
118struct A {};
119std::vector<A> vectorRecordWithTwoParameter() {
120 A a{};
121 return std::vector<A>(1, a);
122}
123
124
125Bar f8() {
126 return {};
127}
128
129Bar f9() {
130 return Bar();
131 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
132}
133
134Bar f10() {
135 return Bar{};
136}
137
138Foo f11(Bar b11) {
139 return Foo(b11);
140 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
141 // CHECK-FIXES: return {b11};
142}
143
144Foo f12() {
145 return f11(b11: Bar());
146}
147
148Foo f13() {
149 return Foo(Bar()); // 13
150 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
151 // CHECK-FIXES: return {Bar()}; // 13
152}
153
154Foo f14() {
155 // FIXME: Type narrowing should not occur!
156 return Foo(-1);
157 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
158 // CHECK-FIXES: return {-1};
159}
160
161Foo f15() {
162 return Foo(f10());
163 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
164 // CHECK-FIXES: return {f10()};
165}
166
167Quux f16() {
168 return Quux(1, 2);
169 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
170 // CHECK-FIXES: return {1, 2};
171}
172
173Quux f17() {
174 return Quux(1, 2, 3);
175 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: avoid repeating the return type
176 // CHECK-FIXES: return {1, 2, 3};
177}
178
179template <typename T>
180T f19() {
181 return T();
182}
183
184Bar i1 = f19<Bar>();
185Baz i2 = f19<Baz>();
186
187template <typename T>
188Foo f20(T t) {
189 return Foo(t);
190}
191
192Foo i3 = f20(t: b0);
193
194template <typename T>
195class BazT {
196public:
197 T m() {
198 Bar b;
199 return T(b);
200 }
201
202 Foo m2(T t) {
203 return Foo(t);
204 }
205};
206
207BazT<Foo> bazFoo;
208Foo i4 = bazFoo.m();
209Foo i5 = bazFoo.m2(t: b0);
210
211BazT<Quux> bazQuux;
212Foo i6 = bazQuux.m();
213Foo i7 = bazQuux.m2(t: b0);
214
215auto v1 = []() { return std::vector<int>({1, 2}); }();
216auto v2 = []() -> std::vector<int> { return std::vector<int>({1, 2}); }();
217

source code of clang-tools-extra/test/clang-tidy/checkers/modernize/return-braced-init-list.cpp