1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <rtl/ustring.hxx>
#include <o3tl/cow_wrapper.hxx>
#include <vector>

struct S1 {
    OUString mv1;
    OUString const & get() const { return mv1; }
    OUString const & get2(bool) const { return mv1; }
};
struct S2 {
    OUString mv1;
    OUString mv2;
    OUString mv3[2];
    S1 child;
    static OUString gs1;
    o3tl::cow_wrapper<S1> mxCow;

    // make sure we ignore cases where the passed in parameter is std::move'd
    S2(OUString v1, OUString v2)
      : mv1(std::move(v1)), mv2((std::move(v2))) {}

    OUString get1() { return mv1; } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}<--- The function 'get1' is never used.
    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]}}
    OUString get3() { return child.mv1; } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}<--- The function 'get3' is never used.
    OUString get4() { return mv3[0]; } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}<--- The function 'get4' is never used.
    OUString get5() { return gs1; } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}<--- The function 'get5' is never used.
    OUString const & get6() { return gs1; }
    OUString get7() { return get6(); } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}<--- The function 'get7' is never used.
    OUString & get8() { return gs1; }
    OUString get9() { return get8(); } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}<--- The function 'get9' is never used.
    // TODO
    OUString get10() { return OUString(*&get6()); } // todoexpected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}<--- The function 'get10' is never used.
    OUString get11() const { return mxCow->get(); } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}<--- The function 'get11' is never used.
    OUString get12() { return child.get2(false); } // expected-error {{rather return class rtl::OUString by const& than by value, to avoid unnecessary copying [loplugin:passstuffbyref]}}<--- The function 'get12' is never used.

    // no warning expected
    OUString set1() { return OUString("xxx"); }<--- The function 'set1' is never used.
    OUString set2() { OUString v1("xxx"); return v1; }
    OUString set3() { S1 v1; return v1.get(); }<--- The function 'set3' is never used.
    OUString set4() { OUString v1[1]; return v1[0]; }<--- The function 'set4' is never used.
    OUString set5(OUString const & s) { return s; }<--- The function 'set5' is never used.
    OUString set6() { std::vector<OUString> v1(1); return v1[0]; }<--- The function 'set6' is never used.
    OUString set7(S1 const & s) { return s.get(); }<--- The function 'set7' is never used.
    OUString set8() { OUString * p = nullptr; return *p; }<--- Null pointer dereference<--- Assignment 'p=nullptr', assigned value is 0<--- The function 'set8' is never used.
};


// no warning expected

// Don't flag stuff where the local var is hidden behind a self-returning operation like -=:
S2 &operator -= ( S2 &t1, const S2 &t2 );
S2 operator-( const S2 &t1, const S2 &t2 )
{
    S2 t0 = t1;
    return t0 -= t2;
}

void f()
{
    S2* s;
    OUString v1, v2;
    s = new S2(v1, v2);<--- Variable 's' is assigned a value that is never used.
}

struct S3 { S3(int); };

S3 f2() {
    static int n;
    return n;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */