1 // RUN: %check_clang_tidy -std=c++11,c++14 %s readability-redundant-string-init %t \
2 // RUN: -config="{CheckOptions: \
3 // RUN: [{key: readability-redundant-string-init.StringNames, \
4 // RUN: value: '::std::basic_string;::std::basic_string_view;our::TestString'}] \
5 // RUN: }"
6 // FIXME: Fix the checker to work in C++17 mode.
7
8 namespace std {
9 template <typename T>
10 class allocator {};
11 template <typename T>
12 class char_traits {};
13 template <typename C, typename T = std::char_traits<C>, typename A = std::allocator<C>>
14 struct basic_string {
15 basic_string();
16 basic_string(const basic_string&);
17 basic_string(const C *, const A &a = A());
18 ~basic_string();
19 };
20 typedef basic_string<char> string;
21 typedef basic_string<wchar_t> wstring;
22
23 template <typename C, typename T = std::char_traits<C>, typename A = std::allocator<C>>
24 struct basic_string_view {
25 using size_type = decltype(sizeof(0));
26
27 basic_string_view();
28 basic_string_view(const basic_string_view &);
29 basic_string_view(const C *, size_type);
30 basic_string_view(const C *);
31 template <class It, class End>
32 basic_string_view(It, End);
33 };
34 typedef basic_string_view<char> string_view;
35 typedef basic_string_view<wchar_t> wstring_view;
36 }
37
f()38 void f() {
39 std::string a = "";
40 // CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization [readability-redundant-string-init]
41 // CHECK-FIXES: std::string a;
42 std::string b("");
43 // CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization
44 // CHECK-FIXES: std::string b;
45 std::string c = R"()";
46 // CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization
47 // CHECK-FIXES: std::string c;
48 std::string d(R"()");
49 // CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization
50 // CHECK-FIXES: std::string d;
51 std::string e{""};
52 // CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization
53 // CHECK-FIXES: std::string e;
54 std::string f = {""};
55 // CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization
56 // CHECK-FIXES: std::string f;
57
58 std::string u = "u";
59 std::string w("w");
60 std::string x = R"(x)";
61 std::string y(R"(y)");
62 std::string z;
63 }
64
fview()65 void fview() {
66 std::string_view a = "";
67 // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization [readability-redundant-string-init]
68 // CHECK-FIXES: std::string_view a;
69 std::string_view b("");
70 // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
71 // CHECK-FIXES: std::string_view b;
72 std::string_view c = R"()";
73 // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
74 // CHECK-FIXES: std::string_view c;
75 std::string_view d(R"()");
76 // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
77 // CHECK-FIXES: std::string_view d;
78 std::string_view e{""};
79 // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
80 // CHECK-FIXES: std::string_view e;
81 std::string_view f = {""};
82 // CHECK-MESSAGES: [[@LINE-1]]:20: warning: redundant string initialization
83 // CHECK-FIXES: std::string_view f;
84
85 std::string_view u = "u";
86 std::string_view w("w");
87 std::string_view x = R"(x)";
88 std::string_view y(R"(y)");
89 std::string_view z;
90 }
91
g()92 void g() {
93 std::wstring a = L"";
94 // CHECK-MESSAGES: [[@LINE-1]]:16: warning: redundant string initialization
95 // CHECK-FIXES: std::wstring a;
96 std::wstring b(L"");
97 // CHECK-MESSAGES: [[@LINE-1]]:16: warning: redundant string initialization
98 // CHECK-FIXES: std::wstring b;
99 std::wstring c = LR"()";
100 // CHECK-MESSAGES: [[@LINE-1]]:16: warning: redundant string initialization
101 // CHECK-FIXES: std::wstring c;
102 std::wstring d(LR"()");
103 // CHECK-MESSAGES: [[@LINE-1]]:16: warning: redundant string initialization
104 // CHECK-FIXES: std::wstring d;
105
106 std::wstring u = L"u";
107 std::wstring w(L"w");
108 std::wstring x = LR"(x)";
109 std::wstring y(LR"(y)");
110 std::wstring z;
111 }
112
gview()113 void gview() {
114 std::wstring_view a = L"";
115 // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization [readability-redundant-string-init]
116 // CHECK-FIXES: std::wstring_view a;
117 std::wstring_view b(L"");
118 // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
119 // CHECK-FIXES: std::wstring_view b;
120 std::wstring_view c = L"";
121 // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
122 // CHECK-FIXES: std::wstring_view c;
123 std::wstring_view d(L"");
124 // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
125 // CHECK-FIXES: std::wstring_view d;
126 std::wstring_view e{L""};
127 // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
128 // CHECK-FIXES: std::wstring_view e;
129 std::wstring_view f = {L""};
130 // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
131 // CHECK-FIXES: std::wstring_view f;
132
133 std::wstring_view u = L"u";
134 std::wstring_view w(L"w");
135 std::wstring_view x = LR"(x)";
136 std::wstring_view y(LR"(y)");
137 std::wstring_view z;
138 }
139
140 template <typename T>
templ()141 void templ() {
142 std::string s = "";
143 // CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization
144 // CHECK-FIXES: std::string s;
145 }
146
147 #define M(x) x
148 #define N { std::string s = ""; }
149 // CHECK-FIXES: #define N { std::string s = ""; }
150
h()151 void h() {
152 templ<int>();
153 templ<double>();
154
155 M({ std::string s = ""; })
156 // CHECK-MESSAGES: [[@LINE-1]]:19: warning: redundant string initialization
157 // CHECK-FIXES: M({ std::string s; })
158
159 N
160 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: redundant string initialization
161 // CHECK-FIXES: N
162 N
163 // CHECK-MESSAGES: [[@LINE-1]]:3: warning: redundant string initialization
164 // CHECK-FIXES: N
165 }
166
167 typedef std::string MyString;
168 #define STRING MyString
169 #define DECL_STRING(name, val) STRING name = val
170
i()171 void i() {
172 MyString a = "";
173 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: redundant string initialization
174 // CHECK-FIXES: MyString a;
175 STRING b = "";
176 // CHECK-MESSAGES: [[@LINE-1]]:10: warning: redundant string initialization
177 // CHECK-FIXES: STRING b;
178 MyString c = "" "" "";
179 // CHECK-MESSAGES: [[@LINE-1]]:12: warning: redundant string initialization
180 // CHECK-FIXES: MyString c;
181 STRING d = "" "" "";
182 // CHECK-MESSAGES: [[@LINE-1]]:10: warning: redundant string initialization
183 // CHECK-FIXES: STRING d;
184 DECL_STRING(e, "");
185 // CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization
186
187 MyString f = "u";
188 STRING g = "u";
189 DECL_STRING(h, "u");
190 }
191
192 #define EMPTY_STR ""
j()193 void j() {
194 std::string a(EMPTY_STR);
195 // CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization
196 // CHECK-FIXES: std::string a;
197 std::string b = (EMPTY_STR);
198 // CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization
199 // CHECK-FIXES: std::string b;
200
201 std::string c(EMPTY_STR "u" EMPTY_STR);
202 }
203
k()204 void k() {
205 std::string a = "", b = "", c = "";
206 // CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization
207 // CHECK-MESSAGES: [[@LINE-2]]:23: warning: redundant string initialization
208 // CHECK-MESSAGES: [[@LINE-3]]:31: warning: redundant string initialization
209 // CHECK-FIXES: std::string a, b, c;
210
211 std::string d = "u", e = "u", f = "u";
212
213 std::string g = "u", h = "", i = "uuu", j = "", k;
214 // CHECK-MESSAGES: [[@LINE-1]]:24: warning: redundant string initialization
215 // CHECK-MESSAGES: [[@LINE-2]]:43: warning: redundant string initialization
216 // CHECK-FIXES: std::string g = "u", h, i = "uuu", j, k;
217 }
218
219 // These cases should not generate warnings.
220 extern void Param1(std::string param = "");
221 extern void Param2(const std::string& param = "");
Param3(std::string param="")222 void Param3(std::string param = "") {}
Param4(STRING param="")223 void Param4(STRING param = "") {}
224
225 namespace our {
226 struct TestString {
227 TestString();
228 TestString(const TestString &);
229 TestString(const char *);
230 ~TestString();
231 };
232 }
233
ourTestStringTests()234 void ourTestStringTests() {
235 our::TestString a = "";
236 // CHECK-MESSAGES: [[@LINE-1]]:19: warning: redundant string initialization
237 // CHECK-FIXES: our::TestString a;
238 our::TestString b("");
239 // CHECK-MESSAGES: [[@LINE-1]]:19: warning: redundant string initialization
240 // CHECK-FIXES: our::TestString b;
241 our::TestString c = R"()";
242 // CHECK-MESSAGES: [[@LINE-1]]:19: warning: redundant string initialization
243 // CHECK-FIXES: our::TestString c;
244 our::TestString d(R"()");
245 // CHECK-MESSAGES: [[@LINE-1]]:19: warning: redundant string initialization
246 // CHECK-FIXES: our::TestString d;
247
248 our::TestString u = "u";
249 our::TestString w("w");
250 our::TestString x = R"(x)";
251 our::TestString y(R"(y)");
252 our::TestString z;
253 }
254
255 namespace their {
256 using TestString = our::TestString;
257 }
258
259 // their::TestString is the same type so should warn / fix
theirTestStringTests()260 void theirTestStringTests() {
261 their::TestString a = "";
262 // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
263 // CHECK-FIXES: their::TestString a;
264 their::TestString b("");
265 // CHECK-MESSAGES: [[@LINE-1]]:21: warning: redundant string initialization
266 // CHECK-FIXES: their::TestString b;
267 }
268
269 namespace other {
270 // Identical declarations to above but different type
271 struct TestString {
272 TestString();
273 TestString(const TestString &);
274 TestString(const char *);
275 ~TestString();
276 };
277
278 // Identical declarations to above but different type
279 template <typename T>
280 class allocator {};
281 template <typename T>
282 class char_traits {};
283 template <typename C, typename T = std::char_traits<C>, typename A = std::allocator<C>>
284 struct basic_string {
285 basic_string();
286 basic_string(const basic_string &);
287 basic_string(const C *, const A &a = A());
288 ~basic_string();
289 };
290 typedef basic_string<char> string;
291 typedef basic_string<wchar_t> wstring;
292 }
293
294 // other::TestString, other::string, other::wstring are unrelated to the types
295 // being checked. No warnings / fixes should be produced for these types.
otherTestStringTests()296 void otherTestStringTests() {
297 other::TestString a = "";
298 other::TestString b("");
299 other::string c = "";
300 other::string d("");
301 other::wstring e = L"";
302 other::wstring f(L"");
303 }
304
305 class Foo {
306 std::string A = "";
307 // CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization
308 // CHECK-FIXES: std::string A;
309 std::string B;
310 std::string C;
311 std::string D;
312 std::string E = "NotEmpty";
313
314 public:
315 // Check redundant constructor where Field has a redundant initializer.
Foo()316 Foo() : A("") {}
317 // CHECK-MESSAGES: [[@LINE-1]]:11: warning: redundant string initialization
318 // CHECK-FIXES: Foo() {}
319
320 // Check redundant constructor where Field has no initializer.
Foo(char)321 Foo(char) : B("") {}
322 // CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization
323 // CHECK-FIXES: Foo(char) {}
324
325 // Check redundant constructor where Field has a valid initializer.
Foo(long)326 Foo(long) : E("") {}
327 // CHECK-MESSAGES: [[@LINE-1]]:15: warning: redundant string initialization
328 // CHECK-FIXES: Foo(long) : E() {}
329
330 // Check how it handles removing 1 initializer, and defaulting the other.
Foo(int)331 Foo(int) : B(""), E("") {}
332 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: redundant string initialization
333 // CHECK-MESSAGES: [[@LINE-2]]:21: warning: redundant string initialization
334 // CHECK-FIXES: Foo(int) : E() {}
335
Foo(short)336 Foo(short) : B{""} {}
337 // CHECK-MESSAGES: [[@LINE-1]]:16: warning: redundant string initialization
338 // CHECK-FIXES: Foo(short) {}
339
Foo(float)340 Foo(float) : A{""}, B{""} {}
341 // CHECK-MESSAGES: [[@LINE-1]]:16: warning: redundant string initialization
342 // CHECK-MESSAGES: [[@LINE-2]]:23: warning: redundant string initialization
343 // CHECK-FIXES: Foo(float) {}
344
345 // Check how it handles removing some redundant initializers while leaving
346 // valid initializers intact.
Foo(std::string Arg)347 Foo(std::string Arg) : A(Arg), B(""), C("NonEmpty"), D(R"()"), E("") {}
348 // CHECK-MESSAGES: [[@LINE-1]]:34: warning: redundant string initialization
349 // CHECK-MESSAGES: [[@LINE-2]]:56: warning: redundant string initialization
350 // CHECK-MESSAGES: [[@LINE-3]]:66: warning: redundant string initialization
351 // CHECK-FIXES: Foo(std::string Arg) : A(Arg), C("NonEmpty"), E() {}
352 };
353