blob: 6d23c4cb1fe087b114167a29632d89693655f222 [file] [log] [blame]
Manuel Klimekb91bee02015-10-22 11:31:44 +00001// RUN: %check_clang_tidy %s modernize-make-unique %t
Angel Garcia Gomez26fd0e82015-09-29 09:36:41 +00002
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
Angel Garcia Gomezbaf573e2015-10-14 09:22:32 +000037struct APair {
Angel Garcia Gomez26fd0e82015-09-29 09:36:41 +000038 int a, b;
39};
40
Angel Garcia Gomezbaf573e2015-10-14 09:22:32 +000041struct DPair {
42 DPair() : a(0), b(0) {}
43 DPair(int x, int y) : a(y), b(x) {}
44 int a, b;
45};
46
47struct Empty {};
48
Angel Garcia Gomez26fd0e82015-09-29 09:36:41 +000049template<class T> using unique_ptr_ = std::unique_ptr<T>;
50
Angel Garcia Gomez9eb6e2e2015-10-14 10:30:32 +000051void *operator new(__SIZE_TYPE__ Count, void *Ptr);
Angel Garcia Gomezbaf573e2015-10-14 09:22:32 +000052
Angel Garcia Gomez26fd0e82015-09-29 09:36:41 +000053int g(std::unique_ptr<int> P);
54
55std::unique_ptr<Base> getPointer() {
56 return std::unique_ptr<Base>(new Base);
57 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::make_unique instead
58 // CHECK-FIXES: return std::make_unique<Base>();
59}
60
Angel Garcia Gomezbaf573e2015-10-14 09:22:32 +000061void basic() {
Angel Garcia Gomez26fd0e82015-09-29 09:36:41 +000062 std::unique_ptr<int> P1 = std::unique_ptr<int>(new int());
63 // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
64 // CHECK-FIXES: std::unique_ptr<int> P1 = std::make_unique<int>();
65
66 // Without parenthesis.
67 std::unique_ptr<int> P2 = std::unique_ptr<int>(new int);
68 // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
69 // CHECK-FIXES: std::unique_ptr<int> P2 = std::make_unique<int>();
70
71 // With auto.
72 auto P3 = std::unique_ptr<int>(new int());
73 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
74 // CHECK-FIXES: auto P3 = std::make_unique<int>();
75
76 {
77 // No std.
78 using namespace std;
79 unique_ptr<int> Q = unique_ptr<int>(new int());
80 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use std::make_unique instead
81 // CHECK-FIXES: unique_ptr<int> Q = std::make_unique<int>();
82 }
83
84 std::unique_ptr<int> R(new int());
85
86 // Create the unique_ptr as a parameter to a function.
87 int T = g(std::unique_ptr<int>(new int()));
88 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
89 // CHECK-FIXES: int T = g(std::make_unique<int>());
90
Angel Garcia Gomez26fd0e82015-09-29 09:36:41 +000091 // 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
Angel Garcia Gomezbaf573e2015-10-14 09:22:32 +000098 // This emulates std::move.
99 std::unique_ptr<int> Move = static_cast<std::unique_ptr<int>&&>(P1);
100
101 // Placemenet arguments should not be removed.
102 int *PInt = new int;
103 std::unique_ptr<int> Placement = std::unique_ptr<int>(new (PInt) int{3});
104}
105
106void initialization(int T, Base b) {
107 // Test different kinds of initialization of the pointee.
108
109 // Direct initialization with parenthesis.
110 std::unique_ptr<DPair> PDir1 = std::unique_ptr<DPair>(new DPair(1, T));
111 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
112 // CHECK-FIXES: std::unique_ptr<DPair> PDir1 = std::make_unique<DPair>(1, T);
113
114 // Direct initialization with braces.
115 std::unique_ptr<DPair> PDir2 = std::unique_ptr<DPair>(new DPair{2, T});
116 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
117 // CHECK-FIXES: std::unique_ptr<DPair> PDir2 = std::make_unique<DPair>(2, T);
118
119 // Aggregate initialization.
120 std::unique_ptr<APair> PAggr = std::unique_ptr<APair>(new APair{T, 1});
121 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
122 // CHECK-FIXES: std::unique_ptr<APair> PAggr = std::make_unique<APair>(APair{T, 1});
123
124
125 // Test different kinds of initialization of the pointee, when the unique_ptr
126 // is initialized with braces.
127
128 // Direct initialization with parenthesis.
129 std::unique_ptr<DPair> PDir3 = std::unique_ptr<DPair>{new DPair(3, T)};
130 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
131 // CHECK-FIXES: std::unique_ptr<DPair> PDir3 = std::make_unique<DPair>(3, T);
132
133 // Direct initialization with braces.
134 std::unique_ptr<DPair> PDir4 = std::unique_ptr<DPair>{new DPair{4, T}};
135 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
136 // CHECK-FIXES: std::unique_ptr<DPair> PDir4 = std::make_unique<DPair>(4, T);
137
138 // Aggregate initialization.
139 std::unique_ptr<APair> PAggr2 = std::unique_ptr<APair>{new APair{T, 2}};
140 // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: use std::make_unique instead
141 // CHECK-FIXES: std::unique_ptr<APair> PAggr2 = std::make_unique<APair>(APair{T, 2});
142
143
144 // Direct initialization with parenthesis, without arguments.
145 std::unique_ptr<DPair> PDir5 = std::unique_ptr<DPair>(new DPair());
146 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
147 // CHECK-FIXES: std::unique_ptr<DPair> PDir5 = std::make_unique<DPair>();
148
149 // Direct initialization with braces, without arguments.
150 std::unique_ptr<DPair> PDir6 = std::unique_ptr<DPair>(new DPair{});
151 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
152 // CHECK-FIXES: std::unique_ptr<DPair> PDir6 = std::make_unique<DPair>();
153
154 // Aggregate initialization without arguments.
155 std::unique_ptr<Empty> PEmpty = std::unique_ptr<Empty>(new Empty{});
156 // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: use std::make_unique instead
157 // CHECK-FIXES: std::unique_ptr<Empty> PEmpty = std::make_unique<Empty>(Empty{});
158}
159
160void aliases() {
Angel Garcia Gomez26fd0e82015-09-29 09:36:41 +0000161 typedef std::unique_ptr<int> IntPtr;
162 IntPtr Typedef = IntPtr(new int);
163 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use std::make_unique instead
164 // CHECK-FIXES: IntPtr Typedef = std::make_unique<int>();
165
Angel Garcia Gomez37ab7fd2015-10-22 13:20:49 +0000166 // We use 'bool' instead of '_Bool'.
167 typedef std::unique_ptr<bool> BoolPtr;
168 BoolPtr BoolType = BoolPtr(new bool);
169 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use std::make_unique instead
170 // CHECK-FIXES: BoolPtr BoolType = std::make_unique<bool>();
171
172 // We use 'Base' instead of 'struct Base'.
173 typedef std::unique_ptr<Base> BasePtr;
174 BasePtr StructType = BasePtr(new Base);
175 // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead
176 // CHECK-FIXES: BasePtr StructType = std::make_unique<Base>();
177
Angel Garcia Gomez26fd0e82015-09-29 09:36:41 +0000178#define PTR unique_ptr<int>
179 std::unique_ptr<int> Macro = std::PTR(new int);
180 // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead
181 // CHECK-FIXES: std::unique_ptr<int> Macro = std::make_unique<int>();
182#undef PTR
183
184 std::unique_ptr<int> Using = unique_ptr_<int>(new int);
185 // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead
186 // CHECK-FIXES: std::unique_ptr<int> Using = std::make_unique<int>();
Angel Garcia Gomezbaf573e2015-10-14 09:22:32 +0000187}
Angel Garcia Gomez26fd0e82015-09-29 09:36:41 +0000188
Angel Garcia Gomezbaf573e2015-10-14 09:22:32 +0000189void whitespaces() {
Angel Garcia Gomez26fd0e82015-09-29 09:36:41 +0000190 auto Space = std::unique_ptr <int>(new int());
191 // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use std::make_unique instead
192 // CHECK-FIXES: auto Space = std::make_unique<int>();
193
194 auto Spaces = std :: unique_ptr <int>(new int());
195 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use std::make_unique instead
196 // CHECK-FIXES: auto Spaces = std::make_unique<int>();
197}