bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / test / stringadd.cxx
blob748ee35cfe61d58be8407455cdcbe4e230247c21
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 // ---------------------------------------------------------------
16 // += tests
18 namespace test1
20 static const char XXX1[] = "xxx";
21 static const char XXX2[] = "xxx";
22 void f1(OUString s1, int i, OString o)
24 OUString s2 = s1;
25 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
26 s2 += "xxx";
27 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
28 s2 += "xxx";
29 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
30 s2 += s1;
31 s2 = s1 + "xxx";
32 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
33 s2 += s1;
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]}}
37 s2 += XXX1;
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)
46 OString s2 = s1;
47 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
48 s2 += "xxx";
49 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
50 s2 += "xxx";
51 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
52 s2 += s1;
53 s2 = s1 + "xxx";
54 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
55 s2 += s1;
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]}}
66 aFirstStr += "...";
68 OUString side_effect();
69 void f4(int i)
71 OUString s1;
72 OUString s2("xxx");
73 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
74 s2 += "xxx";
75 ++i;
76 // any other kind of statement breaks the chain (at least for now)
77 s2 += "xxx";
78 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
79 s2 += side_effect();
80 s1 += "yyy";
81 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
82 s1 += "yyy";
86 namespace test2
88 void f(OUString s3)
90 s3 += "xxx";
91 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
92 s3 += "xxx";
94 void g(OString s3)
96 s3 += "xxx";
97 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
98 s3 += "xxx";
102 namespace test3
104 struct Bar
106 OUString m_field;
108 void f(Bar b1, Bar& b2, Bar* b3)
110 OUString s3 = "xxx";
111 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
112 s3 += b1.m_field;
113 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
114 s3 += b2.m_field;
115 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
116 s3 += b3->m_field;
118 OUString side_effect();
119 void f2(OUString s)
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]}}
125 sRet += "xxx";
126 sRet += side_effect();
127 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
128 sRet += "xxx";
129 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
130 sRet += "xxx";
131 sRet += s;
132 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
133 sRet += "xxx";
137 // no warning expected
138 namespace test4
140 OUString side_effect();
141 void f()
143 OUString sRet = "xxx";
144 #if OSL_DEBUG_LEVEL > 0
145 sRet += ";";
146 #endif
147 sRet += " ";
148 // expected-error@+1 {{simplify by merging with the preceding assignment [loplugin:stringadd]}}
149 sRet += side_effect();
153 // no warning expected
154 namespace test5
156 OUString side_effect();
157 void f()
159 OUString sRet = side_effect();
160 sRet += side_effect();
164 namespace test6
166 void f(OUString sComma, OUString maExtension, int mnDocumentIconID)
168 OUString sValue;
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;
174 struct Foo
176 OUString sFormula1;
178 void g(int x, const Foo& aValidation)
180 OUString sCondition;
181 switch (x)
183 case 1:
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 +
194 namespace test9
196 OUString getByValue();
197 const OUString& getByRef();
198 void f1(OUString s, OUString t, int i, const char* pChar)
200 // no warning expected
201 t = t + "xxx";
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
208 OUString a;
209 a = a + getByValue();
211 // no warning expected
212 OUString b;
213 b = b + (i == 1 ? "aaa" : "bbb");
215 // no warning expected
216 OUString c;
217 c = c + OUString(pChar, strlen(pChar), RTL_TEXTENCODING_UTF8);
219 void f2(char ch)
221 OString s;
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]}}
225 s = s + OString(ch);
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */