Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / test / makeshared.cxx
blobd6c3cbdac7367890f8dfad8727d1226ca68d31d1
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 <memory>
13 #include <o3tl/deleter.hxx>
14 #include <o3tl/sorted_vector.hxx>
16 struct S1
18 friend void test1();
20 private:
21 S1() {}
24 void test1()
26 // expected-error@+1 {{rather use make_shared than constructing from 'int *' [loplugin:makeshared]}}
27 std::shared_ptr<int> x(new int);
28 // expected-error@+1 {{rather use make_shared [loplugin:makeshared]}}
29 x.reset(new int);
30 // expected-error@+1 {{rather use make_shared than constructing from 'int *' [loplugin:makeshared]}}
31 x = std::shared_ptr<int>(new int);
33 // no warning expected
34 std::shared_ptr<int> y(new int, o3tl::default_delete<int>());
35 y.reset(new int, o3tl::default_delete<int>());
36 // no warning expected, no public constructor
37 std::shared_ptr<S1> z(new S1);
38 z.reset(new S1);
40 // no warning expected - this constructor takes an initializer-list, which make_shared does not support
41 auto a = std::shared_ptr<o3tl::sorted_vector<int>>(new o3tl::sorted_vector<int>({ 1, 2 }));
44 void test2()
46 // expected-error-re@+1 {{rather use make_shared than constructing from {{.*}}'unique_ptr<int>'{{.*}} [loplugin:makeshared]}}
47 std::shared_ptr<int> x = std::make_unique<int>(1);
48 // expected-error-re@+1 {{rather use make_shared than constructing from {{.*}}'unique_ptr<int>'{{.*}} [loplugin:makeshared]}}
49 x = std::make_unique<int>(1);
50 (void)x;
52 // expected-error-re@+1 {{rather use make_shared than constructing from {{.*}}'unique_ptr<int>'{{.*}} [loplugin:makeshared]}}
53 std::shared_ptr<int> y(std::make_unique<int>(1));
54 (void)y;
56 std::unique_ptr<int> u1;
57 // expected-error-re@+1 {{rather use make_shared than constructing from {{.+}} (aka 'std{{.*}}::unique_ptr<int{{.*}}>') [loplugin:makeshared]}}
58 std::shared_ptr<int> z = std::move(u1);
59 // expected-error-re@+1 {{rather use make_shared than constructing from {{.+}} (aka 'std{{.*}}::unique_ptr<int{{.*}}>') [loplugin:makeshared]}}
60 z = std::move(u1);
63 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */