Index: clang-tidy/modernize/MakeUniqueCheck.cpp =================================================================== --- clang-tidy/modernize/MakeUniqueCheck.cpp +++ clang-tidy/modernize/MakeUniqueCheck.cpp @@ -54,8 +54,11 @@ SourceManager &SM = *Result.SourceManager; const auto *Construct = Result.Nodes.getNodeAs(ConstructorCall); - const auto *New = Result.Nodes.getNodeAs(NewExpression); const auto *Type = Result.Nodes.getNodeAs(PointerType); + const auto *New = Result.Nodes.getNodeAs(NewExpression); + + if (New->getNumPlacementArgs() != 0) + return; SourceLocation ConstructCallStart = Construct->getExprLoc(); @@ -86,6 +89,20 @@ CharSourceRange::getCharRange(ConstructCallStart, ConstructCallEnd), "std::make_unique"); + // If the unique_ptr is built with brace enclosed direct initialization, use + // parenthesis instead. + if (Construct->isListInitialization()) { + SourceRange BraceRange = Construct->getParenOrBraceRange(); + Diag << FixItHint::CreateReplacement( + CharSourceRange::getCharRange( + BraceRange.getBegin(), BraceRange.getBegin().getLocWithOffset(1)), + "("); + Diag << FixItHint::CreateReplacement( + CharSourceRange::getCharRange(BraceRange.getEnd(), + BraceRange.getEnd().getLocWithOffset(1)), + ")"); + } + SourceLocation NewStart = New->getSourceRange().getBegin(); SourceLocation NewEnd = New->getSourceRange().getEnd(); switch (New->getInitializationStyle()) { @@ -101,9 +118,30 @@ break; } case CXXNewExpr::ListInit: { - SourceRange InitRange = New->getInitializer()->getSourceRange(); + // Range of the substring that we do not want to remove. + SourceRange InitRange; + if (const auto *NewConstruct = New->getConstructExpr()) { + // Direct initialization with initialization list. + // struct S { S(int x) {} }; + // std::unique_ptr(new S{5}); + // The arguments in the initialization list are going to be forwarded to + // the constructor, so this has to be replaced with: + // struct S { S(int x) {} }; + // std::make_unique(5); + InitRange = SourceRange( + NewConstruct->getParenOrBraceRange().getBegin().getLocWithOffset(1), + NewConstruct->getParenOrBraceRange().getEnd().getLocWithOffset(-1)); + } else { + // Aggregate initialization. + // std::unique_ptr(new Pair{first, second}); + // Has to be replaced with: + // std::make_unique(Pair{first, second}); + InitRange = SourceRange( + New->getAllocatedTypeSourceInfo()->getTypeLoc().getLocStart(), + New->getInitializer()->getSourceRange().getEnd()); + } Diag << FixItHint::CreateRemoval( - SourceRange(NewStart, InitRange.getBegin().getLocWithOffset(-1))); + CharSourceRange::getCharRange(NewStart, InitRange.getBegin())); Diag << FixItHint::CreateRemoval( SourceRange(InitRange.getEnd().getLocWithOffset(1), NewEnd)); break; Index: test/clang-tidy/modernize-make-unique.cpp =================================================================== --- test/clang-tidy/modernize-make-unique.cpp +++ test/clang-tidy/modernize-make-unique.cpp @@ -34,12 +34,22 @@ Derived(int, int); }; -struct Pair { +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(unsigned long Count, void *Ptr); + int g(std::unique_ptr P); std::unique_ptr getPointer() { @@ -48,7 +58,7 @@ // CHECK-FIXES: return std::make_unique(); } -void f() { +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(); @@ -78,16 +88,6 @@ // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead // CHECK-FIXES: int T = g(std::make_unique()); - // Arguments are correctly handled. - std::unique_ptr Pbase = std::unique_ptr(new Base(5, T)); - // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: use std::make_unique instead - // CHECK-FIXES: std::unique_ptr Pbase = std::make_unique(5, T); - - // Works with init lists. - std::unique_ptr Ppair = std::unique_ptr(new Pair{T, 1}); - // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: use std::make_unique instead - // CHECK-FIXES: std::unique_ptr Ppair = std::make_unique({T, 1}); - // 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()); @@ -95,7 +95,69 @@ // The pointer is returned by the function, nothing to do. std::unique_ptr RetPtr = getPointer(); - // Aliases. + // 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 @@ -110,11 +172,9 @@ 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(); +} - // This emulates std::move. - std::unique_ptr Move = static_cast&&>(P1); - - // Adding whitespaces. +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();