blob: f8851b51077a21334c68217fb23d6f71629ffe64 [file] [log] [blame]
Angel Garcia Gomez26fd0e82015-09-29 09:36:41 +00001// RUN: %python %S/check_clang_tidy.py %s modernize-make-unique %t
2
3namespace std {
4
Angel Garcia Gomezb9f30592015-10-05 12:20:17 +00005template <typename T>
6class default_delete {};
7
8template <typename type, typename Deleter = std::default_delete<type>>
Angel Garcia Gomez26fd0e82015-09-29 09:36:41 +00009class unique_ptr {
10public:
11 unique_ptr(type *ptr);
12 unique_ptr(const unique_ptr<type> &t) = delete;
13 unique_ptr(unique_ptr<type> &&t);
14 ~unique_ptr();
15 type &operator*() { return *ptr; }
16 type *operator->() { return ptr; }
17 type *release();
18 void reset();
19 void reset(type *pt);
20
21private:
22 type *ptr;
23};
24
25}
26
27struct Base {
28 Base();
29 Base(int, int);
30};
31
32struct Derived : public Base {
33 Derived();
34 Derived(int, int);
35};
36
37struct Pair {
38 int a, b;
39};
40
41template<class T> using unique_ptr_ = std::unique_ptr<T>;
42
43int g(std::unique_ptr<int> P);
44
45std::unique_ptr<Base> getPointer() {
46 return std::unique_ptr<Base>(new Base);
47 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::make_unique instead
48 // CHECK-FIXES: return std::make_unique<Base>();
49}
50
51void f() {
52 std::unique_ptr<int> P1 = std::unique_ptr<int>(new int());
53 // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
54 // CHECK-FIXES: std::unique_ptr<int> P1 = std::make_unique<int>();
55
56 // Without parenthesis.
57 std::unique_ptr<int> P2 = std::unique_ptr<int>(new int);
58 // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
59 // CHECK-FIXES: std::unique_ptr<int> P2 = std::make_unique<int>();
60
61 // With auto.
62 auto P3 = std::unique_ptr<int>(new int());
63 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
64 // CHECK-FIXES: auto P3 = std::make_unique<int>();
65
66 {
67 // No std.
68 using namespace std;
69 unique_ptr<int> Q = unique_ptr<int>(new int());
70 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use std::make_unique instead
71 // CHECK-FIXES: unique_ptr<int> Q = std::make_unique<int>();
72 }
73
74 std::unique_ptr<int> R(new int());
75
76 // Create the unique_ptr as a parameter to a function.
77 int T = g(std::unique_ptr<int>(new int()));
78 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
79 // CHECK-FIXES: int T = g(std::make_unique<int>());
80
81 // Arguments are correctly handled.
82 std::unique_ptr<Base> Pbase = std::unique_ptr<Base>(new Base(5, T));
83 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: use std::make_unique instead
84 // CHECK-FIXES: std::unique_ptr<Base> Pbase = std::make_unique<Base>(5, T);
85
86 // Works with init lists.
87 std::unique_ptr<Pair> Ppair = std::unique_ptr<Pair>(new Pair{T, 1});
88 // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: use std::make_unique instead
89 // CHECK-FIXES: std::unique_ptr<Pair> Ppair = std::make_unique<Pair>({T, 1});
90
91 // Only replace if the type in the template is the same than the type returned
92 // by the new operator.
93 auto Pderived = std::unique_ptr<Base>(new Derived());
94
95 // The pointer is returned by the function, nothing to do.
96 std::unique_ptr<Base> RetPtr = getPointer();
97
98 // Aliases.
99 typedef std::unique_ptr<int> IntPtr;
100 IntPtr Typedef = IntPtr(new int);
101 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use std::make_unique instead
102 // CHECK-FIXES: IntPtr Typedef = std::make_unique<int>();
103
104#define PTR unique_ptr<int>
105 std::unique_ptr<int> Macro = std::PTR(new int);
106 // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead
107 // CHECK-FIXES: std::unique_ptr<int> Macro = std::make_unique<int>();
108#undef PTR
109
110 std::unique_ptr<int> Using = unique_ptr_<int>(new int);
111 // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead
112 // CHECK-FIXES: std::unique_ptr<int> Using = std::make_unique<int>();
113
114 // This emulates std::move.
115 std::unique_ptr<int> Move = static_cast<std::unique_ptr<int>&&>(P1);
116
117 // Adding whitespaces.
118 auto Space = std::unique_ptr <int>(new int());
119 // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use std::make_unique instead
120 // CHECK-FIXES: auto Space = std::make_unique<int>();
121
122 auto Spaces = std :: unique_ptr <int>(new int());
123 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use std::make_unique instead
124 // CHECK-FIXES: auto Spaces = std::make_unique<int>();
125}