bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / test / passstuffbyref.cxx
blob3f0efb1d106e62067a4bbc18b89dc8b4c7e651c7
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 struct S1 {
15 OUString mv1;
16 OUString const & get() const { return mv1; }
17 OUString const & get2(bool) const { return mv1; }
19 struct S2 {
20 OUString mv1;
21 OUString mv2;
22 OUString mv3[2];
23 S1 child;
24 static OUString gs1;
25 o3tl::cow_wrapper<S1> mxCow;
27 // make sure we ignore cases where the passed in parameter is std::move'd
28 S2(OUString v1, OUString v2)
29 : mv1(std::move(v1)), mv2((std::move(v2))) {}
31 OUString get1() { return mv1; } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
32 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]}}
33 OUString get3() { return child.mv1; } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
34 OUString get4() { return mv3[0]; } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
35 OUString get5() { return gs1; } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
36 OUString const & get6() { return gs1; }
37 OUString get7() { return get6(); } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
38 OUString & get8() { return gs1; }
39 OUString get9() { return get8(); } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
40 // TODO
41 OUString get10() { return OUString(*&get6()); } // todoexpected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
42 OUString get11() const { return mxCow->get(); } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
43 OUString get12() { return child.get2(false); } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}
45 // no warning expected
46 OUString set1() { return OUString("xxx"); }
47 OUString set2() { OUString v1("xxx"); return v1; }
48 OUString set3() { S1 v1; return v1.get(); }
49 OUString set4() { OUString v1[1]; return v1[0]; }
50 OUString set5(OUString const & s) { return s; }
51 OUString set6() { std::vector<OUString> v1(1); return v1[0]; }
52 OUString set7(S1 const & s) { return s.get(); }
53 OUString set8() { OUString * p = nullptr; return *p; }
57 // no warning expected
59 // Don't flag stuff where the local var is hidden behind a self-returning operation like -=:
60 S2 &operator -= ( S2 &t1, const S2 &t2 );
61 S2 operator-( const S2 &t1, const S2 &t2 )
63 S2 t0 = t1;
64 return t0 -= t2;
67 void f()
69 S2* s;
70 OUString v1, v2;
71 s = new S2(v1, v2);
74 struct S3 { S3(int); };
76 S3 f2() {
77 static int n;
78 return n;
81 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */