Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / test / stringadd.cxx
blob7c11936433032857c2f532d5e0b5363c31589c49
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <rtl/strbuf.hxx>
11 #include <rtl/string.hxx>
12 #include <rtl/ustrbuf.hxx>
13 #include <rtl/ustring.hxx>
15 #pragma clang diagnostic ignored "-Wunknown-warning-option" // for Clang < 13
16 #pragma clang diagnostic ignored "-Wunused-but-set-parameter"
17 #pragma clang diagnostic ignored "-Wunused-but-set-variable"
19 // ---------------------------------------------------------------
20 // += tests
22 namespace test1
24 static const char XXX1[] = "xxx";
25 static constexpr char16_t XXX1u[] = u"xxx";
26 static const char XXX2[] = "xxx";
27 void f1(OUString s1, int i, OString o)
29 OUString s2 = s1;
30 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
31 s2 += "xxx";
32 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
33 s2 += "xxx";
34 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
35 s2 += s1;
36 s2 = s1 + "xxx";
37 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
38 s2 += s1;
39 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
40 s2 += OUString::number(i);
41 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
42 s2 += XXX1;
43 // expected-error-re@+2 {{rather use O[U]String::Concat than constructing '{{(rtl::)?}}OUStringLiteral<4>'{{( \(aka 'rtl::OUStringLiteral<4>'\))?}} from 'const char16_t{{ ?}}[4]' on LHS of + (where RHS is of type 'const char{{ ?}}[4]') [loplugin:stringadd]}}
44 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
45 s2 += OUStringLiteral(XXX1u) + XXX2;
47 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
48 s2 += OStringToOUString(o, RTL_TEXTENCODING_UTF8);
50 void f2(OString s1, int i, OUString u)
52 OString s2 = s1;
53 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
54 s2 += "xxx";
55 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
56 s2 += "xxx";
57 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
58 s2 += s1;
59 s2 = s1 + "xxx";
60 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
61 s2 += s1;
62 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
63 s2 += OString::number(i);
65 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
66 s2 += OUStringToOString(u, RTL_TEXTENCODING_ASCII_US);
68 void f3(OUString aStr, int nFirstContent)
70 OUString aFirstStr = aStr.copy(0, nFirstContent);
71 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
72 aFirstStr += "...";
74 OUString side_effect();
75 void f4(int i)
77 OUString s1;
78 OUString s2("xxx");
79 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
80 s2 += "xxx";
81 ++i;
82 // any other kind of statement breaks the chain (at least for now)
83 s2 += "xxx";
84 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
85 s2 += side_effect();
86 s1 += "yyy";
87 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
88 s1 += "yyy";
92 namespace test2
94 void f(OUString s3)
96 s3 += "xxx";
97 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
98 s3 += "xxx";
100 void g(OString s3)
102 s3 += "xxx";
103 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
104 s3 += "xxx";
108 namespace test3
110 struct Bar
112 OUString m_field;
114 void f(Bar b1, Bar& b2, Bar* b3)
116 OUString s3 = "xxx";
117 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
118 s3 += b1.m_field;
119 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
120 s3 += b2.m_field;
121 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
122 s3 += b3->m_field;
124 OUString side_effect();
125 void f2(OUString s)
127 OUString sRet = "xxx";
128 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
129 sRet += side_effect();
130 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
131 sRet += "xxx";
132 sRet += side_effect();
133 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
134 sRet += "xxx";
135 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
136 sRet += "xxx";
137 sRet += s;
138 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
139 sRet += "xxx";
143 // no warning expected
144 namespace test4
146 OUString side_effect();
147 void f()
149 OUString sRet = "xxx";
150 #if OSL_DEBUG_LEVEL > 0
151 sRet += ";";
152 #endif
153 sRet += " ";
154 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
155 sRet += side_effect();
159 // no warning expected
160 namespace test5
162 OUString side_effect();
163 void f()
165 OUString sRet = side_effect();
166 sRet += side_effect();
170 namespace test6
172 void f(OUString sComma, OUString maExtension, int mnDocumentIconID)
174 OUString sValue;
175 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
176 sValue += sComma + sComma + maExtension + sComma;
177 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
178 sValue += OUString::number(mnDocumentIconID) + sComma;
180 struct Foo
182 OUString sFormula1;
184 void g(int x, const Foo& aValidation)
186 OUString sCondition;
187 switch (x)
189 case 1:
190 sCondition += "cell-content-is-in-list(";
191 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
192 sCondition += aValidation.sFormula1 + ")";
197 // ---------------------------------------------------------------
198 // detecting OUString temporary construction in +
200 namespace test9
202 OUString getByValue();
203 const OUString& getByRef();
204 void f1(OUString s, OUString t, int i, const char* pChar)
206 // no warning expected
207 t = t + "xxx";
208 // expected-error-re@+1 {{rather use O[U]String::Concat than constructing '{{(rtl::)?}}OUString' from 'const char{{ ?}}[4]' on RHS of + (where LHS is of type '{{(rtl::)?}}OUString') [loplugin:stringadd]}}
209 s = s + OUString("xxx");
210 // expected-error-re@+1 {{rather use O[U]String::Concat than constructing '{{(rtl::)?}}OUString' from 'const {{(rtl::)?}}OUString' on RHS of + (where LHS is of type '{{(rtl::)?}}OUString') [loplugin:stringadd]}}
211 s = s + OUString(getByRef());
213 // no warning expected
214 OUString a;
215 a = a + getByValue();
217 // no warning expected
218 OUString b;
219 b = b + (i == 1 ? "aaa" : "bbb");
221 // no warning expected
222 OUString c;
223 c = c + OUString(pChar, strlen(pChar), RTL_TEXTENCODING_UTF8);
225 OUStringBuffer buf;
226 // expected-error@+1 {{chained append, rather use single append call and + operator [loplugin:stringadd]}}
227 buf.append(" ").append(b);
229 void f2(char ch)
231 OString s;
232 // expected-error-re@+1 {{rather use O[U]String::Concat than constructing '{{(rtl::)?}}OString' from 'const char{{ ?}}[4]' on RHS of + (where LHS is of type '{{(rtl::)?}}OString') [loplugin:stringadd]}}
233 s = s + OString("xxx");
234 // expected-error-re@+1 {{rather use O[U]String::Concat than constructing '{{(rtl::)?}}OString' from 'char' on RHS of + (where LHS is of type '{{(rtl::)?}}OString') [loplugin:stringadd]}}
235 s = s + OString(ch);
239 namespace test10
241 struct C
243 OString constStringFunction(int) const;
244 OString nonConstStringFunction();
245 int constIntFunction() const;
246 int nonConstIntFunction();
249 C getC();
251 void f1(C c)
253 OString s;
254 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
255 s += c.constStringFunction(c.constIntFunction());
256 s += c.constStringFunction(c.nonConstIntFunction());
257 s += c.nonConstStringFunction();
258 s += getC().constStringFunction(c.constIntFunction());
262 namespace test11
264 void f1()
266 OUStringBuffer aFirstStr1("aaa");
267 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
268 aFirstStr1.append("...");
269 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
270 aFirstStr1.append("...");
274 namespace test12
276 void f1(int j)
278 OUStringBuffer aFirstStr1(12);
279 // no warning expected
280 aFirstStr1.append("...");
281 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
282 aFirstStr1.append("...");
283 // no warning expected
284 aFirstStr1.append(((j + 1) % 15) ? " " : "\n");
288 namespace test13
290 void f1()
292 OUStringBuffer aFirstStr1(12);
293 // no warning expected
294 aFirstStr1.append("...");
295 // because we have a comment between them
296 aFirstStr1.append("...");
300 namespace test14
302 void f1()
304 OUStringBuffer b(16);
305 b.append("...");
308 void f2(long long n)
310 OUStringBuffer b(n);
311 b.append("...");
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */