1 // RUN: %check_clang_tidy %s modernize-make-unique %t -- -- -std=c++14 \
2 // RUN:   -I%S/Inputs/modernize-smart-ptr
3 
4 #include "unique_ptr.h"
5 #include "initializer_list.h"
6 // CHECK-FIXES: #include <memory>
7 
8 struct Base {
9   Base();
10   Base(int, int);
11 };
12 
13 struct Derived : public Base {
14   Derived();
15   Derived(int, int);
16 };
17 
18 struct APair {
19   int a, b;
20 };
21 
22 struct DPair {
DPairDPair23   DPair() : a(0), b(0) {}
DPairDPair24   DPair(int x, int y) : a(y), b(x) {}
25   int a, b;
26 };
27 
28 template<typename T>
29 struct MyVector {
30   MyVector(std::initializer_list<T>);
31 };
32 
33 struct Empty {};
34 
35 struct E {
36   E(std::initializer_list<int>);
37   E();
38 };
39 
40 struct F {
41   F(std::initializer_list<int>);
42   F();
43   int a;
44 };
45 
46 struct G {
47   G(std::initializer_list<int>);
48   G(int);
49 };
50 
51 struct H {
52   H(std::vector<int>);
53   H(std::vector<int> &, double);
54   H(MyVector<int>, int);
55 };
56 
57 struct I {
58   I(G);
59 };
60 
61 namespace {
62 class Foo {};
63 } // namespace
64 
65 namespace bar {
66 class Bar {};
67 } // namespace bar
68 
69 template <class T>
70 using unique_ptr_ = std::unique_ptr<T>;
71 
72 void *operator new(__SIZE_TYPE__ Count, void *Ptr);
73 
74 int g(std::unique_ptr<int> P);
75 
getPointer()76 std::unique_ptr<Base> getPointer() {
77   return std::unique_ptr<Base>(new Base);
78   // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::make_unique instead
79   // CHECK-FIXES: return std::make_unique<Base>();
80 }
81 
basic()82 void basic() {
83   std::unique_ptr<int> P1 = std::unique_ptr<int>(new int());
84   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
85   // CHECK-FIXES: std::unique_ptr<int> P1 = std::make_unique<int>();
86 
87   P1.reset(new int());
88   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
89   // CHECK-FIXES: P1 = std::make_unique<int>();
90 
91   P1 = std::unique_ptr<int>(new int());
92   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
93   // CHECK-FIXES: P1 = std::make_unique<int>();
94 
95   // Without parenthesis.
96   std::unique_ptr<int> P2 = std::unique_ptr<int>(new int);
97   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]
98   // CHECK-FIXES: std::unique_ptr<int> P2 = std::make_unique<int>();
99 
100   P2.reset(new int);
101   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]
102   // CHECK-FIXES: P2 = std::make_unique<int>();
103 
104   P2 = std::unique_ptr<int>(new int);
105   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]
106   // CHECK-FIXES: P2 = std::make_unique<int>();
107 
108   // With auto.
109   auto P3 = std::unique_ptr<int>(new int());
110   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
111   // CHECK-FIXES: auto P3 = std::make_unique<int>();
112 
113   {
114     // No std.
115     using namespace std;
116     unique_ptr<int> Q = unique_ptr<int>(new int());
117     // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use std::make_unique instead
118     // CHECK-FIXES: unique_ptr<int> Q = std::make_unique<int>();
119 
120     Q = unique_ptr<int>(new int());
121     // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
122     // CHECK-FIXES: Q = std::make_unique<int>();
123   }
124 
125   std::unique_ptr<int> R(new int());
126 
127   // Create the unique_ptr as a parameter to a function.
128   int T = g(std::unique_ptr<int>(new int()));
129   // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
130   // CHECK-FIXES: int T = g(std::make_unique<int>());
131 
132   // Only replace if the type in the template is the same as the type returned
133   // by the new operator.
134   auto Pderived = std::unique_ptr<Base>(new Derived());
135 
136   // OK to replace for reset and assign
137   Pderived.reset(new Derived());
138   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use std::make_unique instead
139   // CHECK-FIXES: Pderived = std::make_unique<Derived>();
140 
141   Pderived = std::unique_ptr<Derived>(new Derived());
142   // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use std::make_unique instead
143   // CHECK-FIXES: Pderived = std::make_unique<Derived>();
144 
145   // FIXME: OK to replace if assigned to unique_ptr<Base>
146   Pderived = std::unique_ptr<Base>(new Derived());
147 
148   // FIXME: OK to replace when auto is not used
149   std::unique_ptr<Base> PBase = std::unique_ptr<Base>(new Derived());
150 
151   // The pointer is returned by the function, nothing to do.
152   std::unique_ptr<Base> RetPtr = getPointer();
153 
154   // This emulates std::move.
155   std::unique_ptr<int> Move = static_cast<std::unique_ptr<int> &&>(P1);
156 
157   // Placement arguments should not be removed.
158   int *PInt = new int;
159   std::unique_ptr<int> Placement = std::unique_ptr<int>(new (PInt) int{3});
160   Placement.reset(new (PInt) int{3});
161   Placement = std::unique_ptr<int>(new (PInt) int{3});
162 }
163 
164 // Calling make_smart_ptr from within a member function of a type with a
165 // private or protected constructor would be ill-formed.
166 class Private {
167 private:
Private(int z)168   Private(int z) {}
169 
170 public:
Private()171   Private() {}
create()172   void create() {
173     auto callsPublic = std::unique_ptr<Private>(new Private);
174     // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead
175     // CHECK-FIXES: auto callsPublic = std::make_unique<Private>();
176     auto ptr = std::unique_ptr<Private>(new Private(42));
177     ptr.reset(new Private(42));
178     ptr = std::unique_ptr<Private>(new Private(42));
179   }
180 
181   virtual ~Private();
182 };
183 
184 class Protected {
185 protected:
Protected()186   Protected() {}
187 
188 public:
Protected(int,int)189   Protected(int, int) {}
create()190   void create() {
191     auto callsPublic = std::unique_ptr<Protected>(new Protected(1, 2));
192     // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead
193     // CHECK-FIXES: auto callsPublic = std::make_unique<Protected>(1, 2);
194     auto ptr = std::unique_ptr<Protected>(new Protected);
195     ptr.reset(new Protected);
196     ptr = std::unique_ptr<Protected>(new Protected);
197   }
198 };
199 
initialization(int T,Base b)200 void initialization(int T, Base b) {
201   // Test different kinds of initialization of the pointee.
202 
203   // Direct initialization with parenthesis.
204   std::unique_ptr<DPair> PDir1 = std::unique_ptr<DPair>(new DPair(1, T));
205   // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
206   // CHECK-FIXES: std::unique_ptr<DPair> PDir1 = std::make_unique<DPair>(1, T);
207   PDir1.reset(new DPair(1, T));
208   // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
209   // CHECK-FIXES: PDir1 = std::make_unique<DPair>(1, T);
210 
211   // Direct initialization with braces.
212   std::unique_ptr<DPair> PDir2 = std::unique_ptr<DPair>(new DPair{2, T});
213   // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
214   // CHECK-FIXES: std::unique_ptr<DPair> PDir2 = std::make_unique<DPair>(2, T);
215   PDir2.reset(new DPair{2, T});
216   // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
217   // CHECK-FIXES: PDir2 = std::make_unique<DPair>(2, T);
218 
219   // Aggregate initialization.
220   std::unique_ptr<APair> PAggr = std::unique_ptr<APair>(new APair{T, 1});
221   // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
222   // CHECK-FIXES: std::unique_ptr<APair> PAggr = std::make_unique<APair>(APair{T, 1});
223   PAggr.reset(new APair{T, 1});
224   // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
225   // CHECK-FIXES: std::make_unique<APair>(APair{T, 1});
226 
227   // Test different kinds of initialization of the pointee, when the unique_ptr
228   // is initialized with braces.
229 
230   // Direct initialization with parenthesis.
231   std::unique_ptr<DPair> PDir3 = std::unique_ptr<DPair>{new DPair(3, T)};
232   // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
233   // CHECK-FIXES: std::unique_ptr<DPair> PDir3 = std::make_unique<DPair>(3, T);
234 
235   // Direct initialization with braces.
236   std::unique_ptr<DPair> PDir4 = std::unique_ptr<DPair>{new DPair{4, T}};
237   // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
238   // CHECK-FIXES: std::unique_ptr<DPair> PDir4 = std::make_unique<DPair>(4, T);
239 
240   // Aggregate initialization.
241   std::unique_ptr<APair> PAggr2 = std::unique_ptr<APair>{new APair{T, 2}};
242   // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: use std::make_unique instead
243   // CHECK-FIXES: std::unique_ptr<APair> PAggr2 = std::make_unique<APair>(APair{T, 2});
244 
245   // Direct initialization with parenthesis, without arguments.
246   std::unique_ptr<DPair> PDir5 = std::unique_ptr<DPair>(new DPair());
247   // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
248   // CHECK-FIXES: std::unique_ptr<DPair> PDir5 = std::make_unique<DPair>();
249 
250   // Direct initialization with braces, without arguments.
251   std::unique_ptr<DPair> PDir6 = std::unique_ptr<DPair>(new DPair{});
252   // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead
253   // CHECK-FIXES: std::unique_ptr<DPair> PDir6 = std::make_unique<DPair>();
254 
255   // Aggregate initialization without arguments.
256   std::unique_ptr<Empty> PEmpty = std::unique_ptr<Empty>(new Empty{});
257   // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: use std::make_unique instead
258   // CHECK-FIXES: std::unique_ptr<Empty> PEmpty = std::make_unique<Empty>(Empty{});
259 
260   // Initialization with default constructor.
261   std::unique_ptr<E> PE1 = std::unique_ptr<E>(new E{});
262   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
263   // CHECK-FIXES: std::unique_ptr<E> PE1 = std::make_unique<E>();
264   PE1.reset(new E{});
265   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead
266   // CHECK-FIXES: PE1 = std::make_unique<E>();
267 
268   //============================================================================
269   //  NOTE: For initlializer-list constructors, the check only gives warnings,
270   //  and no fixes are generated.
271   //============================================================================
272 
273   // Initialization with the initializer-list constructor.
274   std::unique_ptr<E> PE2 = std::unique_ptr<E>(new E{1, 2});
275   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
276   // CHECK-FIXES: std::unique_ptr<E> PE2 = std::unique_ptr<E>(new E{1, 2});
277   PE2.reset(new E{1, 2});
278   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead
279   // CHECK-FIXES: PE2.reset(new E{1, 2});
280 
281   // Initialization with default constructor.
282   std::unique_ptr<F> PF1 = std::unique_ptr<F>(new F());
283   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
284   // CHECK-FIXES: std::unique_ptr<F> PF1 = std::make_unique<F>();
285   PF1.reset(new F());
286   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead
287   // CHECK-FIXES: PF1 = std::make_unique<F>();
288 
289   // Initialization with default constructor.
290   std::unique_ptr<F> PF2 = std::unique_ptr<F>(new F{});
291   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
292   // CHECK-FIXES: std::unique_ptr<F> PF2 = std::make_unique<F>();
293   PF2.reset(new F());
294   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead
295   // CHECK-FIXES: PF2 = std::make_unique<F>();
296 
297   // Initialization with the initializer-list constructor.
298   std::unique_ptr<F> PF3 = std::unique_ptr<F>(new F{1});
299   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
300   // CHECK-FIXES: std::unique_ptr<F> PF3 = std::unique_ptr<F>(new F{1});
301   PF3.reset(new F{1});
302   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead
303   // CHECK-FIXES: PF3.reset(new F{1});
304 
305   // Initialization with the initializer-list constructor.
306   std::unique_ptr<F> PF4 = std::unique_ptr<F>(new F{1, 2});
307   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
308   // CHECK-FIXES: std::unique_ptr<F> PF4 = std::unique_ptr<F>(new F{1, 2});
309 
310   // Initialization with the initializer-list constructor.
311   std::unique_ptr<F> PF5 = std::unique_ptr<F>(new F({1, 2}));
312   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
313   // CHECK-FIXES: std::unique_ptr<F> PF5 = std::unique_ptr<F>(new F({1, 2}));
314 
315   // Initialization with the initializer-list constructor as the default
316   // constructor is not present.
317   std::unique_ptr<G> PG1 = std::unique_ptr<G>(new G{});
318   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
319   // CHECK-FIXES: std::unique_ptr<G> PG1 = std::unique_ptr<G>(new G{});
320   PG1.reset(new G{});
321   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead
322   // CHECK-FIXES: PG1.reset(new G{});
323 
324   // Initialization with the initializer-list constructor.
325   std::unique_ptr<G> PG2 = std::unique_ptr<G>(new G{1});
326   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
327   // CHECK-FIXES: std::unique_ptr<G> PG2 = std::unique_ptr<G>(new G{1});
328 
329   // Initialization with the initializer-list constructor.
330   std::unique_ptr<G> PG3 = std::unique_ptr<G>(new G{1, 2});
331   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
332   // CHECK-FIXES: std::unique_ptr<G> PG3 = std::unique_ptr<G>(new G{1, 2});
333 
334   std::unique_ptr<H> PH1 = std::unique_ptr<H>(new H({1, 2, 3}));
335   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
336   // CHECK-FIXES: std::unique_ptr<H> PH1 = std::unique_ptr<H>(new H({1, 2, 3}));
337   PH1.reset(new H({1, 2, 3}));
338   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead
339   // CHECK-FIXES: PH1.reset(new H({1, 2, 3}));
340 
341   std::unique_ptr<H> PH2 = std::unique_ptr<H>(new H({1, 2, 3}, 1));
342   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
343   // CHECK-FIXES: std::unique_ptr<H> PH2 = std::unique_ptr<H>(new H({1, 2, 3}, 1));
344   PH2.reset(new H({1, 2, 3}, 1));
345   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead
346   // CHECK-FIXES: PH2.reset(new H({1, 2, 3}, 1));
347 
348   std::unique_ptr<H> PH3 = std::unique_ptr<H>(new H({1, 2, 3}, 1.0));
349   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
350   // CHECK-FIXES: std::unique_ptr<H> PH3 = std::unique_ptr<H>(new H({1, 2, 3}, 1.0));
351   PH3.reset(new H({1, 2, 3}, 1.0));
352   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead
353   // CHECK-FIXES: PH3.reset(new H({1, 2, 3}, 1.0));
354 
355   std::unique_ptr<I> PI1 = std::unique_ptr<I>(new I(G({1, 2, 3})));
356   // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead
357   // CHECK-FIXES: std::unique_ptr<I> PI1 = std::make_unique<I>(G({1, 2, 3}));
358   PI1.reset(new I(G({1, 2, 3})));
359   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead
360   // CHECK-FIXES: PI1 = std::make_unique<I>(G({1, 2, 3}));
361 
362   std::unique_ptr<Foo> FF = std::unique_ptr<Foo>(new Foo());
363   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning:
364   // CHECK-FIXES: std::unique_ptr<Foo> FF = std::make_unique<Foo>();
365   FF.reset(new Foo());
366   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning:
367   // CHECK-FIXES: FF = std::make_unique<Foo>();
368 
369   std::unique_ptr<bar::Bar> BB = std::unique_ptr<bar::Bar>(new bar::Bar());
370   // CHECK-MESSAGES: :[[@LINE-1]]:34: warning:
371   // CHECK-FIXES: std::unique_ptr<bar::Bar> BB = std::make_unique<bar::Bar>();
372   BB.reset(new bar::Bar());
373   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning:
374   // CHECK-FIXES: BB = std::make_unique<bar::Bar>();
375 
376   std::unique_ptr<Foo[]> FFs;
377   FFs.reset(new Foo[5]);
378   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning:
379   // CHECK-FIXES: FFs = std::make_unique<Foo[]>(5);
380   FFs.reset(new Foo[5]());
381   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning:
382   // CHECK-FIXES: FFs = std::make_unique<Foo[]>(5);
383   const int Num = 1;
384   FFs.reset(new Foo[Num]);
385   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning:
386   // CHECK-FIXES: FFs = std::make_unique<Foo[]>(Num);
387   int Num2 = 1;
388   FFs.reset(new Foo[Num2]);
389   // CHECK-MESSAGES: :[[@LINE-1]]:7: warning:
390   // CHECK-FIXES: FFs = std::make_unique<Foo[]>(Num2);
391 
392   std::unique_ptr<int[]> FI;
393   FI.reset(new int[5]);
394   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning:
395   // CHECK-FIXES: FI = std::make_unique<int[]>(5);
396   FI.reset(new int[5]());
397   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning:
398   // CHECK-FIXES: FI = std::make_unique<int[]>(5);
399   FI.reset(new int[Num]);
400   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning:
401   // CHECK-FIXES: FI = std::make_unique<int[]>(Num);
402   FI.reset(new int[Num2]);
403   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning:
404   // CHECK-FIXES: FI = std::make_unique<int[]>(Num2);
405 }
406 
aliases()407 void aliases() {
408   typedef std::unique_ptr<int> IntPtr;
409   IntPtr Typedef = IntPtr(new int);
410   // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use std::make_unique instead
411   // CHECK-FIXES: IntPtr Typedef = std::make_unique<int>();
412 
413   // We use 'bool' instead of '_Bool'.
414   typedef std::unique_ptr<bool> BoolPtr;
415   BoolPtr BoolType = BoolPtr(new bool);
416   // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use std::make_unique instead
417   // CHECK-FIXES: BoolPtr BoolType = std::make_unique<bool>();
418 
419   // We use 'Base' instead of 'struct Base'.
420   typedef std::unique_ptr<Base> BasePtr;
421   BasePtr StructType = BasePtr(new Base);
422 // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead
423 // CHECK-FIXES: BasePtr StructType = std::make_unique<Base>();
424 
425 #define PTR unique_ptr<int>
426   std::unique_ptr<int> Macro = std::PTR(new int);
427 // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead
428 // CHECK-FIXES: std::unique_ptr<int> Macro = std::make_unique<int>();
429 #undef PTR
430 
431   std::unique_ptr<int> Using = unique_ptr_<int>(new int);
432   // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead
433   // CHECK-FIXES: std::unique_ptr<int> Using = std::make_unique<int>();
434 }
435 
whitespaces()436 void whitespaces() {
437   // clang-format off
438   auto Space = std::unique_ptr <int>(new int());
439   // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use std::make_unique instead
440   // CHECK-FIXES: auto Space = std::make_unique<int>();
441 
442   auto Spaces = std  ::    unique_ptr  <int>(new int());
443   // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use std::make_unique instead
444   // CHECK-FIXES: auto Spaces = std::make_unique<int>();
445   // clang-format on
446 }
447 
nesting()448 void nesting() {
449   auto Nest = std::unique_ptr<std::unique_ptr<int>>(new std::unique_ptr<int>(new int));
450   // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use std::make_unique instead
451   // CHECK-FIXES: auto Nest = std::make_unique<std::unique_ptr<int>>(new int);
452   Nest.reset(new std::unique_ptr<int>(new int));
453   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead
454   // CHECK-FIXES: Nest = std::make_unique<std::unique_ptr<int>>(new int);
455   Nest->reset(new int);
456   // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead
457   // CHECK-FIXES: *Nest = std::make_unique<int>();
458 }
459 
reset()460 void reset() {
461   std::unique_ptr<int> P;
462   P.reset();
463   P.reset(nullptr);
464   P.reset(new int());
465   // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use std::make_unique instead
466   // CHECK-FIXES: P = std::make_unique<int>();
467 
468   auto Q = &P;
469   Q->reset(new int());
470   // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead
471   // CHECK-FIXES: *Q = std::make_unique<int>();
472 }
473 
474 #define DEFINE(...) __VA_ARGS__
475 template<typename T>
g2(std::unique_ptr<Foo> * t)476 void g2(std::unique_ptr<Foo> *t) {
477   DEFINE(auto p = std::unique_ptr<Foo>(new Foo); t->reset(new Foo););
478 }
macro()479 void macro() {
480   std::unique_ptr<Foo> *t;
481   g2<bar::Bar>(t);
482 }
483 #undef DEFINE
484 
485 class UniqueFoo : public std::unique_ptr<Foo> {
486  public:
foo()487   void foo() {
488     reset(new Foo);
489     this->reset(new Foo);
490     // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use std::make_unique instead
491     // CHECK-FIXES: *this = std::make_unique<Foo>();
492     (*this).reset(new Foo);
493     // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead
494     // CHECK-FIXES: (*this) = std::make_unique<Foo>();
495   }
496 };
497 
498 // Ignore statements inside a template instantiation.
499 template<typename T>
template_fun(T * t)500 void template_fun(T* t) {
501   std::unique_ptr<T> t2 = std::unique_ptr<T>(new T);
502   t2.reset(new T);
503 }
504 
invoke_template()505 void invoke_template() {
506   Foo* foo;
507   template_fun(foo);
508 }
509 
no_fix_for_invalid_new_loc()510 void no_fix_for_invalid_new_loc() {
511   // FIXME: Although the code is valid, the end location of `new struct Base` is
512   // invalid. Correct it once https://bugs.llvm.org/show_bug.cgi?id=35952 is
513   // fixed.
514   auto T = std::unique_ptr<Base>(new struct Base);
515   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use std::make_unique instead
516   // CHECK-FIXES: auto T = std::unique_ptr<Base>(new struct Base);
517 }
518