Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / test / passstuffbyref.cxx
blobd90d6f05ba9fe035df905341359d79df1d9e09be
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/ustring.hxx>
11 #include <o3tl/cow_wrapper.hxx>
12 #include <vector>
14 #pragma clang diagnostic ignored "-Wunknown-warning-option" // for Clang < 13
15 #pragma clang diagnostic ignored "-Wunused-but-set-variable"
17 struct S1 {
18 OUString mv1;
19 OUString const & get() const { return mv1; }
20 OUString const & get2(bool) const { return mv1; }
22 struct S2 {
23 OUString mv1;
24 OUString mv2;
25 OUString mv3[2];
26 S1 child;
27 static OUString gs1;
28 o3tl::cow_wrapper<S1> mxCow;
30 // make sure we ignore cases where the passed in parameter is std::move'd
31 S2(OUString v1, OUString v2)
32 : mv1(std::move(v1)), mv2((std::move(v2))) {}
34 OUString get1() { return mv1; } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
35 OUString get2(bool b) { return b ? mv1 : mv2; } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
36 OUString get3() { return child.mv1; } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
37 OUString get4() { return mv3[0]; } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
38 OUString get5() { return gs1; } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
39 OUString const & get6() { return gs1; }
40 OUString get7() { return get6(); } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
41 OUString & get8() { return gs1; }
42 OUString get9() { return get8(); } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
43 // TODO
44 OUString get10() { return OUString(*&get6()); } // todoexpected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
45 OUString get11() const { return mxCow->get(); } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
46 OUString get12() { return child.get2(false); } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
48 // no warning expected
49 OUString set1() { return OUString("xxx"); }
50 OUString set2() { OUString v1("xxx"); return v1; }
51 OUString set3() { S1 v1; return v1.get(); }
52 OUString set4() { OUString v1[1]; return v1[0]; }
53 OUString set5(OUString const & s) { return s; }
54 OUString set6() { std::vector<OUString> v1(1); return v1[0]; }
55 OUString set7(S1 const & s) { return s.get(); }
56 OUString set8() { OUString * p = nullptr; return *p; }
60 // no warning expected
62 // Don't flag stuff where the local var is hidden behind a self-returning operation like -=:
63 S2 &operator -= ( S2 &t1, const S2 &t2 );
64 S2 operator-( const S2 &t1, const S2 &t2 )
66 S2 t0 = t1;
67 return t0 -= t2;
70 void f()
72 S2* s;
73 OUString v1, v2;
74 s = new S2(v1, v2);
77 struct S3 { S3(int); };
79 S3 f2() {
80 static int n;
81 return n;
84 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */