Manuel Klimek | b91bee0 | 2015-10-22 11:31:44 +0000 | [diff] [blame^] | 1 | // RUN: %check_clang_tidy %s modernize-make-unique %t |
Angel Garcia Gomez | 26fd0e8 | 2015-09-29 09:36:41 +0000 | [diff] [blame] | 2 | |
| 3 | namespace std { |
| 4 | |
Angel Garcia Gomez | b9f3059 | 2015-10-05 12:20:17 +0000 | [diff] [blame] | 5 | template <typename T> |
| 6 | class default_delete {}; |
| 7 | |
| 8 | template <typename type, typename Deleter = std::default_delete<type>> |
Angel Garcia Gomez | 26fd0e8 | 2015-09-29 09:36:41 +0000 | [diff] [blame] | 9 | class unique_ptr { |
| 10 | public: |
| 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 | |
| 21 | private: |
| 22 | type *ptr; |
| 23 | }; |
| 24 | |
| 25 | } |
| 26 | |
| 27 | struct Base { |
| 28 | Base(); |
| 29 | Base(int, int); |
| 30 | }; |
| 31 | |
| 32 | struct Derived : public Base { |
| 33 | Derived(); |
| 34 | Derived(int, int); |
| 35 | }; |
| 36 | |
Angel Garcia Gomez | baf573e | 2015-10-14 09:22:32 +0000 | [diff] [blame] | 37 | struct APair { |
Angel Garcia Gomez | 26fd0e8 | 2015-09-29 09:36:41 +0000 | [diff] [blame] | 38 | int a, b; |
| 39 | }; |
| 40 | |
Angel Garcia Gomez | baf573e | 2015-10-14 09:22:32 +0000 | [diff] [blame] | 41 | struct DPair { |
| 42 | DPair() : a(0), b(0) {} |
| 43 | DPair(int x, int y) : a(y), b(x) {} |
| 44 | int a, b; |
| 45 | }; |
| 46 | |
| 47 | struct Empty {}; |
| 48 | |
Angel Garcia Gomez | 26fd0e8 | 2015-09-29 09:36:41 +0000 | [diff] [blame] | 49 | template<class T> using unique_ptr_ = std::unique_ptr<T>; |
| 50 | |
Angel Garcia Gomez | 9eb6e2e | 2015-10-14 10:30:32 +0000 | [diff] [blame] | 51 | void *operator new(__SIZE_TYPE__ Count, void *Ptr); |
Angel Garcia Gomez | baf573e | 2015-10-14 09:22:32 +0000 | [diff] [blame] | 52 | |
Angel Garcia Gomez | 26fd0e8 | 2015-09-29 09:36:41 +0000 | [diff] [blame] | 53 | int g(std::unique_ptr<int> P); |
| 54 | |
| 55 | std::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 Gomez | baf573e | 2015-10-14 09:22:32 +0000 | [diff] [blame] | 61 | void basic() { |
Angel Garcia Gomez | 26fd0e8 | 2015-09-29 09:36:41 +0000 | [diff] [blame] | 62 | 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 Gomez | 26fd0e8 | 2015-09-29 09:36:41 +0000 | [diff] [blame] | 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 | |
Angel Garcia Gomez | baf573e | 2015-10-14 09:22:32 +0000 | [diff] [blame] | 98 | // 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 | |
| 106 | void 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 | |
| 160 | void aliases() { |
Angel Garcia Gomez | 26fd0e8 | 2015-09-29 09:36:41 +0000 | [diff] [blame] | 161 | 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 | |
| 166 | #define PTR unique_ptr<int> |
| 167 | std::unique_ptr<int> Macro = std::PTR(new int); |
| 168 | // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead |
| 169 | // CHECK-FIXES: std::unique_ptr<int> Macro = std::make_unique<int>(); |
| 170 | #undef PTR |
| 171 | |
| 172 | std::unique_ptr<int> Using = unique_ptr_<int>(new int); |
| 173 | // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead |
| 174 | // CHECK-FIXES: std::unique_ptr<int> Using = std::make_unique<int>(); |
Angel Garcia Gomez | baf573e | 2015-10-14 09:22:32 +0000 | [diff] [blame] | 175 | } |
Angel Garcia Gomez | 26fd0e8 | 2015-09-29 09:36:41 +0000 | [diff] [blame] | 176 | |
Angel Garcia Gomez | baf573e | 2015-10-14 09:22:32 +0000 | [diff] [blame] | 177 | void whitespaces() { |
Angel Garcia Gomez | 26fd0e8 | 2015-09-29 09:36:41 +0000 | [diff] [blame] | 178 | auto Space = std::unique_ptr <int>(new int()); |
| 179 | // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use std::make_unique instead |
| 180 | // CHECK-FIXES: auto Space = std::make_unique<int>(); |
| 181 | |
| 182 | auto Spaces = std :: unique_ptr <int>(new int()); |
| 183 | // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use std::make_unique instead |
| 184 | // CHECK-FIXES: auto Spaces = std::make_unique<int>(); |
| 185 | } |