1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
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 // ---------------------------------------------------------------
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
)
30 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
32 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
34 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
37 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
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]}}
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
)
53 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
55 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
57 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
60 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
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]}}
74 OUString
side_effect();
79 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
82 // any other kind of statement breaks the chain (at least for now)
84 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
87 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
97 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
103 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
114 void f(Bar b1
, Bar
& b2
, Bar
* b3
)
117 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
119 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
121 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
124 OUString
side_effect();
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]}}
132 sRet
+= side_effect();
133 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
135 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
138 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
143 // no warning expected
146 OUString
side_effect();
149 OUString sRet
= "xxx";
150 #if OSL_DEBUG_LEVEL > 0
154 // expected-error@+1 {{simplify by merging with the preceding assign/append [loplugin:stringadd]}}
155 sRet
+= side_effect();
159 // no warning expected
162 OUString
side_effect();
165 OUString sRet
= side_effect();
166 sRet
+= side_effect();
172 void f(OUString sComma
, OUString maExtension
, int mnDocumentIconID
)
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
;
184 void g(int x
, const Foo
& aValidation
)
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 +
202 OUString
getByValue();
203 const OUString
& getByRef();
204 void f1(OUString s
, OUString t
, int i
, const char* pChar
)
206 // no warning expected
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
215 a
= a
+ getByValue();
217 // no warning expected
219 b
= b
+ (i
== 1 ? "aaa" : "bbb");
221 // no warning expected
223 c
= c
+ OUString(pChar
, strlen(pChar
), RTL_TEXTENCODING_UTF8
);
226 // expected-error@+1 {{chained append, rather use single append call and + operator [loplugin:stringadd]}}
227 buf
.append(" ").append(b
);
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]}}
243 OString
constStringFunction(int) const;
244 OString
nonConstStringFunction();
245 int constIntFunction() const;
246 int nonConstIntFunction();
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());
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("...");
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");
292 OUStringBuffer
aFirstStr1(12);
293 // no warning expected
294 aFirstStr1
.append("...");
295 // because we have a comment between them
296 aFirstStr1
.append("...");
304 OUStringBuffer
b(16);
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */