Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / test / stringview.cxx
blobb7284cd5ed574b11e13387153cd30d3f9a8ea880
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 <sal/config.h>
12 #include <string_view>
13 #include <utility>
15 #include <rtl/strbuf.hxx>
16 #include <rtl/string.hxx>
17 #include <rtl/ustrbuf.hxx>
18 #include <rtl/ustring.hxx>
19 #include <sal/types.h>
21 void call_view(std::u16string_view) {}
22 void call_view(std::string_view) {}
23 struct ConstructWithView
25 ConstructWithView(std::u16string_view) {}
26 ConstructWithView(std::string_view) {}
29 namespace test1
31 void f1(std::u16string_view s1)
33 // no warning expected
34 call_view(s1);
36 void f1(std::string_view s1)
38 // no warning expected
39 call_view(s1);
41 void f1(OUString s1)
43 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
44 call_view(s1.copy(1, 2));
45 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
46 call_view(s1.copy(1));
47 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
48 ConstructWithView(s1.copy(1, 2));
49 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
50 ConstructWithView(s1.copy(1));
52 void f1(OString s1)
54 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
55 call_view(s1.copy(1, 2));
56 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
57 call_view(s1.copy(1));
58 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
59 ConstructWithView(s1.copy(1, 2));
60 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
61 ConstructWithView(s1.copy(1));
63 void f1(OUStringBuffer s1)
65 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
66 call_view(s1.copy(1, 2));
67 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
68 call_view(s1.copy(1));
69 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
70 ConstructWithView(s1.copy(1, 2));
71 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
72 ConstructWithView(s1.copy(1));
73 // expected-error@+1 {{rather than call toString, pass with a view [loplugin:stringview]}}
74 call_view(s1.toString());
75 // expected-error@+1 {{rather than call toString, pass with a view [loplugin:stringview]}}
76 ConstructWithView(s1.toString());
78 void f1(OStringBuffer s1)
80 // expected-error@+1 {{rather than call toString, pass with a view [loplugin:stringview]}}
81 call_view(s1.toString());
82 // expected-error@+1 {{rather than call toString, pass with a view [loplugin:stringview]}}
83 ConstructWithView(s1.toString());
85 void makeStringAndClear(OUStringBuffer s)
87 call_view(s.makeStringAndClear());
88 ConstructWithView(s.makeStringAndClear());
89 call_view((&s)->makeStringAndClear());
90 ConstructWithView((&s)->makeStringAndClear());
91 // expected-error@+1 {{rather than call makeStringAndClear on an rvalue, pass with a view [loplugin:stringview]}}
92 call_view(std::move(s).makeStringAndClear());
93 // expected-error@+1 {{rather than call makeStringAndClear on an rvalue, pass with a view [loplugin:stringview]}}
94 ConstructWithView(std::move(s).makeStringAndClear());
95 // expected-error@+1 {{rather than call makeStringAndClear on an rvalue, pass with a view [loplugin:stringview]}}
96 call_view((s).copy(1).makeStringAndClear());
97 // expected-error@+1 {{rather than call makeStringAndClear on an rvalue, pass with a view [loplugin:stringview]}}
98 ConstructWithView(s.copy(1).makeStringAndClear());
100 void makeStringAndClear(OStringBuffer s)
102 call_view(s.makeStringAndClear());
103 ConstructWithView(s.makeStringAndClear());
104 call_view((&s)->makeStringAndClear());
105 ConstructWithView((&s)->makeStringAndClear());
106 // expected-error@+1 {{rather than call makeStringAndClear on an rvalue, pass with a view [loplugin:stringview]}}
107 call_view(std::move(s).makeStringAndClear());
108 // expected-error@+1 {{rather than call makeStringAndClear on an rvalue, pass with a view [loplugin:stringview]}}
109 ConstructWithView(std::move(s).makeStringAndClear());
113 namespace test2
115 void f3(OUString s1)
117 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
118 OUString s2 = s1.copy(1, 2) + "xxx";
119 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
120 s2 = s1.copy(1) + "xxx";
121 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
122 s2 = "xxx" + s1.copy(1);
123 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
124 s2 += s1.copy(1);
125 (void)s2;
127 void f3(OString s1)
129 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
130 OString s2 = s1.copy(1, 2) + "xxx";
131 (void)s2;
132 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
133 OString s3 = s1.copy(1) + "xxx";
134 (void)s3;
138 namespace test3
140 void f4(OUString s1, OUString s2)
142 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
143 sal_Unicode x = s2.copy(1, 2)[12];
144 (void)x;
145 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
146 if (s2.copy(1, 2) < s1)
151 void f5(char const* s1, sal_Int32 n1, char16_t const* s2, sal_Int32 n2, OString s3, OUString s4)
153 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OString', pass a 'std::string_view' [loplugin:stringview]}}
154 call_view(OString());
155 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OString' constructed from a 'const char{{ ?}}[4]', pass a 'std::string_view' [loplugin:stringview]}}
156 call_view(OString("foo"));
157 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OString' constructed from a 'const char', pass a 'std::string_view' (or an 'rtl::OStringChar') [loplugin:stringview]}}
158 call_view(OString(*s1));
159 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OString' constructed from a 'const char *', pass a 'std::string_view' [loplugin:stringview]}}
160 call_view(OString(s1));
161 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OString' constructed from a 'const char *', pass a 'std::string_view' [loplugin:stringview]}}
162 call_view(OString(s1, n1));
163 constexpr OStringLiteral l1("foo");
164 call_view(OString(l1));
165 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OString' constructed from a 'std::string_view' (aka 'basic_string_view<char>'), pass a 'std::string_view' [loplugin:stringview]}}
166 call_view(OString(std::string_view("foo")));
167 // expected-error-re@+1 {{instead of an {{'(rtl::)?}}OString' constructed from a {{'(rtl::)?StringNumber<char, 33>'|'OStringNumber<33>' \(aka 'StringNumber<char, 33ULL?>'\)}}, pass a 'std::string_view' [loplugin:stringview]}}
168 call_view(OString(OString::number(0)));
169 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OString' constructed from a 'OStringConcat<{{(rtl::)?}}OString, {{(rtl::)?}}OString>' (aka 'StringConcat<char, rtl::OString, rtl::OString>'), pass a 'std::string_view' via 'rtl::Concat2View' [loplugin:stringview]}}
170 call_view(OString(s3 + s3));
171 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OUString', pass a 'std::u16string_view' [loplugin:stringview]}}
172 call_view(OUString());
173 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OUString' constructed from a 'const char{{ ?}}[4]', pass a 'std::u16string_view' [loplugin:stringview]}}
174 call_view(OUString("foo"));
175 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OUString' constructed from a 'const char16_t{{ ?}}[4]', pass a 'std::u16string_view' [loplugin:stringview]}}
176 call_view(OUString(u"foo"));
177 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OUString' constructed from a 'const char', pass a 'std::u16string_view' (or an 'rtl::OUStringChar') [loplugin:stringview]}}
178 call_view(OUString(*s1));
179 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OUString' constructed from a 'const char16_t', pass a 'std::u16string_view' (or an 'rtl::OUStringChar') [loplugin:stringview]}}
180 call_view(OUString(*s2));
181 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OUString' constructed from a 'const char16_t *', pass a 'std::u16string_view' [loplugin:stringview]}}
182 call_view(OUString(s2));
183 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OUString' constructed from a 'const char16_t *', pass a 'std::u16string_view' [loplugin:stringview]}}
184 call_view(OUString(s2, n2));
185 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OUString' constructed from a 'const char16_t *', pass a 'std::u16string_view' (or an 'rtl::OUStringChar') [loplugin:stringview]}}
186 call_view(OUString(s2, 1));
187 constexpr OUStringLiteral l2(u"foo");
188 call_view(OUString(l2));
189 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OUString' constructed from a 'std::u16string_view' (aka 'basic_string_view<char16_t>'), pass a 'std::u16string_view' [loplugin:stringview]}}
190 call_view(OUString(std::u16string_view(u"foo")));
191 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OUString' constructed from a {{'(rtl::)?StringNumber<char16_t, 33>'|'OUStringNumber<33>' \(aka 'StringNumber<char16_t, 33ULL?>'\)}}, pass a 'std::u16string_view' [loplugin:stringview]}}
192 call_view(OUString(OUString::number(0)));
193 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OUString' constructed from a 'OUStringConcat<{{(rtl::)?}}OUString, {{(rtl::)?}}OUString>' (aka 'StringConcat<char16_t, rtl::OUString, rtl::OUString>'), pass a 'std::u16string_view' via 'rtl::Concat2View' [loplugin:stringview]}}
194 call_view(OUString(s4 + s4));
196 (void)(s3 == l1);
197 (void)(s4 == l2);
200 void f5(OUString s)
202 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
203 OUStringBuffer buf(s.copy(5));
204 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
205 buf = s.copy(5);
206 // expected-error@+1 {{rather than copy, pass with a view using subView() [loplugin:stringview]}}
207 buf.append(s.copy(12));
208 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OUString' constructed from a 'std::u16string_view' (aka 'basic_string_view<char16_t>'), pass a 'std::u16string_view' [loplugin:stringview]}}
209 buf.append(OUString(std::u16string_view(u"foo")));
210 // expected-error-re@+1 {{instead of an '{{(rtl::)?}}OUString' constructed from a 'std::u16string_view' (aka 'basic_string_view<char16_t>'), pass a 'std::u16string_view' [loplugin:stringview]}}
211 s += OUString(std::u16string_view(u"foo"));
214 void f6(OUString s)
216 // expected-error@+1 {{rather than getToken, pass with a view using o3tl::getToken() [loplugin:stringview]}}
217 s.getToken(1, ' ').toInt32();
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */