1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
10 #include <sal/config.h>
11 #include <config_clang.h>
15 #include <unordered_map>
22 XXX
* m_pbar
; // expected-note {{member is here [loplugin:useuniqueptr]}}
25 delete m_pbar
; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
32 char* m_pbar1
; // expected-note {{member is here [loplugin:useuniqueptr]}}
33 char* m_pbar2
; // expected-note {{member is here [loplugin:useuniqueptr]}}
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]}}
52 int* m_pbar
[10]; // expected-note {{member is here [loplugin:useuniqueptr]}}
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]}}
60 int* m_pbar
[10]; // expected-note {{member is here [loplugin:useuniqueptr]}}
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]}}
68 int* m_pbar
[10]; // expected-note {{member is here [loplugin:useuniqueptr]}}
71 for (auto p
: m_pbar
) // expected-note {{var is here [loplugin:useuniqueptr]}}
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]}}
80 std::array
<int*,10> m_pbar
; // expected-note {{member is here [loplugin:useuniqueptr]}}
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]}}
88 std::array
<int*,10> m_pbar
; // expected-note {{member is here [loplugin:useuniqueptr]}}
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]}}
96 std::unordered_map
<int, int*> m_pbar
; // expected-note {{member is here [loplugin:useuniqueptr]}}
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]}}
104 XXX
* m_pbar1
; // expected-note {{member is here [loplugin:useuniqueptr]}}
105 XXX
* m_pbar2
; // expected-note {{member is here [loplugin:useuniqueptr]}}
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]}}
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]}}
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)
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
142 if (m_pbar1
!= getOther())
147 XXX
* getOther() { return nullptr; }
150 std::vector
<XXX
*> m_pbar1
; // expected-note {{member is here [loplugin:useuniqueptr]}}
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]}}
160 std::array
<int*,10> m_pbar
; // expected-note {{member is here [loplugin:useuniqueptr]}}
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 )
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]}}
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]}}
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
187 int * m_pbar1
; // expected-note {{member is here [loplugin:useuniqueptr]}}
191 delete m_pbar1
; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
198 delete p
; // expected-error {{calling delete on a pointer param, should be either whitelisted or simplified [loplugin:useuniqueptr]}}
204 delete p
; // expected-error {{calling delete on a pointer param, should be either whitelisted or simplified [loplugin:useuniqueptr]}}
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
214 int * m_pbar1
[6]; // expected-note {{member is here [loplugin:useuniqueptr]}}
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
223 std::vector
<char*> m_pbar1
; // expected-note {{member is here [loplugin:useuniqueptr]}}
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]}}
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
241 struct struct20_1
{};
242 struct struct20_2
: public struct20_1
{
245 void foo20(struct20_1
* pMapping
)
247 delete static_cast< struct20_2
* >( pMapping
)->p
;
251 // ------------------------------------------------------------------------------------------------
252 // tests for deleting when looping via iterators
253 // ------------------------------------------------------------------------------------------------
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]}}
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]}}
271 std::unordered_map
<int, float*> map
; // expected-note {{member is here [loplugin:useuniqueptr]}}
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]}}
281 typedef std::vector
<int*> HTMLAttrs
;
282 HTMLAttrs m_aSetAttrTab
; // expected-note {{member is here [loplugin:useuniqueptr]}}
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 // ------------------------------------------------------------------------------------------------
297 std::unique_ptr
<int> m_field1
;
299 : m_field1(p
) // expected-error {{should be passing via std::unique_ptr param [loplugin:useuniqueptr]}}
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: */