Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / test / referencecasting.cxx
blobbeb69cc86616fe237ab535f8ddb528967375c35c
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 "com/sun/star/uno/Sequence.hxx"
13 #include "com/sun/star/uno/XInterface.hpp"
14 #include "com/sun/star/io/XStreamListener.hpp"
15 #include "com/sun/star/io/XInputStream.hpp"
16 #include "com/sun/star/lang/XTypeProvider.hpp"
17 #include "com/sun/star/lang/XComponent.hpp"
18 #include "cppuhelper/implbase.hxx"
19 #include "cppuhelper/weak.hxx"
20 #include "rtl/ref.hxx"
22 void test1(const css::uno::Reference<css::io::XStreamListener>& a)
24 // expected-error@+1 {{the source reference is already a subtype of the destination reference, just use = [loplugin:referencecasting]}}
25 css::uno::Reference<css::lang::XEventListener> b(a, css::uno::UNO_QUERY);
26 // expected-error@+1 {{the source reference is already a subtype of the destination reference, just use = [loplugin:referencecasting]}}
27 auto c = css::uno::Reference<css::lang::XEventListener>::query(a);
30 namespace test2
32 css::uno::Reference<css::io::XStreamListener> getListener();
34 void test()
36 // expected-error@+1 {{the source reference is already a subtype of the destination reference, just use = [loplugin:referencecasting]}}
37 css::uno::Reference<css::lang::XEventListener> b(getListener(), css::uno::UNO_QUERY);
41 namespace test3
43 void callListener(css::uno::Reference<css::uno::XInterface> const&);
45 void test(css::uno::Reference<css::io::XStreamListener> const& l)
47 // expected-error@+1 {{the source reference is already a subtype of the destination reference, just use = [loplugin:referencecasting]}}
48 callListener(css::uno::Reference<css::lang::XEventListener>(l, css::uno::UNO_QUERY));
52 void test4(const css::uno::Reference<css::io::XStreamListener>& a)
54 // no warning expected, used to reject null references
55 css::uno::Reference<css::lang::XEventListener> b(a, css::uno::UNO_SET_THROW);
58 // no warning expected
59 namespace test5
61 void test(css::uno::Reference<css::io::XStreamListener> l)
63 css::uno::Reference<css::uno::XInterface> a = l;
67 namespace test6
69 void test(css::uno::Reference<css::io::XStreamListener> l)
71 css::uno::Reference<css::lang::XEventListener> a;
72 // expected-error@+1 {{the source reference is already a subtype of the destination reference, just use = [loplugin:referencecasting]}}
73 a.set(l, css::uno::UNO_QUERY);
77 namespace test7
79 void test(css::uno::Reference<css::io::XStreamListener> l)
81 // expected-error@+1 {{unnecessary get() call [loplugin:referencecasting]}}
82 css::uno::Reference<css::lang::XEventListener> a(l.get(), css::uno::UNO_QUERY);
83 // expected-error@+1 {{unnecessary get() call [loplugin:referencecasting]}}
84 a.set(l.get(), css::uno::UNO_QUERY);
87 class FooStream : public css::io::XStreamListener
89 virtual ~FooStream();
91 void test(rtl::Reference<FooStream> l)
93 // expected-error@+1 {{unnecessary get() call [loplugin:referencecasting]}}
94 css::uno::Reference<css::io::XStreamListener> a(l.get());
95 // expected-error@+1 {{the source reference is already a subtype of the destination reference, just use = [loplugin:referencecasting]}}
96 a.set(l.get(), css::uno::UNO_QUERY);
97 // expected-error@+1 {{unnecessary get() call [loplugin:referencecasting]}}
98 a.set(l.get());
99 // expected-error@+1 {{the source reference is already a subtype of the destination reference, just use = [loplugin:referencecasting]}}
100 css::uno::Reference<css::io::XStreamListener> b(l.get(), css::uno::UNO_QUERY);
101 // no warning expected
102 css::uno::Reference<css::lang::XTypeProvider> c(l.get(), css::uno::UNO_QUERY);
103 // no warning expected
104 css::uno::Reference<css::io::XStreamListener> a2 = l;
105 (void)a2;
107 css::uno::Sequence<css::uno::Reference<css::io::XStreamListener>> getContinuations()
109 rtl::Reference<FooStream> noel1;
110 // expected-error@+1 {{unnecessary get() call [loplugin:referencecasting]}}
111 return { noel1.get() };
115 namespace test8
117 void test(css::io::XStreamListener* l)
119 // expected-error@+1 {{the source reference is already a subtype of the destination reference, just use = [loplugin:referencecasting]}}
120 css::uno::Reference<css::lang::XEventListener> a(l, css::uno::UNO_QUERY);
121 // expected-error@+1 {{the source reference is already a subtype of the destination reference, just use = [loplugin:referencecasting]}}
122 a.set(l, css::uno::UNO_QUERY);
126 // check for looking through casts
127 namespace test9
129 class StatusbarController : public css::io::XStreamListener, public ::cppu::OWeakObject
133 void test(StatusbarController* pController)
135 css::uno::Reference<css::io::XStreamListener> xController;
136 // expected-error@+1 {{the source reference is already a subtype of the destination reference, just use = [loplugin:referencecasting]}}
137 xController.set(static_cast<::cppu::OWeakObject*>(pController), css::uno::UNO_QUERY);
141 // no warning expected when we have an ambiguous base
142 namespace test10
144 class Foo : public css::lang::XTypeProvider, public css::lang::XComponent
146 virtual ~Foo();
147 void bar()
149 css::uno::Reference<css::lang::XEventListener> xSource(
150 static_cast<css::lang::XTypeProvider*>(this), css::uno::UNO_QUERY);
155 // no warning expected for SAL_NO_ACQUIRE
156 namespace test11
158 void test(css::io::XStreamListener* l)
160 css::uno::Reference<css::lang::XEventListener> a(l, SAL_NO_ACQUIRE);
161 a.set(l, SAL_NO_ACQUIRE);
165 // no warning expected: querying for XInterface (instead of doing an upcast) has special semantics,
166 // to check for UNO object equivalence.
167 void test12(const css::uno::Reference<css::io::XStreamListener>& a)
169 css::uno::Reference<css::uno::XInterface> b(a, css::uno::UNO_QUERY);
172 // no warning expected: querying for XInterface (instead of doing an upcast) has special semantics,
173 // to check for UNO object equivalence.
174 struct Test13
176 css::uno::Reference<css::uno::XInterface> m_xNormalizedIFace;
177 void newObject(const css::uno::Reference<css::uno::XInterface>& _rxIFace)
179 m_xNormalizedIFace.set(_rxIFace, css::uno::UNO_QUERY);
183 void test14(css::uno::Sequence<css::uno::Reference<css::io::XStreamListener>> seq)
185 for (sal_Int32 i = 0; i < seq.getLength(); ++i)
187 // expected-error@+1 {{the source reference is already a subtype of the destination reference, just use = [loplugin:referencecasting]}}
188 css::uno::Reference<css::io::XStreamListener> xDataSeries(seq[i], css::uno::UNO_QUERY);
192 namespace test15
194 class Foo : public cppu::WeakImplHelper<css::lang::XComponent, css::io::XInputStream>
196 virtual ~Foo();
197 css::uno::Reference<css::lang::XTypeProvider> bar()
199 // expected-error@+1 {{the source reference is already a subtype of the destination reference, just use = [loplugin:referencecasting]}}
200 return css::uno::Reference<css::lang::XTypeProvider>(
201 static_cast<css::lang::XTypeProvider*>(this), css::uno::UNO_QUERY);
203 css::uno::Reference<css::io::XInputStream> bar2()
205 // expected-error@+1 {{the source reference is already a subtype of the destination reference, just use = [loplugin:referencecasting]}}
206 return css::uno::Reference<css::io::XInputStream>(static_cast<css::io::XInputStream*>(this),
207 css::uno::UNO_QUERY);
209 css::uno::Reference<css::io::XInputStream> bar3()
211 // expected-error@+1 {{the source reference is already a subtype of the destination reference, just use = [loplugin:referencecasting]}}
212 return css::uno::Reference<css::io::XInputStream>(*this, css::uno::UNO_QUERY);
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */