Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / test / useuniqueptr.cxx
blob8ef71175bb9976df04b7cb7fad5a3d818737cb9e
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 <array>
11 #include <vector>
12 #include <unordered_map>
14 struct XXX {
15 ~XXX() {}
18 class Foo1 {
19 XXX* m_pbar; // expected-note {{member is here [loplugin:useuniqueptr]}}
20 ~Foo1()
22 delete m_pbar; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
23 m_pbar = nullptr;
28 class Foo2 {
29 char* m_pbar1; // expected-note {{member is here [loplugin:useuniqueptr]}}
30 char* m_pbar2; // expected-note {{member is here [loplugin:useuniqueptr]}}
31 ~Foo2()
33 delete[] m_pbar1; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
34 delete[] m_pbar2; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
38 class Foo3 {
39 char* m_pbar;
40 bool bMine;
41 ~Foo3()
43 if (bMine)
44 delete[] m_pbar;
48 class Class4 {
49 int* m_pbar[10]; // expected-note {{member is here [loplugin:useuniqueptr]}}
50 ~Class4()
52 for (int i = 0; i < 10; ++i)
53 delete m_pbar[i]; // expected-error {{rather manage with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}}
56 class Class5 {
57 int* m_pbar[10]; // expected-note {{member is here [loplugin:useuniqueptr]}}
58 ~Class5()
60 for (auto p : m_pbar)
61 delete p; // expected-error {{rather manage with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}}
64 class Class6 {
65 std::array<int*,10> m_pbar; // expected-note {{member is here [loplugin:useuniqueptr]}}
66 ~Class6()
68 for (auto p : m_pbar)
69 delete p; // expected-error {{rather manage with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}}
72 class Class7 {
73 std::array<int*,10> m_pbar; // expected-note {{member is here [loplugin:useuniqueptr]}}
74 ~Class7()
76 for (int i = 0; i < 10; ++i)
77 delete m_pbar[i]; // expected-error {{rather manage with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}}
80 // don't warn for maps, MSVC 2015 has problems with mixing std::map/std::unordered_map and std::unique_ptr
81 class Class8 {
82 std::unordered_map<int, int*> m_pbar;
83 ~Class8()
85 for (auto i : m_pbar)
86 delete i.second;
89 class Foo8 {
90 XXX* m_pbar1; // expected-note {{member is here [loplugin:useuniqueptr]}}
91 XXX* m_pbar2; // expected-note {{member is here [loplugin:useuniqueptr]}}
92 ~Foo8()
94 delete m_pbar1; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
95 delete m_pbar2; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
98 class Foo9 {
99 XXX* m_pbar1; // expected-note {{member is here [loplugin:useuniqueptr]}}
100 XXX* m_pbar2; // expected-note {{member is here [loplugin:useuniqueptr]}}
101 XXX* m_pbar3; // expected-note {{member is here [loplugin:useuniqueptr]}}
102 XXX* m_pbar4; // expected-note {{member is here [loplugin:useuniqueptr]}}
103 ~Foo9()
105 if (m_pbar1)
107 delete m_pbar1; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
109 if (m_pbar2 != nullptr)
111 delete m_pbar2; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
113 if (m_pbar3 != nullptr)
114 delete m_pbar3; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
115 if (m_pbar4 != nullptr)
117 int x = 1;
118 (void)x;
119 delete m_pbar4; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
123 // no warning expected
124 class Foo10 {
125 XXX* m_pbar1;
126 ~Foo10()
128 if (m_pbar1 != getOther())
130 delete m_pbar1;
133 XXX* getOther() { return nullptr; }
135 class Foo11 {
136 std::vector<XXX*> m_pbar1; // expected-note {{member is here [loplugin:useuniqueptr]}}
137 ~Foo11()
139 for (const auto & p : m_pbar1)
141 delete p; // expected-error {{rather manage with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}}
145 class Foo12 {
146 std::array<int*,10> m_pbar; // expected-note {{member is here [loplugin:useuniqueptr]}}
147 ~Foo12()
149 int i = 0;
150 while (i < 10)
151 delete m_pbar[i++]; // expected-error {{rather manage with std::some_container<std::unique_ptr<T>> [loplugin:useuniqueptr]}}
154 #define DELETEZ( p ) ( delete p,p = NULL )
155 class Foo13 {
156 int * m_pbar1; // expected-note {{member is here [loplugin:useuniqueptr]}}
157 int * m_pbar2; // expected-note {{member is here [loplugin:useuniqueptr]}}
158 ~Foo13()
160 if (m_pbar1)
161 DELETEZ(m_pbar1); // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
162 DELETEZ(m_pbar2); // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
165 // check for unconditional inner compound statements
166 class Foo14 {
167 int * m_pbar1; // expected-note {{member is here [loplugin:useuniqueptr]}}
168 ~Foo14()
171 delete m_pbar1; // expected-error {{unconditional call to delete on a member, should be using std::unique_ptr [loplugin:useuniqueptr]}}
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */