616 lines · cpp
1// RUN: %check_clang_tidy -std=c++14-or-later %s modernize-make-unique %t -- -- -I %S/Inputs/smart-ptr2 3#include "unique_ptr.h"4#include "initializer_list.h"5// CHECK-FIXES: #include <memory>6 7struct Base {8 Base();9 Base(int, int);10};11 12struct Derived : public Base {13 Derived();14 Derived(int, int);15};16 17struct APair {18 int a, b;19};20 21struct DPair {22 DPair() : a(0), b(0) {}23 DPair(int x, int y) : a(y), b(x) {}24 int a, b;25};26 27template<typename T>28struct MyVector {29 MyVector(std::initializer_list<T>);30};31 32struct Empty {};33 34 35struct E {36 E(std::initializer_list<int>);37 E();38};39 40struct F {41 F(std::initializer_list<int>);42 F();43 int a;44};45 46struct G {47 G(std::initializer_list<int>);48 G(int);49};50 51struct H {52 H(std::vector<int>);53 H(std::vector<int> &, double);54 H(MyVector<int>, int);55};56 57struct I {58 I(G);59};60 61struct J {62 J(E e, int);63};64 65namespace {66class Foo {};67} // namespace68 69namespace bar {70class Bar {};71} // namespace bar72 73template <class T>74using unique_ptr_ = std::unique_ptr<T>;75 76void *operator new(__SIZE_TYPE__ Count, void *Ptr);77 78int g(std::unique_ptr<int> P);79 80std::unique_ptr<Base> getPointer() {81 return std::unique_ptr<Base>(new Base);82 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::make_unique instead83 // CHECK-FIXES: return std::make_unique<Base>();84}85 86std::unique_ptr<Base> getPointerValue() {87 return std::unique_ptr<Base>(new Base());88 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::make_unique instead89 // CHECK-FIXES: return std::make_unique<Base>();90}91 92void basic() {93 std::unique_ptr<int> P1 = std::unique_ptr<int>(new int());94 // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]95 // CHECK-FIXES: std::unique_ptr<int> P1 = std::make_unique<int>();96 std::unique_ptr<int> P2 = std::unique_ptr<int>(new int);97 98 P1.reset(new int());99 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]100 // CHECK-FIXES: P1 = std::make_unique<int>();101 P2.reset(new int);102 103 P1 = std::unique_ptr<int>(new int());104 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique]105 // CHECK-FIXES: P1 = std::make_unique<int>();106 P1 = std::unique_ptr<int>(new int);107 108 // Without parenthesis, default initialization.109 std::unique_ptr<int> P3 = std::unique_ptr<int>(new int);110 111 P2.reset(new int);112 113 P2 = std::unique_ptr<int>(new int);114 115 // With auto.116 auto P4 = std::unique_ptr<int>(new int());117 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead118 // CHECK-FIXES: auto P4 = std::make_unique<int>();119 auto P5 = std::unique_ptr<int>(new int);120 121 std::unique_ptr<int> P6 = std::unique_ptr<int>((new int()));122 // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]123 // CHECK-FIXES: std::unique_ptr<int> P6 = std::make_unique<int>();124 std::unique_ptr<int> P7 = std::unique_ptr<int>((new int));125 126 P4.reset((new int()));127 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]128 // CHECK-FIXES: P4 = std::make_unique<int>();129 P5.reset((new int));130 131 std::unique_ptr<int> P8 = std::unique_ptr<int>((((new int()))));132 // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique]133 // CHECK-FIXES: std::unique_ptr<int> P8 = std::make_unique<int>();134 std::unique_ptr<int> P9 = std::unique_ptr<int>((((new int))));135 136 P5.reset(((((new int())))));137 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique]138 // CHECK-FIXES: P5 = std::make_unique<int>();139 P6.reset(((((new int)))));140 141 {142 // No std.143 using namespace std;144 unique_ptr<int> Q = unique_ptr<int>(new int());145 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use std::make_unique instead146 // CHECK-FIXES: unique_ptr<int> Q = std::make_unique<int>();147 unique_ptr<int> P = unique_ptr<int>(new int);148 149 Q = unique_ptr<int>(new int());150 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead151 // CHECK-FIXES: Q = std::make_unique<int>();152 153 P = unique_ptr<int>(new int);154 }155 156 std::unique_ptr<int> R(new int());157 // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead158 // CHECK-FIXES: std::unique_ptr<int> R = std::make_unique<int>();159 std::unique_ptr<int> S(new int);160 161 // Create the unique_ptr as a parameter to a function.162 int T = g(std::unique_ptr<int>(new int()));163 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead164 // CHECK-FIXES: int T = g(std::make_unique<int>());165 T = g(std::unique_ptr<int>(new int));166 167 // Only replace if the type in the template is the same as the type returned168 // by the new operator.169 auto Pderived = std::unique_ptr<Base>(new Derived());170 auto PderivedNoparen = std::unique_ptr<Base>(new Derived);171 172 // OK to replace for reset and assign173 Pderived.reset(new Derived());174 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use std::make_unique instead175 // CHECK-FIXES: Pderived = std::make_unique<Derived>();176 PderivedNoparen.reset(new Derived);177 // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use std::make_unique instead178 // CHECK-FIXES: PderivedNoparen = std::make_unique<Derived>();179 180 Pderived = std::unique_ptr<Derived>(new Derived());181 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use std::make_unique instead182 // CHECK-FIXES: Pderived = std::make_unique<Derived>();183 PderivedNoparen = std::unique_ptr<Derived>(new Derived);184 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use std::make_unique instead185 // CHECK-FIXES: PderivedNoparen = std::make_unique<Derived>();186 187 // FIXME: OK to replace if assigned to unique_ptr<Base>188 Pderived = std::unique_ptr<Base>(new Derived());189 Pderived = std::unique_ptr<Base>(new Derived);190 191 // FIXME: OK to replace when auto is not used192 std::unique_ptr<Base> PBase = std::unique_ptr<Base>(new Derived());193 std::unique_ptr<Base> PBaseNoparen = std::unique_ptr<Base>(new Derived);194 195 // The pointer is returned by the function, nothing to do.196 std::unique_ptr<Base> RetPtr = getPointer();197 RetPtr = getPointerValue();198 199 // This emulates std::move.200 std::unique_ptr<int> Move = static_cast<std::unique_ptr<int> &&>(P1);201 202 // Placement arguments should not be removed.203 int *PInt = new int;204 std::unique_ptr<int> Placement = std::unique_ptr<int>(new (PInt) int{3});205 Placement.reset(new (PInt) int{3});206 Placement = std::unique_ptr<int>(new (PInt) int{3});207 208 std::unique_ptr<int> PlacementNoparen = std::unique_ptr<int>(new (PInt) int);209 PlacementNoparen.reset(new (PInt) int);210 PlacementNoparen = std::unique_ptr<int>(new (PInt) int);211}212 213// Calling make_smart_ptr from within a member function of a type with a214// private or protected constructor would be ill-formed.215class Private {216private:217 Private(int z) {}218 219public:220 Private() {}221 void create() {222 auto callsPublic = std::unique_ptr<Private>(new Private);223 // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead224 // CHECK-FIXES: auto callsPublic = std::make_unique<Private>();225 auto ptr = std::unique_ptr<Private>(new Private(42));226 ptr.reset(new Private(42));227 ptr = std::unique_ptr<Private>(new Private(42));228 }229 230 virtual ~Private();231};232 233class Protected {234protected:235 Protected() {}236 237public:238 Protected(int, int) {}239 void create() {240 auto callsPublic = std::unique_ptr<Protected>(new Protected(1, 2));241 // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead242 // CHECK-FIXES: auto callsPublic = std::make_unique<Protected>(1, 2);243 auto ptr = std::unique_ptr<Protected>(new Protected);244 ptr.reset(new Protected);245 ptr = std::unique_ptr<Protected>(new Protected);246 }247};248 249void initialization(int T, Base b) {250 // Test different kinds of initialization of the pointee.251 252 // Direct initialization with parenthesis.253 std::unique_ptr<DPair> PDir1 = std::unique_ptr<DPair>(new DPair(1, T));254 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead255 // CHECK-FIXES: std::unique_ptr<DPair> PDir1 = std::make_unique<DPair>(1, T);256 PDir1.reset(new DPair(1, T));257 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead258 // CHECK-FIXES: PDir1 = std::make_unique<DPair>(1, T);259 260 // Direct initialization with braces.261 std::unique_ptr<DPair> PDir2 = std::unique_ptr<DPair>(new DPair{2, T});262 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead263 // CHECK-FIXES: std::unique_ptr<DPair> PDir2 = std::make_unique<DPair>(2, T);264 PDir2.reset(new DPair{2, T});265 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead266 // CHECK-FIXES: PDir2 = std::make_unique<DPair>(2, T);267 268 // Aggregate initialization.269 std::unique_ptr<APair> PAggr = std::unique_ptr<APair>(new APair{T, 1});270 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead271 // CHECK-FIXES: std::unique_ptr<APair> PAggr = std::make_unique<APair>(APair{T, 1});272 PAggr.reset(new APair{T, 1});273 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead274 // CHECK-FIXES: PAggr = std::make_unique<APair>(APair{T, 1});275 276 // Check aggregate init with intermediate temporaries.277 std::unique_ptr<APair> PAggrTemp = std::unique_ptr<APair>(new APair({T, 1}));278 // CHECK-MESSAGES: :[[@LINE-1]]:38: warning: use std::make_unique instead279 // CHECK-FIXES: std::unique_ptr<APair> PAggrTemp = std::unique_ptr<APair>(new APair({T, 1}));280 PAggrTemp.reset(new APair({T, 1}));281 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead282 // CHECK-FIXES: PAggrTemp.reset(new APair({T, 1}));283 284 // Test different kinds of initialization of the pointee, when the unique_ptr285 // is initialized with braces.286 287 // Direct initialization with parenthesis.288 std::unique_ptr<DPair> PDir3 = std::unique_ptr<DPair>{new DPair(3, T)};289 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead290 // CHECK-FIXES: std::unique_ptr<DPair> PDir3 = std::make_unique<DPair>(3, T);291 292 // Direct initialization with braces.293 std::unique_ptr<DPair> PDir4 = std::unique_ptr<DPair>{new DPair{4, T}};294 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead295 // CHECK-FIXES: std::unique_ptr<DPair> PDir4 = std::make_unique<DPair>(4, T);296 297 // Aggregate initialization.298 std::unique_ptr<APair> PAggr2 = std::unique_ptr<APair>{new APair{T, 2}};299 // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: use std::make_unique instead300 // CHECK-FIXES: std::unique_ptr<APair> PAggr2 = std::make_unique<APair>(APair{T, 2});301 302 // Direct initialization with parenthesis, without arguments.303 std::unique_ptr<DPair> PDir5 = std::unique_ptr<DPair>(new DPair());304 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead305 // CHECK-FIXES: std::unique_ptr<DPair> PDir5 = std::make_unique<DPair>();306 307 // Direct initialization with braces, without arguments.308 std::unique_ptr<DPair> PDir6 = std::unique_ptr<DPair>(new DPair{});309 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead310 // CHECK-FIXES: std::unique_ptr<DPair> PDir6 = std::make_unique<DPair>();311 312 // Aggregate initialization without arguments.313 std::unique_ptr<Empty> PEmpty = std::unique_ptr<Empty>(new Empty{});314 // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: use std::make_unique instead315 // CHECK-FIXES: std::unique_ptr<Empty> PEmpty = std::make_unique<Empty>(Empty{});316 317 // Initialization with default constructor.318 std::unique_ptr<E> PE1 = std::unique_ptr<E>(new E{});319 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead320 // CHECK-FIXES: std::unique_ptr<E> PE1 = std::make_unique<E>();321 PE1.reset(new E{});322 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead323 // CHECK-FIXES: PE1 = std::make_unique<E>();324 325 // No warnings for `auto` new expression.326 PE1.reset(new auto(E()));327 328 //============================================================================329 // NOTE: For initializer-list constructors, the check only gives warnings,330 // and no fixes are generated.331 //============================================================================332 333 // Initialization with the initializer-list constructor.334 std::unique_ptr<E> PE2 = std::unique_ptr<E>(new E{1, 2});335 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead336 // CHECK-FIXES: std::unique_ptr<E> PE2 = std::unique_ptr<E>(new E{1, 2});337 PE2.reset(new E{1, 2});338 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead339 // CHECK-FIXES: PE2.reset(new E{1, 2});340 341 // Initialization with default constructor.342 std::unique_ptr<F> PF1 = std::unique_ptr<F>(new F());343 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead344 // CHECK-FIXES: std::unique_ptr<F> PF1 = std::make_unique<F>();345 PF1.reset(new F());346 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead347 // CHECK-FIXES: PF1 = std::make_unique<F>();348 349 // Initialization with default constructor.350 std::unique_ptr<F> PF2 = std::unique_ptr<F>(new F{});351 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead352 // CHECK-FIXES: std::unique_ptr<F> PF2 = std::make_unique<F>();353 PF2.reset(new F());354 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead355 // CHECK-FIXES: PF2 = std::make_unique<F>();356 357 // Initialization with the initializer-list constructor.358 std::unique_ptr<F> PF3 = std::unique_ptr<F>(new F{1});359 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead360 // CHECK-FIXES: std::unique_ptr<F> PF3 = std::unique_ptr<F>(new F{1});361 PF3.reset(new F{1});362 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead363 // CHECK-FIXES: PF3.reset(new F{1});364 365 // Initialization with the initializer-list constructor.366 std::unique_ptr<F> PF4 = std::unique_ptr<F>(new F{1, 2});367 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead368 // CHECK-FIXES: std::unique_ptr<F> PF4 = std::unique_ptr<F>(new F{1, 2});369 370 // Initialization with the initializer-list constructor.371 std::unique_ptr<F> PF5 = std::unique_ptr<F>(new F({1, 2}));372 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead373 // CHECK-FIXES: std::unique_ptr<F> PF5 = std::unique_ptr<F>(new F({1, 2}));374 375 // Initialization with the initializer-list constructor as the default376 // constructor is not present.377 std::unique_ptr<G> PG1 = std::unique_ptr<G>(new G{});378 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead379 // CHECK-FIXES: std::unique_ptr<G> PG1 = std::unique_ptr<G>(new G{});380 PG1.reset(new G{});381 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead382 // CHECK-FIXES: PG1.reset(new G{});383 384 // Initialization with the initializer-list constructor.385 std::unique_ptr<G> PG2 = std::unique_ptr<G>(new G{1});386 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead387 // CHECK-FIXES: std::unique_ptr<G> PG2 = std::unique_ptr<G>(new G{1});388 389 // Initialization with the initializer-list constructor.390 std::unique_ptr<G> PG3 = std::unique_ptr<G>(new G{1, 2});391 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead392 // CHECK-FIXES: std::unique_ptr<G> PG3 = std::unique_ptr<G>(new G{1, 2});393 394 std::unique_ptr<H> PH1 = std::unique_ptr<H>(new H({1, 2, 3}));395 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead396 // CHECK-FIXES: std::unique_ptr<H> PH1 = std::unique_ptr<H>(new H({1, 2, 3}));397 PH1.reset(new H({1, 2, 3}));398 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead399 // CHECK-FIXES: PH1.reset(new H({1, 2, 3}));400 401 std::unique_ptr<H> PH2 = std::unique_ptr<H>(new H({1, 2, 3}, 1));402 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead403 // CHECK-FIXES: std::unique_ptr<H> PH2 = std::unique_ptr<H>(new H({1, 2, 3}, 1));404 PH2.reset(new H({1, 2, 3}, 1));405 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead406 // CHECK-FIXES: PH2.reset(new H({1, 2, 3}, 1));407 408 std::unique_ptr<H> PH3 = std::unique_ptr<H>(new H({1, 2, 3}, 1.0));409 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead410 // CHECK-FIXES: std::unique_ptr<H> PH3 = std::unique_ptr<H>(new H({1, 2, 3}, 1.0));411 PH3.reset(new H({1, 2, 3}, 1.0));412 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead413 // CHECK-FIXES: PH3.reset(new H({1, 2, 3}, 1.0));414 415 std::unique_ptr<I> PI1 = std::unique_ptr<I>(new I(G({1, 2, 3})));416 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead417 // CHECK-FIXES: std::unique_ptr<I> PI1 = std::make_unique<I>(G({1, 2, 3}));418 PI1.reset(new I(G({1, 2, 3})));419 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead420 // CHECK-FIXES: PI1 = std::make_unique<I>(G({1, 2, 3}));421 422 std::unique_ptr<J> PJ1 = std::unique_ptr<J>(new J({1, 2}, 1));423 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead424 // CHECK-FIXES: std::unique_ptr<J> PJ1 = std::unique_ptr<J>(new J({1, 2}, 1));425 PJ1.reset(new J({1, 2}, 1));426 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead427 // CHECK-FIXES: PJ1.reset(new J({1, 2}, 1));428 429 std::unique_ptr<J> PJ2 = std::unique_ptr<J>(new J(E{1, 2}, 1));430 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead431 // CHECK-FIXES: std::unique_ptr<J> PJ2 = std::unique_ptr<J>(new J(E{1, 2}, 1));432 PJ2.reset(new J(E{1, 2}, 1));433 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead434 // CHECK-FIXES: PJ2.reset(new J(E{1, 2}, 1));435 436 std::unique_ptr<J> PJ3 = std::unique_ptr<J>(new J{ {1, 2}, 1 });437 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead438 // CHECK-FIXES: std::unique_ptr<J> PJ3 = std::unique_ptr<J>(new J{ {1, 2}, 1 });439 PJ3.reset(new J{ {1, 2}, 1 });440 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead441 // CHECK-FIXES: PJ3.reset(new J{ {1, 2}, 1 });442 443 std::unique_ptr<J> PJ4 = std::unique_ptr<J>(new J{E{1, 2}, 1});444 // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead445 // CHECK-FIXES: std::unique_ptr<J> PJ4 = std::unique_ptr<J>(new J{E{1, 2}, 1});446 PJ4.reset(new J{E{1, 2}, 1});447 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead448 // CHECK-FIXES: PJ4.reset(new J{E{1, 2}, 1});449 450 std::unique_ptr<Foo> FF = std::unique_ptr<Foo>(new Foo());451 // CHECK-MESSAGES: :[[@LINE-1]]:29: warning:452 // CHECK-FIXES: std::unique_ptr<Foo> FF = std::make_unique<Foo>();453 FF.reset(new Foo());454 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning:455 // CHECK-FIXES: FF = std::make_unique<Foo>();456 457 std::unique_ptr<bar::Bar> BB = std::unique_ptr<bar::Bar>(new bar::Bar());458 // CHECK-MESSAGES: :[[@LINE-1]]:34: warning:459 // CHECK-FIXES: std::unique_ptr<bar::Bar> BB = std::make_unique<bar::Bar>();460 BB.reset(new bar::Bar());461 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning:462 // CHECK-FIXES: BB = std::make_unique<bar::Bar>();463 464 std::unique_ptr<Foo[]> FFs;465 FFs.reset(new Foo[5]);466 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning:467 // CHECK-FIXES: FFs = std::make_unique<Foo[]>(5);468 FFs.reset(new Foo[5]());469 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning:470 // CHECK-FIXES: FFs = std::make_unique<Foo[]>(5);471 const int Num = 1;472 FFs.reset(new Foo[Num]);473 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning:474 // CHECK-FIXES: FFs = std::make_unique<Foo[]>(Num);475 int Num2 = 1;476 FFs.reset(new Foo[Num2]);477 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning:478 // CHECK-FIXES: FFs = std::make_unique<Foo[]>(Num2);479 480 std::unique_ptr<int[]> FI;481 FI.reset(new int[5]()); // value initialization.482 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning:483 // CHECK-FIXES: FI = std::make_unique<int[]>(5); // value initialization.484 485 // The check doesn't give warnings and fixes for cases where the original new486 // expression does default initialization.487 FI.reset(new int[5]);488 FI.reset(new int[Num]);489 FI.reset(new int[Num2]);490}491 492void aliases() {493 typedef std::unique_ptr<int> IntPtr;494 IntPtr Typedef = IntPtr(new int());495 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use std::make_unique instead496 // CHECK-FIXES: IntPtr Typedef = std::make_unique<int>();497 IntPtr Typedef2 = IntPtr(new int);498 499 // We use 'bool' instead of '_Bool'.500 typedef std::unique_ptr<bool> BoolPtr;501 BoolPtr BoolType = BoolPtr(new bool());502 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use std::make_unique instead503 // CHECK-FIXES: BoolPtr BoolType = std::make_unique<bool>();504 BoolPtr BoolType2 = BoolPtr(new bool);505 506 // We use 'Base' instead of 'struct Base'.507 typedef std::unique_ptr<Base> BasePtr;508 BasePtr StructType = BasePtr(new Base);509// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead510// CHECK-FIXES: BasePtr StructType = std::make_unique<Base>();511 512#define PTR unique_ptr<int>513 std::unique_ptr<int> Macro = std::PTR(new int());514 // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead515 // CHECK-FIXES: std::unique_ptr<int> Macro = std::make_unique<int>();516 std::unique_ptr<int> Macro2 = std::PTR(new int);517#undef PTR518 519 std::unique_ptr<int> Using = unique_ptr_<int>(new int());520 // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead521 // CHECK-FIXES: std::unique_ptr<int> Using = std::make_unique<int>();522 std::unique_ptr<int> Using2 = unique_ptr_<int>(new int);523}524 525void whitespaces() {526 // clang-format off527 auto Space = std::unique_ptr <int>(new int());528 // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use std::make_unique instead529 // CHECK-FIXES: auto Space = std::make_unique<int>();530 auto Space2 = std::unique_ptr <int>(new int);531 532 auto Spaces = std :: unique_ptr <int>(new int());533 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use std::make_unique instead534 // CHECK-FIXES: auto Spaces = std::make_unique<int>();535 auto Spaces2 = std :: unique_ptr <int>(new int);536 // clang-format on537}538 539void nesting() {540 auto Nest = std::unique_ptr<std::unique_ptr<int>>(new std::unique_ptr<int>(new int));541 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use std::make_unique instead542 // CHECK-FIXES: auto Nest = std::make_unique<std::unique_ptr<int>>(new int);543 Nest.reset(new std::unique_ptr<int>(new int));544 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead545 // CHECK-FIXES: Nest = std::make_unique<std::unique_ptr<int>>(new int);546 Nest->reset(new int());547 // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead548 // CHECK-FIXES: *Nest = std::make_unique<int>();549}550 551void reset() {552 std::unique_ptr<int> P;553 P.reset();554 P.reset(nullptr);555 P.reset(new int());556 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use std::make_unique instead557 // CHECK-FIXES: P = std::make_unique<int>();558 P.reset(new int);559 560 auto Q = &P;561 Q->reset(new int());562 // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead563 // CHECK-FIXES: *Q = std::make_unique<int>();564 Q->reset(new int);565}566 567#define DEFINE(...) __VA_ARGS__568template<typename T>569void g2(std::unique_ptr<Foo> *t) {570 DEFINE(auto p = std::unique_ptr<Foo>(new Foo); t->reset(new Foo););571}572void macro() {573 std::unique_ptr<Foo> *t;574 g2<bar::Bar>(t);575}576#undef DEFINE577 578class UniqueFoo : public std::unique_ptr<Foo> {579 public:580 void foo() {581 reset(new Foo);582 this->reset(new Foo);583 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use std::make_unique instead584 // CHECK-FIXES: *this = std::make_unique<Foo>();585 this->reset(new Foo());586 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use std::make_unique instead587 // CHECK-FIXES: *this = std::make_unique<Foo>();588 (*this).reset(new Foo);589 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead590 // CHECK-FIXES: (*this) = std::make_unique<Foo>();591 (*this).reset(new Foo());592 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead593 // CHECK-FIXES: (*this) = std::make_unique<Foo>();594 }595};596 597// Ignore statements inside a template instantiation.598template<typename T>599void template_fun(T* t) {600 std::unique_ptr<T> t2 = std::unique_ptr<T>(new T);601 std::unique_ptr<T> t3 = std::unique_ptr<T>(new T());602 t2.reset(new T);603 t3.reset(new T());604}605 606void invoke_template() {607 Foo* foo;608 template_fun(foo);609}610 611void fix_for_c_style_struct() {612 auto T = std::unique_ptr<Base>(new struct Base);613 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use std::make_unique instead614 // CHECK-FIXES: auto T = std::make_unique<Base>();615}616