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 // ---------------------------------------------------------------
20 static const char XXX1
[] = "xxx";
21 static const char XXX2
[] = "xxx";
22 void f1(OUString s1
, int i
, OString o
)
25 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
27 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
29 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
32 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
34 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
35 s2
+= OUString::number(i
);
36 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
38 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
39 s2
+= OUStringLiteral(XXX1
) + XXX2
;
41 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
42 s2
+= OStringToOUString(o
, RTL_TEXTENCODING_UTF8
);
44 void f2(OString s1
, int i
, OUString u
)
47 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
49 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
51 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
54 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
56 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
57 s2
+= OString::number(i
);
59 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
60 s2
+= OUStringToOString(u
, RTL_TEXTENCODING_ASCII_US
);
62 void f3(OUString aStr
, int nFirstContent
)
64 OUString aFirstStr
= aStr
.copy(0, nFirstContent
);
65 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
68 OUString
side_effect();
73 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
76 // any other kind of statement breaks the chain (at least for now)
78 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
81 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
91 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
97 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
108 void f(Bar b1
, Bar
& b2
, Bar
* b3
)
111 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
113 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
115 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
118 OUString
side_effect();
121 OUString sRet
= "xxx";
122 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
123 sRet
+= side_effect();
124 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
126 sRet
+= side_effect();
127 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
129 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
132 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
137 // no warning expected
140 OUString
side_effect();
143 OUString sRet
= "xxx";
144 #if OSL_DEBUG_LEVEL > 0
148 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
149 sRet
+= side_effect();
153 // no warning expected
156 OUString
side_effect();
159 OUString sRet
= side_effect();
160 sRet
+= side_effect();
166 void f(OUString sComma
, OUString maExtension
, int mnDocumentIconID
)
169 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
170 sValue
+= sComma
+ sComma
+ maExtension
+ sComma
;
171 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
172 sValue
+= OUString::number(mnDocumentIconID
) + sComma
;
178 void g(int x
, const Foo
& aValidation
)
184 sCondition
+= "cell-content-is-in-list(";
185 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
186 sCondition
+= aValidation
.sFormula1
+ ")";
191 // ---------------------------------------------------------------
192 // detecting OUString temporary construction in +
196 OUString
getByValue();
197 const OUString
& getByRef();
198 void f1(OUString s
, OUString t
, int i
, const char* pChar
)
200 // no warning expected
202 // expected-error@+1 {{avoid constructing 'rtl::OUString' from 'const char [4]' on RHS of + (where LHS is of type 'rtl::OUString') [loplugin:stringadd]}}
203 s
= s
+ OUString("xxx");
204 // expected-error@+1 {{avoid constructing 'rtl::OUString' from 'const rtl::OUString' on RHS of + (where LHS is of type 'rtl::OUString') [loplugin:stringadd]}}
205 s
= s
+ OUString(getByRef());
207 // no warning expected
209 a
= a
+ getByValue();
211 // no warning expected
213 b
= b
+ (i
== 1 ? "aaa" : "bbb");
215 // no warning expected
217 c
= c
+ OUString(pChar
, strlen(pChar
), RTL_TEXTENCODING_UTF8
);
222 // expected-error@+1 {{avoid constructing 'rtl::OString' from 'const char [4]' on RHS of + (where LHS is of type 'rtl::OString') [loplugin:stringadd]}}
223 s
= s
+ OString("xxx");
224 // expected-error@+1 {{avoid constructing 'rtl::OString' from 'char' on RHS of + (where LHS is of type 'rtl::OString') [loplugin:stringadd]}}
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */