| 1 |
// RUN: %check_clang_tidy -std=c++14-or-later %s modernize-make-unique %t -- -- -I %S/Inputs/smart-ptr |
| 2 |
|
| 3 |
#include "unique_ptr.h" |
| 4 |
#include "initializer_list.h" |
| 5 |
// CHECK-FIXES: #include <memory> |
| 6 |
|
| 7 |
struct Base { |
| 8 |
Base(); |
| 9 |
Base(int, int); |
| 10 |
}; |
| 11 |
|
| 12 |
struct Derived : public Base { |
| 13 |
Derived(); |
| 14 |
Derived(int, int); |
| 15 |
}; |
| 16 |
|
| 17 |
struct APair { |
| 18 |
int a, b; |
| 19 |
}; |
| 20 |
|
| 21 |
struct DPair { |
| 22 |
DPair() : a(0), b(0) {} |
| 23 |
DPair(int x, int y) : a(y), b(x) {} |
| 24 |
int a, b; |
| 25 |
}; |
| 26 |
|
| 27 |
template<typename T> |
| 28 |
struct MyVector { |
| 29 |
MyVector(std::initializer_list<T>); |
| 30 |
}; |
| 31 |
|
| 32 |
struct Empty {}; |
| 33 |
|
| 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 |
struct J { |
| 62 |
J(E e, int); |
| 63 |
}; |
| 64 |
|
| 65 |
namespace { |
| 66 |
class Foo {}; |
| 67 |
} // namespace |
| 68 |
|
| 69 |
namespace bar { |
| 70 |
class Bar {}; |
| 71 |
} // namespace bar |
| 72 |
|
| 73 |
template <class T> |
| 74 |
using unique_ptr_ = std::unique_ptr<T>; |
| 75 |
|
| 76 |
void *operator new(__SIZE_TYPE__ Count, void *Ptr); |
| 77 |
|
| 78 |
int g(std::unique_ptr<int> P); |
| 79 |
|
| 80 |
std::unique_ptr<Base> getPointer() { |
| 81 |
return std::unique_ptr<Base>(new Base); |
| 82 |
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::make_unique instead |
| 83 |
// CHECK-FIXES: return std::make_unique<Base>(); |
| 84 |
} |
| 85 |
|
| 86 |
std::unique_ptr<Base> getPointerValue() { |
| 87 |
return std::unique_ptr<Base>(new Base()); |
| 88 |
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::make_unique instead |
| 89 |
// CHECK-FIXES: return std::make_unique<Base>(); |
| 90 |
} |
| 91 |
|
| 92 |
void 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 instead |
| 118 |
// 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 instead |
| 146 |
// 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 instead |
| 151 |
// 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 |
std::unique_ptr<int> S(new int); |
| 158 |
|
| 159 |
// Create the unique_ptr as a parameter to a function. |
| 160 |
int T = g(std::unique_ptr<int>(new int())); |
| 161 |
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead |
| 162 |
// CHECK-FIXES: int T = g(std::make_unique<int>()); |
| 163 |
T = g(std::unique_ptr<int>(new int)); |
| 164 |
|
| 165 |
// Only replace if the type in the template is the same as the type returned |
| 166 |
// by the new operator. |
| 167 |
auto Pderived = std::unique_ptr<Base>(new Derived()); |
| 168 |
auto PderivedNoparen = std::unique_ptr<Base>(new Derived); |
| 169 |
|
| 170 |
// OK to replace for reset and assign |
| 171 |
Pderived.reset(new Derived()); |
| 172 |
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use std::make_unique instead |
| 173 |
// CHECK-FIXES: Pderived = std::make_unique<Derived>(); |
| 174 |
PderivedNoparen.reset(new Derived); |
| 175 |
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use std::make_unique instead |
| 176 |
// CHECK-FIXES: PderivedNoparen = std::make_unique<Derived>(); |
| 177 |
|
| 178 |
Pderived = std::unique_ptr<Derived>(new Derived()); |
| 179 |
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use std::make_unique instead |
| 180 |
// CHECK-FIXES: Pderived = std::make_unique<Derived>(); |
| 181 |
PderivedNoparen = std::unique_ptr<Derived>(new Derived); |
| 182 |
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use std::make_unique instead |
| 183 |
// CHECK-FIXES: PderivedNoparen = std::make_unique<Derived>(); |
| 184 |
|
| 185 |
// FIXME: OK to replace if assigned to unique_ptr<Base> |
| 186 |
Pderived = std::unique_ptr<Base>(new Derived()); |
| 187 |
Pderived = std::unique_ptr<Base>(new Derived); |
| 188 |
|
| 189 |
// FIXME: OK to replace when auto is not used |
| 190 |
std::unique_ptr<Base> PBase = std::unique_ptr<Base>(new Derived()); |
| 191 |
std::unique_ptr<Base> PBaseNoparen = std::unique_ptr<Base>(new Derived); |
| 192 |
|
| 193 |
// The pointer is returned by the function, nothing to do. |
| 194 |
std::unique_ptr<Base> RetPtr = getPointer(); |
| 195 |
RetPtr = getPointerValue(); |
| 196 |
|
| 197 |
// This emulates std::move. |
| 198 |
std::unique_ptr<int> Move = static_cast<std::unique_ptr<int> &&>(P1); |
| 199 |
|
| 200 |
// Placement arguments should not be removed. |
| 201 |
int *PInt = new int; |
| 202 |
std::unique_ptr<int> Placement = std::unique_ptr<int>(new (PInt) int{3}); |
| 203 |
Placement.reset(new (PInt) int{3}); |
| 204 |
Placement = std::unique_ptr<int>(new (PInt) int{3}); |
| 205 |
|
| 206 |
std::unique_ptr<int> PlacementNoparen = std::unique_ptr<int>(new (PInt) int); |
| 207 |
PlacementNoparen.reset(new (PInt) int); |
| 208 |
PlacementNoparen = std::unique_ptr<int>(new (PInt) int); |
| 209 |
} |
| 210 |
|
| 211 |
// Calling make_smart_ptr from within a member function of a type with a |
| 212 |
// private or protected constructor would be ill-formed. |
| 213 |
class Private { |
| 214 |
private: |
| 215 |
Private(int z) {} |
| 216 |
|
| 217 |
public: |
| 218 |
Private() {} |
| 219 |
void create() { |
| 220 |
auto callsPublic = std::unique_ptr<Private>(new Private); |
| 221 |
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead |
| 222 |
// CHECK-FIXES: auto callsPublic = std::make_unique<Private>(); |
| 223 |
auto ptr = std::unique_ptr<Private>(new Private(42)); |
| 224 |
ptr.reset(new Private(42)); |
| 225 |
ptr = std::unique_ptr<Private>(new Private(42)); |
| 226 |
} |
| 227 |
|
| 228 |
virtual ~Private(); |
| 229 |
}; |
| 230 |
|
| 231 |
class Protected { |
| 232 |
protected: |
| 233 |
Protected() {} |
| 234 |
|
| 235 |
public: |
| 236 |
Protected(int, int) {} |
| 237 |
void create() { |
| 238 |
auto callsPublic = std::unique_ptr<Protected>(new Protected(1, 2)); |
| 239 |
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead |
| 240 |
// CHECK-FIXES: auto callsPublic = std::make_unique<Protected>(1, 2); |
| 241 |
auto ptr = std::unique_ptr<Protected>(new Protected); |
| 242 |
ptr.reset(new Protected); |
| 243 |
ptr = std::unique_ptr<Protected>(new Protected); |
| 244 |
} |
| 245 |
}; |
| 246 |
|
| 247 |
void initialization(int T, Base b) { |
| 248 |
// Test different kinds of initialization of the pointee. |
| 249 |
|
| 250 |
// Direct initialization with parenthesis. |
| 251 |
std::unique_ptr<DPair> PDir1 = std::unique_ptr<DPair>(new DPair(1, T)); |
| 252 |
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead |
| 253 |
// CHECK-FIXES: std::unique_ptr<DPair> PDir1 = std::make_unique<DPair>(1, T); |
| 254 |
PDir1.reset(new DPair(1, T)); |
| 255 |
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead |
| 256 |
// CHECK-FIXES: PDir1 = std::make_unique<DPair>(1, T); |
| 257 |
|
| 258 |
// Direct initialization with braces. |
| 259 |
std::unique_ptr<DPair> PDir2 = std::unique_ptr<DPair>(new DPair{2, T}); |
| 260 |
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead |
| 261 |
// CHECK-FIXES: std::unique_ptr<DPair> PDir2 = std::make_unique<DPair>(2, T); |
| 262 |
PDir2.reset(new DPair{2, T}); |
| 263 |
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead |
| 264 |
// CHECK-FIXES: PDir2 = std::make_unique<DPair>(2, T); |
| 265 |
|
| 266 |
// Aggregate initialization. |
| 267 |
std::unique_ptr<APair> PAggr = std::unique_ptr<APair>(new APair{T, 1}); |
| 268 |
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead |
| 269 |
// CHECK-FIXES: std::unique_ptr<APair> PAggr = std::make_unique<APair>(APair{T, 1}); |
| 270 |
PAggr.reset(new APair{T, 1}); |
| 271 |
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead |
| 272 |
// CHECK-FIXES: std::make_unique<APair>(APair{T, 1}); |
| 273 |
|
| 274 |
// Check aggregate init with intermediate temporaries. |
| 275 |
std::unique_ptr<APair> PAggrTemp = std::unique_ptr<APair>(new APair({T, 1})); |
| 276 |
// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: use std::make_unique instead |
| 277 |
// CHECK-FIXES: std::unique_ptr<APair> PAggrTemp = std::unique_ptr<APair>(new APair({T, 1})); |
| 278 |
PAggrTemp.reset(new APair({T, 1})); |
| 279 |
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead |
| 280 |
// CHECK-FIXES: PAggrTemp.reset(new APair({T, 1})); |
| 281 |
|
| 282 |
// Test different kinds of initialization of the pointee, when the unique_ptr |
| 283 |
// is initialized with braces. |
| 284 |
|
| 285 |
// Direct initialization with parenthesis. |
| 286 |
std::unique_ptr<DPair> PDir3 = std::unique_ptr<DPair>{new DPair(3, T)}; |
| 287 |
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead |
| 288 |
// CHECK-FIXES: std::unique_ptr<DPair> PDir3 = std::make_unique<DPair>(3, T); |
| 289 |
|
| 290 |
// Direct initialization with braces. |
| 291 |
std::unique_ptr<DPair> PDir4 = std::unique_ptr<DPair>{new DPair{4, T}}; |
| 292 |
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead |
| 293 |
// CHECK-FIXES: std::unique_ptr<DPair> PDir4 = std::make_unique<DPair>(4, T); |
| 294 |
|
| 295 |
// Aggregate initialization. |
| 296 |
std::unique_ptr<APair> PAggr2 = std::unique_ptr<APair>{new APair{T, 2}}; |
| 297 |
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: use std::make_unique instead |
| 298 |
// CHECK-FIXES: std::unique_ptr<APair> PAggr2 = std::make_unique<APair>(APair{T, 2}); |
| 299 |
|
| 300 |
// Direct initialization with parenthesis, without arguments. |
| 301 |
std::unique_ptr<DPair> PDir5 = std::unique_ptr<DPair>(new DPair()); |
| 302 |
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead |
| 303 |
// CHECK-FIXES: std::unique_ptr<DPair> PDir5 = std::make_unique<DPair>(); |
| 304 |
|
| 305 |
// Direct initialization with braces, without arguments. |
| 306 |
std::unique_ptr<DPair> PDir6 = std::unique_ptr<DPair>(new DPair{}); |
| 307 |
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use std::make_unique instead |
| 308 |
// CHECK-FIXES: std::unique_ptr<DPair> PDir6 = std::make_unique<DPair>(); |
| 309 |
|
| 310 |
// Aggregate initialization without arguments. |
| 311 |
std::unique_ptr<Empty> PEmpty = std::unique_ptr<Empty>(new Empty{}); |
| 312 |
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: use std::make_unique instead |
| 313 |
// CHECK-FIXES: std::unique_ptr<Empty> PEmpty = std::make_unique<Empty>(Empty{}); |
| 314 |
|
| 315 |
// Initialization with default constructor. |
| 316 |
std::unique_ptr<E> PE1 = std::unique_ptr<E>(new E{}); |
| 317 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 318 |
// CHECK-FIXES: std::unique_ptr<E> PE1 = std::make_unique<E>(); |
| 319 |
PE1.reset(new E{}); |
| 320 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead |
| 321 |
// CHECK-FIXES: PE1 = std::make_unique<E>(); |
| 322 |
|
| 323 |
// No warnings for `auto` new expression. |
| 324 |
PE1.reset(new auto(E())); |
| 325 |
|
| 326 |
//============================================================================ |
| 327 |
// NOTE: For initializer-list constructors, the check only gives warnings, |
| 328 |
// and no fixes are generated. |
| 329 |
//============================================================================ |
| 330 |
|
| 331 |
// Initialization with the initializer-list constructor. |
| 332 |
std::unique_ptr<E> PE2 = std::unique_ptr<E>(new E{1, 2}); |
| 333 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 334 |
// CHECK-FIXES: std::unique_ptr<E> PE2 = std::unique_ptr<E>(new E{1, 2}); |
| 335 |
PE2.reset(new E{1, 2}); |
| 336 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead |
| 337 |
// CHECK-FIXES: PE2.reset(new E{1, 2}); |
| 338 |
|
| 339 |
// Initialization with default constructor. |
| 340 |
std::unique_ptr<F> PF1 = std::unique_ptr<F>(new F()); |
| 341 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 342 |
// CHECK-FIXES: std::unique_ptr<F> PF1 = std::make_unique<F>(); |
| 343 |
PF1.reset(new F()); |
| 344 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead |
| 345 |
// CHECK-FIXES: PF1 = std::make_unique<F>(); |
| 346 |
|
| 347 |
// Initialization with default constructor. |
| 348 |
std::unique_ptr<F> PF2 = std::unique_ptr<F>(new F{}); |
| 349 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 350 |
// CHECK-FIXES: std::unique_ptr<F> PF2 = std::make_unique<F>(); |
| 351 |
PF2.reset(new F()); |
| 352 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead |
| 353 |
// CHECK-FIXES: PF2 = std::make_unique<F>(); |
| 354 |
|
| 355 |
// Initialization with the initializer-list constructor. |
| 356 |
std::unique_ptr<F> PF3 = std::unique_ptr<F>(new F{1}); |
| 357 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 358 |
// CHECK-FIXES: std::unique_ptr<F> PF3 = std::unique_ptr<F>(new F{1}); |
| 359 |
PF3.reset(new F{1}); |
| 360 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead |
| 361 |
// CHECK-FIXES: PF3.reset(new F{1}); |
| 362 |
|
| 363 |
// Initialization with the initializer-list constructor. |
| 364 |
std::unique_ptr<F> PF4 = std::unique_ptr<F>(new F{1, 2}); |
| 365 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 366 |
// CHECK-FIXES: std::unique_ptr<F> PF4 = std::unique_ptr<F>(new F{1, 2}); |
| 367 |
|
| 368 |
// Initialization with the initializer-list constructor. |
| 369 |
std::unique_ptr<F> PF5 = std::unique_ptr<F>(new F({1, 2})); |
| 370 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 371 |
// CHECK-FIXES: std::unique_ptr<F> PF5 = std::unique_ptr<F>(new F({1, 2})); |
| 372 |
|
| 373 |
// Initialization with the initializer-list constructor as the default |
| 374 |
// constructor is not present. |
| 375 |
std::unique_ptr<G> PG1 = std::unique_ptr<G>(new G{}); |
| 376 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 377 |
// CHECK-FIXES: std::unique_ptr<G> PG1 = std::unique_ptr<G>(new G{}); |
| 378 |
PG1.reset(new G{}); |
| 379 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead |
| 380 |
// CHECK-FIXES: PG1.reset(new G{}); |
| 381 |
|
| 382 |
// Initialization with the initializer-list constructor. |
| 383 |
std::unique_ptr<G> PG2 = std::unique_ptr<G>(new G{1}); |
| 384 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 385 |
// CHECK-FIXES: std::unique_ptr<G> PG2 = std::unique_ptr<G>(new G{1}); |
| 386 |
|
| 387 |
// Initialization with the initializer-list constructor. |
| 388 |
std::unique_ptr<G> PG3 = std::unique_ptr<G>(new G{1, 2}); |
| 389 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 390 |
// CHECK-FIXES: std::unique_ptr<G> PG3 = std::unique_ptr<G>(new G{1, 2}); |
| 391 |
|
| 392 |
std::unique_ptr<H> PH1 = std::unique_ptr<H>(new H({1, 2, 3})); |
| 393 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 394 |
// CHECK-FIXES: std::unique_ptr<H> PH1 = std::unique_ptr<H>(new H({1, 2, 3})); |
| 395 |
PH1.reset(new H({1, 2, 3})); |
| 396 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead |
| 397 |
// CHECK-FIXES: PH1.reset(new H({1, 2, 3})); |
| 398 |
|
| 399 |
std::unique_ptr<H> PH2 = std::unique_ptr<H>(new H({1, 2, 3}, 1)); |
| 400 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 401 |
// CHECK-FIXES: std::unique_ptr<H> PH2 = std::unique_ptr<H>(new H({1, 2, 3}, 1)); |
| 402 |
PH2.reset(new H({1, 2, 3}, 1)); |
| 403 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead |
| 404 |
// CHECK-FIXES: PH2.reset(new H({1, 2, 3}, 1)); |
| 405 |
|
| 406 |
std::unique_ptr<H> PH3 = std::unique_ptr<H>(new H({1, 2, 3}, 1.0)); |
| 407 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 408 |
// CHECK-FIXES: std::unique_ptr<H> PH3 = std::unique_ptr<H>(new H({1, 2, 3}, 1.0)); |
| 409 |
PH3.reset(new H({1, 2, 3}, 1.0)); |
| 410 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead |
| 411 |
// CHECK-FIXES: PH3.reset(new H({1, 2, 3}, 1.0)); |
| 412 |
|
| 413 |
std::unique_ptr<I> PI1 = std::unique_ptr<I>(new I(G({1, 2, 3}))); |
| 414 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 415 |
// CHECK-FIXES: std::unique_ptr<I> PI1 = std::make_unique<I>(G({1, 2, 3})); |
| 416 |
PI1.reset(new I(G({1, 2, 3}))); |
| 417 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead |
| 418 |
// CHECK-FIXES: PI1 = std::make_unique<I>(G({1, 2, 3})); |
| 419 |
|
| 420 |
std::unique_ptr<J> PJ1 = std::unique_ptr<J>(new J({1, 2}, 1)); |
| 421 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 422 |
// CHECK-FIXES: std::unique_ptr<J> PJ1 = std::unique_ptr<J>(new J({1, 2}, 1)); |
| 423 |
PJ1.reset(new J({1, 2}, 1)); |
| 424 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead |
| 425 |
// CHECK-FIXES: PJ1.reset(new J({1, 2}, 1)); |
| 426 |
|
| 427 |
std::unique_ptr<J> PJ2 = std::unique_ptr<J>(new J(E{1, 2}, 1)); |
| 428 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 429 |
// CHECK-FIXES: std::unique_ptr<J> PJ2 = std::unique_ptr<J>(new J(E{1, 2}, 1)); |
| 430 |
PJ2.reset(new J(E{1, 2}, 1)); |
| 431 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead |
| 432 |
// CHECK-FIXES: PJ2.reset(new J(E{1, 2}, 1)); |
| 433 |
|
| 434 |
std::unique_ptr<J> PJ3 = std::unique_ptr<J>(new J{ {1, 2}, 1 }); |
| 435 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 436 |
// CHECK-FIXES: std::unique_ptr<J> PJ3 = std::unique_ptr<J>(new J{ {1, 2}, 1 }); |
| 437 |
PJ3.reset(new J{ {1, 2}, 1 }); |
| 438 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead |
| 439 |
// CHECK-FIXES: PJ3.reset(new J{ {1, 2}, 1 }); |
| 440 |
|
| 441 |
std::unique_ptr<J> PJ4 = std::unique_ptr<J>(new J{E{1, 2}, 1}); |
| 442 |
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use std::make_unique instead |
| 443 |
// CHECK-FIXES: std::unique_ptr<J> PJ4 = std::unique_ptr<J>(new J{E{1, 2}, 1}); |
| 444 |
PJ4.reset(new J{E{1, 2}, 1}); |
| 445 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use std::make_unique instead |
| 446 |
// CHECK-FIXES: PJ4.reset(new J{E{1, 2}, 1}); |
| 447 |
|
| 448 |
std::unique_ptr<Foo> FF = std::unique_ptr<Foo>(new Foo()); |
| 449 |
// CHECK-MESSAGES: :[[@LINE-1]]:29: warning: |
| 450 |
// CHECK-FIXES: std::unique_ptr<Foo> FF = std::make_unique<Foo>(); |
| 451 |
FF.reset(new Foo()); |
| 452 |
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: |
| 453 |
// CHECK-FIXES: FF = std::make_unique<Foo>(); |
| 454 |
|
| 455 |
std::unique_ptr<bar::Bar> BB = std::unique_ptr<bar::Bar>(new bar::Bar()); |
| 456 |
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: |
| 457 |
// CHECK-FIXES: std::unique_ptr<bar::Bar> BB = std::make_unique<bar::Bar>(); |
| 458 |
BB.reset(new bar::Bar()); |
| 459 |
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: |
| 460 |
// CHECK-FIXES: BB = std::make_unique<bar::Bar>(); |
| 461 |
|
| 462 |
std::unique_ptr<Foo[]> FFs; |
| 463 |
FFs.reset(new Foo[5]); |
| 464 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: |
| 465 |
// CHECK-FIXES: FFs = std::make_unique<Foo[]>(5); |
| 466 |
FFs.reset(new Foo[5]()); |
| 467 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: |
| 468 |
// CHECK-FIXES: FFs = std::make_unique<Foo[]>(5); |
| 469 |
const int Num = 1; |
| 470 |
FFs.reset(new Foo[Num]); |
| 471 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: |
| 472 |
// CHECK-FIXES: FFs = std::make_unique<Foo[]>(Num); |
| 473 |
int Num2 = 1; |
| 474 |
FFs.reset(new Foo[Num2]); |
| 475 |
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: |
| 476 |
// CHECK-FIXES: FFs = std::make_unique<Foo[]>(Num2); |
| 477 |
|
| 478 |
std::unique_ptr<int[]> FI; |
| 479 |
FI.reset(new int[5]()); // value initialization. |
| 480 |
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: |
| 481 |
// CHECK-FIXES: FI = std::make_unique<int[]>(5); |
| 482 |
|
| 483 |
// The check doesn't give warnings and fixes for cases where the original new |
| 484 |
// expression does default initialization. |
| 485 |
FI.reset(new int[5]); |
| 486 |
FI.reset(new int[Num]); |
| 487 |
FI.reset(new int[Num2]); |
| 488 |
} |
| 489 |
|
| 490 |
void aliases() { |
| 491 |
typedef std::unique_ptr<int> IntPtr; |
| 492 |
IntPtr Typedef = IntPtr(new int()); |
| 493 |
// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use std::make_unique instead |
| 494 |
// CHECK-FIXES: IntPtr Typedef = std::make_unique<int>(); |
| 495 |
IntPtr Typedef2 = IntPtr(new int); |
| 496 |
|
| 497 |
// We use 'bool' instead of '_Bool'. |
| 498 |
typedef std::unique_ptr<bool> BoolPtr; |
| 499 |
BoolPtr BoolType = BoolPtr(new bool()); |
| 500 |
// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use std::make_unique instead |
| 501 |
// CHECK-FIXES: BoolPtr BoolType = std::make_unique<bool>(); |
| 502 |
BoolPtr BoolType2 = BoolPtr(new bool); |
| 503 |
|
| 504 |
// We use 'Base' instead of 'struct Base'. |
| 505 |
typedef std::unique_ptr<Base> BasePtr; |
| 506 |
BasePtr StructType = BasePtr(new Base); |
| 507 |
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead |
| 508 |
// CHECK-FIXES: BasePtr StructType = std::make_unique<Base>(); |
| 509 |
|
| 510 |
#define PTR unique_ptr<int> |
| 511 |
std::unique_ptr<int> Macro = std::PTR(new int()); |
| 512 |
// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead |
| 513 |
// CHECK-FIXES: std::unique_ptr<int> Macro = std::make_unique<int>(); |
| 514 |
std::unique_ptr<int> Macro2 = std::PTR(new int); |
| 515 |
#undef PTR |
| 516 |
|
| 517 |
std::unique_ptr<int> Using = unique_ptr_<int>(new int()); |
| 518 |
// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_unique instead |
| 519 |
// CHECK-FIXES: std::unique_ptr<int> Using = std::make_unique<int>(); |
| 520 |
std::unique_ptr<int> Using2 = unique_ptr_<int>(new int); |
| 521 |
} |
| 522 |
|
| 523 |
void whitespaces() { |
| 524 |
// clang-format off |
| 525 |
auto Space = std::unique_ptr <int>(new int()); |
| 526 |
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use std::make_unique instead |
| 527 |
// CHECK-FIXES: auto Space = std::make_unique<int>(); |
| 528 |
auto Space2 = std::unique_ptr <int>(new int); |
| 529 |
|
| 530 |
auto Spaces = std :: unique_ptr <int>(new int()); |
| 531 |
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use std::make_unique instead |
| 532 |
// CHECK-FIXES: auto Spaces = std::make_unique<int>(); |
| 533 |
auto Spaces2 = std :: unique_ptr <int>(new int); |
| 534 |
// clang-format on |
| 535 |
} |
| 536 |
|
| 537 |
void nesting() { |
| 538 |
auto Nest = std::unique_ptr<std::unique_ptr<int>>(new std::unique_ptr<int>(new int)); |
| 539 |
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use std::make_unique instead |
| 540 |
// CHECK-FIXES: auto Nest = std::make_unique<std::unique_ptr<int>>(new int); |
| 541 |
Nest.reset(new std::unique_ptr<int>(new int)); |
| 542 |
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead |
| 543 |
// CHECK-FIXES: Nest = std::make_unique<std::unique_ptr<int>>(new int); |
| 544 |
Nest->reset(new int()); |
| 545 |
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead |
| 546 |
// CHECK-FIXES: *Nest = std::make_unique<int>(); |
| 547 |
} |
| 548 |
|
| 549 |
void reset() { |
| 550 |
std::unique_ptr<int> P; |
| 551 |
P.reset(); |
| 552 |
P.reset(nullptr); |
| 553 |
P.reset(new int()); |
| 554 |
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use std::make_unique instead |
| 555 |
// CHECK-FIXES: P = std::make_unique<int>(); |
| 556 |
P.reset(new int); |
| 557 |
|
| 558 |
auto Q = &P; |
| 559 |
Q->reset(new int()); |
| 560 |
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead |
| 561 |
// CHECK-FIXES: *Q = std::make_unique<int>(); |
| 562 |
Q->reset(new int); |
| 563 |
} |
| 564 |
|
| 565 |
#define DEFINE(...) __VA_ARGS__ |
| 566 |
template<typename T> |
| 567 |
void g2(std::unique_ptr<Foo> *t) { |
| 568 |
DEFINE(auto p = std::unique_ptr<Foo>(new Foo); t->reset(new Foo);); |
| 569 |
} |
| 570 |
void macro() { |
| 571 |
std::unique_ptr<Foo> *t; |
| 572 |
g2<bar::Bar>(t); |
| 573 |
} |
| 574 |
#undef DEFINE |
| 575 |
|
| 576 |
class UniqueFoo : public std::unique_ptr<Foo> { |
| 577 |
public: |
| 578 |
void foo() { |
| 579 |
reset(new Foo); |
| 580 |
this->reset(new Foo); |
| 581 |
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use std::make_unique instead |
| 582 |
// CHECK-FIXES: *this = std::make_unique<Foo>(); |
| 583 |
this->reset(new Foo()); |
| 584 |
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use std::make_unique instead |
| 585 |
// CHECK-FIXES: *this = std::make_unique<Foo>(); |
| 586 |
(*this).reset(new Foo); |
| 587 |
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead |
| 588 |
// CHECK-FIXES: (*this) = std::make_unique<Foo>(); |
| 589 |
(*this).reset(new Foo()); |
| 590 |
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead |
| 591 |
// CHECK-FIXES: (*this) = std::make_unique<Foo>(); |
| 592 |
} |
| 593 |
}; |
| 594 |
|
| 595 |
// Ignore statements inside a template instantiation. |
| 596 |
template<typename T> |
| 597 |
void template_fun(T* t) { |
| 598 |
std::unique_ptr<T> t2 = std::unique_ptr<T>(new T); |
| 599 |
std::unique_ptr<T> t3 = std::unique_ptr<T>(new T()); |
| 600 |
t2.reset(new T); |
| 601 |
t3.reset(new T()); |
| 602 |
} |
| 603 |
|
| 604 |
void invoke_template() { |
| 605 |
Foo* foo; |
| 606 |
template_fun(foo); |
| 607 |
} |
| 608 |
|
| 609 |
void fix_for_c_style_struct() { |
| 610 |
auto T = std::unique_ptr<Base>(new struct Base); |
| 611 |
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use std::make_unique instead |
| 612 |
// CHECK-FIXES: auto T = std::make_unique<Base>(); |
| 613 |
} |