Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / test / vclwidgets.cxx
blobc470f991a6678404a267b23419d6ba85c25fd112
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 <vcl/vclptr.hxx>
13 #include <vcl/vclreferencebase.hxx>
15 struct Widget : public VclReferenceBase
17 VclPtr<Widget> mpParent;
19 void widget1()
21 // test that we ignore assignments from a member field
22 Widget* p = mpParent;
23 (void)p;
24 // test against false+
25 p = (true) ? mpParent.get() : nullptr;
28 ~Widget() override
30 disposeOnce();
33 void dispose() override
35 mpParent.clear();
36 VclReferenceBase::dispose();
40 VclPtr<Widget> f()
42 return nullptr;
45 Widget* g()
47 return nullptr;
50 // test the variable init detection
51 void bar()
53 Widget* p = f(); // expected-error {{assigning a returned-by-value VclPtr<T> to a T* variable is dodgy, should be assigned to a VclPtr. If you know that the RHS does not return a newly created T, then add a '.get()' to the RHS [loplugin:vclwidgets]}}
54 (void)p;
55 Widget* q = g();
56 (void)q;
57 Widget* r = nullptr;
58 (void)r;
61 // test the assignment detection
62 void bar2()
64 Widget* p;
65 p = nullptr;
66 p = f(); // expected-error {{assigning a returned-by-value VclPtr<T> to a T* variable is dodgy, should be assigned to a VclPtr. If you know that the RHS does not return a newly created T, then add a '.get()' to the RHS [loplugin:vclwidgets]}}
67 (void)p;
68 Widget* q;
69 q = g();
70 (void)q;
74 // test against false+
76 template<class T>
77 T * get() { return nullptr; }
79 void bar3()
81 Widget* p;
82 p = get<Widget>();
88 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */