// RUN: %check_clang_tidy %s modernize-make-unique %t namespace std { template class default_delete {}; template > class unique_ptr { public: unique_ptr(type *ptr); unique_ptr(const unique_ptr &t) = delete; unique_ptr(unique_ptr &&t); ~unique_ptr(); type &operator*() { return *ptr; } type *operator->() { return ptr; } type *release(); void reset(); void reset(type *pt); private: type *ptr; }; } struct Base { Base(); Base(int, int); }; struct Derived : public Base { Derived(); Derived(int, int); }; struct APair { int a, b; }; struct DPair { DPair() : a(0), b(0) {} DPair(int x, int y) : a(y), b(x) {} int a, b; }; struct Empty {}; template using unique_ptr_ = std::unique_ptr; void *operator new(__SIZE_TYPE__ Count, void *Ptr); int g(std::unique_ptr P); std::unique_ptr getPointer() { return std::unique_ptr(new Base); // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::make_unique instead // CHECK-FIXES: return std::make_unique(); } void basic() { std::unique_ptr P1 = std::unique_ptr(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique] // CHECK-FIXES: std::unique_ptr P1 = std::make_unique(); // Without parenthesis. std::unique_ptr P2 = std::unique_ptr(new int); // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique] // CHECK-FIXES: std::unique_ptr P2 = std::make_unique(); // With auto. auto P3 = std::unique_ptr(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead // CHECK-FIXES: auto P3 = std::make_unique(); { // No std. using namespace std; unique_ptr Q = unique_ptr(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use std::make_unique instead // CHECK-FIXES: unique_ptr Q = std::make_unique(); } std::unique_ptr R(new int()); // Create the unique_ptr as a parameter to a function. int T = g(std::unique_ptr(new int())); // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead // CHECK-FIXES: int T = g(std::make_unique()); // Only replace if the type in the template is the same than the type returned // by the new operator. auto Pderived = std::unique_ptr(new Derived()); // The pointer is returned by the function, nothing to do. std::unique_ptr RetPtr = getPointer(); // This emulates std::move. std::unique_ptr Move = static_cast&&>(P1); // Placemenet arguments should not be removed. int *PInt = new int; std::unique_ptr Placement = std::unique_ptr(new (PInt) int{3}); } void initialization(int T, Base b) { // Test different kinds of initialization of the pointee. // Direct initialization with parenthesis. std::unique_ptr PDir1 = std::unique_ptr(new DPair(1, T)); // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead // CHECK-FIXES: std::unique_ptr PDir1 = std::make_unique(1, T); // Direct initialization with braces. std::unique_ptr PDir2 = std::unique_ptr(new DPair{2, T}); // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead // CHECK-FIXES: std::unique_ptr PDir2 = std::make_unique(2, T); // Aggregate initialization. std::unique_ptr PAggr = std::unique_ptr(new APair{T, 1}); // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead // CHECK-FIXES: std::unique_ptr PAggr = std::make_unique(APair{T, 1}); // Test different kinds of initialization of the pointee, when the unique_ptr // is initialized with braces. // Direct initialization with parenthesis. std::unique_ptr PDir3 = std::unique_ptr{new DPair(3, T)}; // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead // CHECK-FIXES: std::unique_ptr PDir3 = std::make_unique(3, T); // Direct initialization with braces. std::unique_ptr PDir4 = std::unique_ptr{new DPair{4, T}}; // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead // CHECK-FIXES: std::unique_ptr PDir4 = std::make_unique(4, T); // Aggregate initialization. std::unique_ptr PAggr2 = std::unique_ptr{new APair{T, 2}}; // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: use std::make_unique instead // CHECK-FIXES: std::unique_ptr PAggr2 = std::make_unique(APair{T, 2}); // Direct initialization with parenthesis, without arguments. std::unique_ptr PDir5 = std::unique_ptr(new DPair()); // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead // CHECK-FIXES: std::unique_ptr PDir5 = std::make_unique(); // Direct initialization with braces, without arguments. std::unique_ptr PDir6 = std::unique_ptr(new DPair{}); // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead // CHECK-FIXES: std::unique_ptr PDir6 = std::make_unique(); // Aggregate initialization without arguments. std::unique_ptr PEmpty = std::unique_ptr(new Empty{}); // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: use std::make_unique instead // CHECK-FIXES: std::unique_ptr PEmpty = std::make_unique(Empty{}); } void aliases() { typedef std::unique_ptr IntPtr; IntPtr Typedef = IntPtr(new int); // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use std::make_unique instead // CHECK-FIXES: IntPtr Typedef = std::make_unique(); // We use 'bool' instead of '_Bool'. typedef std::unique_ptr BoolPtr; BoolPtr BoolType = BoolPtr(new bool); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use std::make_unique instead // CHECK-FIXES: BoolPtr BoolType = std::make_unique(); // We use 'Base' instead of 'struct Base'. typedef std::unique_ptr BasePtr; BasePtr StructType = BasePtr(new Base); // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead // CHECK-FIXES: BasePtr StructType = std::make_unique(); #define PTR unique_ptr std::unique_ptr Macro = std::PTR(new int); // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead // CHECK-FIXES: std::unique_ptr Macro = std::make_unique(); #undef PTR std::unique_ptr Using = unique_ptr_(new int); // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead // CHECK-FIXES: std::unique_ptr Using = std::make_unique(); } void whitespaces() { auto Space = std::unique_ptr (new int()); // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use std::make_unique instead // CHECK-FIXES: auto Space = std::make_unique(); auto Spaces = std :: unique_ptr (new int()); // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use std::make_unique instead // CHECK-FIXES: auto Spaces = std::make_unique(); } void nesting() { auto Nest = std::unique_ptr>(new std::unique_ptr(new int)); // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use std::make_unique instead // CHECK-FIXES: auto Nest = std::make_unique>(new int); }