bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / test / useuniqueptr.cxx
blob3d4f62d5dfa8422c8853d49198223b629b77ab2e
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>
11 #include <config_clang.h>
12 #include <array>
13 #include <memory>
14 #include <vector>
15 #include <unordered_map>
17 struct XXX {
18 ~XXX() {}
21 class Foo1 {
22 XXX* m_pbar; // expected-note {{member is here [loplugin:useuniqueptr]}}
23 ~Foo1()
25 delete m_pbar; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
26 m_pbar = nullptr;
31 class Foo2 {
32 char* m_pbar1; // expected-note {{member is here [loplugin:useuniqueptr]}}
33 char* m_pbar2; // expected-note {{member is here [loplugin:useuniqueptr]}}
34 ~Foo2()
36 delete[] m_pbar1; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
37 delete[] m_pbar2; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
41 class Foo3 {
42 char* m_pbar;
43 bool bMine;
44 ~Foo3()
46 if (bMine)
47 delete[] m_pbar;
51 class Class4 {
52 int* m_pbar[10]; // expected-note {{member is here [loplugin:useuniqueptr]}}
53 ~Class4()
55 for (int i = 0; i < 10; ++i)
56 delete m_pbar[i]; // expected-error {{rather manage this member with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}}
59 class Class5 {
60 int* m_pbar[10]; // expected-note {{member is here [loplugin:useuniqueptr]}}
61 ~Class5()
63 for (auto p : m_pbar) // expected-note {{var is here [loplugin:useuniqueptr]}}
64 delete p; // expected-error {{rather manage this with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}} expected-error {{call to delete on a var, should be using std::unique_ptr [loplugin:useuniqueptr]}}
67 class Class5a {
68 int* m_pbar[10]; // expected-note {{member is here [loplugin:useuniqueptr]}}
69 ~Class5a()
71 for (auto p : m_pbar) // expected-note {{var is here [loplugin:useuniqueptr]}}
73 int x = 1;
74 x = x + 2;
75 delete p; // expected-error {{rather manage this with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}} expected-error {{call to delete on a var, should be using std::unique_ptr [loplugin:useuniqueptr]}}
79 class Class6 {
80 std::array<int*,10> m_pbar; // expected-note {{member is here [loplugin:useuniqueptr]}}
81 ~Class6()
83 for (auto p : m_pbar) // expected-note {{var is here [loplugin:useuniqueptr]}}
84 delete p; // expected-error {{rather manage this with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}} expected-error {{call to delete on a var, should be using std::unique_ptr [loplugin:useuniqueptr]}}
87 class Class7 {
88 std::array<int*,10> m_pbar; // expected-note {{member is here [loplugin:useuniqueptr]}}
89 ~Class7()
91 for (int i = 0; i < 10; ++i)
92 delete m_pbar[i]; // expected-error {{rather manage this member with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}}
95 class Class8 {
96 std::unordered_map<int, int*> m_pbar; // expected-note {{member is here [loplugin:useuniqueptr]}}
97 ~Class8()
99 for (auto & i : m_pbar)
100 delete i.second; // expected-error {{rather manage this with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}}
103 class Foo8 {
104 XXX* m_pbar1; // expected-note {{member is here [loplugin:useuniqueptr]}}
105 XXX* m_pbar2; // expected-note {{member is here [loplugin:useuniqueptr]}}
106 ~Foo8()
108 delete m_pbar1; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
109 delete m_pbar2; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
112 class Foo9 {
113 XXX* m_pbar1; // expected-note {{member is here [loplugin:useuniqueptr]}}
114 XXX* m_pbar2; // expected-note {{member is here [loplugin:useuniqueptr]}}
115 XXX* m_pbar3; // expected-note {{member is here [loplugin:useuniqueptr]}}
116 XXX* m_pbar4; // expected-note {{member is here [loplugin:useuniqueptr]}}
117 ~Foo9()
119 if (m_pbar1)
121 delete m_pbar1; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
123 if (m_pbar2 != nullptr)
125 delete m_pbar2; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
127 if (m_pbar3 != nullptr)
128 delete m_pbar3; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
129 if (m_pbar4 != nullptr)
131 int x = 1;
132 (void)x;
133 delete m_pbar4; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
137 // no warning expected
138 class Foo10 {
139 XXX* m_pbar1;
140 ~Foo10()
142 if (m_pbar1 != getOther())
144 delete m_pbar1;
147 XXX* getOther() { return nullptr; }
149 class Foo11 {
150 std::vector<XXX*> m_pbar1; // expected-note {{member is here [loplugin:useuniqueptr]}}
151 ~Foo11()
153 for (const auto & p : m_pbar1) // expected-note {{var is here [loplugin:useuniqueptr]}}
155 delete p; // expected-error {{rather manage this with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}} expected-error {{call to delete on a var, should be using std::unique_ptr [loplugin:useuniqueptr]}}
159 class Foo12 {
160 std::array<int*,10> m_pbar; // expected-note {{member is here [loplugin:useuniqueptr]}}
161 ~Foo12()
163 int i = 0;
164 while (i < 10)
165 delete m_pbar[i++]; // expected-error {{rather manage this member with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}}
168 #define DELETEZ( p ) ( delete p,p = NULL )
169 class Foo13 {
170 int * m_pbar1; // expected-note {{member is here [loplugin:useuniqueptr]}}
171 int * m_pbar2; // expected-note {{member is here [loplugin:useuniqueptr]}}
172 int * m_pbar3; // expected-note {{member is here [loplugin:useuniqueptr]}}
173 ~Foo13()
175 if (m_pbar1)
176 DELETEZ(m_pbar1); // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
177 DELETEZ(m_pbar2); // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
178 if (m_pbar3)
180 DELETEZ(m_pbar3); // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
185 // check for unconditional inner compound statements
186 class Foo14 {
187 int * m_pbar1; // expected-note {{member is here [loplugin:useuniqueptr]}}
188 ~Foo14()
191 delete m_pbar1; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
196 void Foo15(int * p)
198 delete p; // expected-error {{calling delete on a pointer param, should be either whitelisted or simplified [loplugin:useuniqueptr]}}
201 class Foo16 {
202 Foo16(int * p)
204 delete p; // expected-error {{calling delete on a pointer param, should be either whitelisted or simplified [loplugin:useuniqueptr]}}
206 void foo(int * p)
208 delete p; // expected-error {{calling delete on a pointer param, should be either whitelisted or simplified [loplugin:useuniqueptr]}}
212 // check for delete on array members
213 class Foo17 {
214 int * m_pbar1[6]; // expected-note {{member is here [loplugin:useuniqueptr]}}
215 ~Foo17()
217 delete m_pbar1[0]; // expected-error {{unconditional call to delete on an array member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
221 // this only starts to work somewhere after clang 3.8 and before clang7
222 class Foo18 {
223 std::vector<char*> m_pbar1; // expected-note {{member is here [loplugin:useuniqueptr]}}
224 ~Foo18()
226 for (auto aIter = m_pbar1.begin(); aIter != m_pbar1.end(); ++aIter)
227 delete *aIter; // expected-error {{rather manage this member with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}}
231 void foo19()
233 std::vector<char*> vec; // expected-note {{var is here [loplugin:useuniqueptr]}}
234 for(char * p : vec) // expected-note {{var is here [loplugin:useuniqueptr]}}
235 delete p; // expected-error {{rather manage this var with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}} expected-error {{call to delete on a var, should be using std::unique_ptr [loplugin:useuniqueptr]}}
238 // no warning expected
239 namespace foo20
241 struct struct20_1 {};
242 struct struct20_2 : public struct20_1 {
243 char * p;
245 void foo20(struct20_1 * pMapping)
247 delete static_cast< struct20_2 * >( pMapping )->p;
251 // ------------------------------------------------------------------------------------------------
252 // tests for deleting when looping via iterators
253 // ------------------------------------------------------------------------------------------------
255 void foo21()
257 std::vector<bool*> vec; // expected-note {{var is here [loplugin:useuniqueptr]}}
258 for(auto it = vec.begin(); it != vec.end(); ++it)
259 delete *it; // expected-error {{rather manage this var with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}}
262 void foo22()
264 std::unordered_map<int, float*> map; // expected-note {{var is here [loplugin:useuniqueptr]}}
265 for(auto it = map.begin(); it != map.end(); ++it)
266 delete it->second; // expected-error {{rather manage this var with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}}
269 class Foo23
271 std::unordered_map<int, float*> map; // expected-note {{member is here [loplugin:useuniqueptr]}}
272 ~Foo23()
274 for(auto it = map.begin(); it != map.end(); ++it)
275 delete it->second; // expected-error {{rather manage this member with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}}
279 class Foo24
281 typedef std::vector<int*> HTMLAttrs;
282 HTMLAttrs m_aSetAttrTab; // expected-note {{member is here [loplugin:useuniqueptr]}}
283 ~Foo24()
285 for ( HTMLAttrs::const_iterator it = m_aSetAttrTab.begin(); it != m_aSetAttrTab.end(); ++it )
286 delete *it; // expected-error {{rather manage this member with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}}
290 // ------------------------------------------------------------------------------------------------
291 // tests for passing owning pointers to constructors
292 // ------------------------------------------------------------------------------------------------
295 class Bravo1
297 std::unique_ptr<int> m_field1;
298 Bravo1(int* p)
299 : m_field1(p) // expected-error {{should be passing via std::unique_ptr param [loplugin:useuniqueptr]}}
302 class Bravo2
304 std::unique_ptr<int> m_field1;
305 Bravo2(std::unique_ptr<int> p)
306 : m_field1(std::move(p)) // no warning expected
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */