Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / test / unnecessarygetstr.cxx
blobbb5fcc20d2ef50d64a4eccf88cab4ac99160f28e
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 <ostream>
13 #include <string_view>
15 #include <rtl/strbuf.hxx>
16 #include <rtl/string.hxx>
17 #include <rtl/ustrbuf.hxx>
18 #include <rtl/ustring.hxx>
19 #include <sal/log.hxx>
21 namespace test1
23 void f1(bool, const OString& s);
24 struct Foo
26 void f1(bool, const OString& s);
28 void test1(Foo& foo)
30 OString s;
31 // expected-error@+1 {{unnecessary call to 'getStr' when passing to OString constructor [loplugin:unnecessarygetstr]}}
32 f1(true, s.getStr());
33 // expected-error@+1 {{unnecessary call to 'getStr' when passing to OString constructor [loplugin:unnecessarygetstr]}}
34 foo.f1(true, s.getStr());
35 // expected-error@+1 {{unnecessary call to 'getStr' when passing to OString constructor [loplugin:unnecessarygetstr]}}
36 foo.f1(true, OString::boolean(true).getStr());
37 // expected-error@+1 {{unnecessary call to 'getStr' when passing to OString constructor [loplugin:unnecessarygetstr]}}
38 foo.f1(true, OString::number(12).getStr());
40 // avoid false +
41 OString aVal = "xx";
42 OUString aCompText
43 = "xx" + OUString(aVal.getStr(), aVal.getLength(), RTL_TEXTENCODING_ASCII_US);
44 (void)aCompText;
48 namespace test2
50 // call to param that takes string_view
51 void f2(bool, std::string_view);
52 void f2(bool, std::u16string_view);
53 struct Foo2
55 void f2(bool, std::string_view);
56 void f2(bool, std::u16string_view);
58 void testOString(Foo2& foo)
60 OString s;
61 // expected-error@+1 {{unnecessary call to 'getStr' when passing to string_view constructor [loplugin:unnecessarygetstr]}}
62 f2(true, s.getStr());
63 // expected-error@+1 {{unnecessary call to 'getStr' when passing to string_view constructor [loplugin:unnecessarygetstr]}}
64 foo.f2(true, s.getStr());
65 // expected-error@+1 {{unnecessary call to 'getStr' when passing to string_view constructor [loplugin:unnecessarygetstr]}}
66 foo.f2(true, OString::boolean(true).getStr());
67 // expected-error@+1 {{unnecessary call to 'getStr' when passing to string_view constructor [loplugin:unnecessarygetstr]}}
68 foo.f2(true, OString::number(12).getStr());
70 void testOUString(Foo2& foo)
72 OUString s;
73 // expected-error@+1 {{unnecessary call to 'getStr' when passing to string_view constructor [loplugin:unnecessarygetstr]}}
74 f2(true, s.getStr());
75 // expected-error@+1 {{unnecessary call to 'getStr' when passing to string_view constructor [loplugin:unnecessarygetstr]}}
76 foo.f2(true, s.getStr());
77 // expected-error@+1 {{unnecessary call to 'getStr' when passing to string_view constructor [loplugin:unnecessarygetstr]}}
78 foo.f2(true, OUString::boolean(true).getStr());
79 // expected-error@+1 {{unnecessary call to 'getStr' when passing to string_view constructor [loplugin:unnecessarygetstr]}}
80 foo.f2(true, OUString::number(12).getStr());
84 namespace test3
86 // call to param that takes string_view
87 void f2(bool, std::string_view);
88 struct Foo2
90 void f2(bool, std::string_view);
92 void test3(Foo2& foo)
94 std::string s;
95 // expected-error@+1 {{unnecessary call to 'c_str' when passing to string_view constructor [loplugin:unnecessarygetstr]}}
96 f2(true, s.c_str());
97 // expected-error@+1 {{unnecessary call to 'c_str' when passing to string_view constructor [loplugin:unnecessarygetstr]}}
98 foo.f2(true, s.c_str());
102 namespace test4
104 void test()
106 std::string s;
107 // expected-error@+1 {{unnecessary call to 'c_str' when passing to OUString::createFromAscii [loplugin:unnecessarygetstr]}}
108 OUString::createFromAscii(s.c_str());
112 namespace test5
114 void test(std::string v, OString o)
116 // expected-error@+1 {{unnecessary call to 'c_str' when passing to string_view constructor [loplugin:unnecessarygetstr]}}
117 std::string_view s1(v.c_str());
118 // expected-error@+1 {{unnecessary call to 'getStr' when passing to string constructor [loplugin:unnecessarygetstr]}}
119 std::string s2(o.getStr());
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */