diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h --- a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h +++ b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h @@ -50,6 +50,7 @@ const std::string MakeSmartPtrFunctionHeader; const std::string MakeSmartPtrFunctionName; const bool IgnoreMacros; + const bool IgnoreDefaultInitialization; void checkConstruct(SourceManager &SM, ASTContext *Ctx, const CXXConstructExpr *Construct, const QualType *Type, diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp --- a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "../utils/TypeTraits.h" #include "MakeSharedCheck.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Lex/Lexer.h" @@ -49,13 +50,17 @@ Options.get("MakeSmartPtrFunctionHeader", "")), MakeSmartPtrFunctionName( Options.get("MakeSmartPtrFunction", MakeSmartPtrFunctionName)), - IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {} + IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)), + IgnoreDefaultInitialization( + Options.get("IgnoreDefaultInitialization", true)) {} void MakeSmartPtrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "IncludeStyle", Inserter.getStyle()); Options.store(Opts, "MakeSmartPtrFunctionHeader", MakeSmartPtrFunctionHeader); Options.store(Opts, "MakeSmartPtrFunction", MakeSmartPtrFunctionName); Options.store(Opts, "IgnoreMacros", IgnoreMacros); + Options.store(Opts, "IgnoreDefaultInitialization", + IgnoreDefaultInitialization); } bool MakeSmartPtrCheck::isLanguageVersionSupported( @@ -120,14 +125,18 @@ if (New->getType()->getPointeeType()->getContainedAutoType()) return; - // Be conservative for cases where we construct an array without any - // initialization. + // Be conservative for cases where we construct and default initialize. + // // For example, + // P.reset(new int) // check fix: P = std::make_unique() // P.reset(new int[5]) // check fix: P = std::make_unique(5) // - // The fix of the check has side effect, it introduces default initialization + // The fix of the check has side effect, it introduces value initialization // which maybe unexpected and cause performance regression. - if (New->isArray() && !New->hasInitializer()) + bool Initializes = New->hasInitializer() || + !utils::type_traits::isTriviallyDefaultConstructible( + New->getAllocatedType(), *Result.Context); + if (!Initializes && IgnoreDefaultInitialization) return; if (Construct) checkConstruct(SM, Result.Context, Construct, Type, New); diff --git a/clang-tools-extra/docs/clang-tidy/checks/modernize-make-shared.rst b/clang-tools-extra/docs/clang-tidy/checks/modernize-make-shared.rst --- a/clang-tools-extra/docs/clang-tidy/checks/modernize-make-shared.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/modernize-make-shared.rst @@ -48,3 +48,9 @@ If set to `true`, the check will not give warnings inside macros. Default is `true`. + +.. option:: IgnoreDefaultInitialization + + If set to non-zero, the check does not suggest edits that will transform + default initialization into value initialization, as this can cause + performance regressions. Default is `1`. diff --git a/clang-tools-extra/docs/clang-tidy/checks/modernize-make-unique.rst b/clang-tools-extra/docs/clang-tidy/checks/modernize-make-unique.rst --- a/clang-tools-extra/docs/clang-tidy/checks/modernize-make-unique.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/modernize-make-unique.rst @@ -48,3 +48,9 @@ If set to `true`, the check will not give warnings inside macros. Default is `true`. + +.. option:: IgnoreDefaultInitialization + + If set to non-zero, the check does not suggest edits that will transform + default initialization into value initialization, as this can cause + performance regressions. Default is `1`. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize-make-shared.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize-make-shared.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/modernize-make-shared.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize-make-shared.cpp @@ -38,48 +38,61 @@ // CHECK-FIXES: return std::make_shared(); } +std::shared_ptr getPointerValue() { + return std::shared_ptr(new Base()); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use std::make_shared instead + // CHECK-FIXES: return std::make_shared(); +} + void basic() { std::shared_ptr P1 = std::shared_ptr(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_shared instead [modernize-make-shared] // CHECK-FIXES: std::shared_ptr P1 = std::make_shared(); + std::shared_ptr P2 = std::shared_ptr(new int); P1.reset(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_shared instead [modernize-make-shared] // CHECK-FIXES: P1 = std::make_shared(); + P1.reset(new int); P1 = std::shared_ptr(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_shared instead [modernize-make-shared] // CHECK-FIXES: P1 = std::make_shared(); + P1 = std::shared_ptr(new int); - // Without parenthesis. - std::shared_ptr P2 = std::shared_ptr(new int); - // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_shared instead [modernize-make-shared] - // CHECK-FIXES: std::shared_ptr P2 = std::make_shared(); + // Without parenthesis, default initialization. + std::shared_ptr P3 = std::shared_ptr(new int); - P2.reset(new int); + P2.reset(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_shared instead [modernize-make-shared] // CHECK-FIXES: P2 = std::make_shared(); + P2.reset(new int); - P2 = std::shared_ptr(new int); + P2 = std::shared_ptr(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_shared instead [modernize-make-shared] // CHECK-FIXES: P2 = std::make_shared(); + P2 = std::shared_ptr(new int); // With auto. - auto P3 = std::shared_ptr(new int()); + auto P4 = std::shared_ptr(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_shared instead - // CHECK-FIXES: auto P3 = std::make_shared(); + // CHECK-FIXES: auto P4 = std::make_shared(); + auto P5 = std::shared_ptr(new int); - std::shared_ptr P4 = std::shared_ptr((new int)); + std::shared_ptr P6 = std::shared_ptr((new int())); // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_shared instead [modernize-make-shared] - // CHECK-FIXES: std::shared_ptr P4 = std::make_shared(); + // CHECK-FIXES: std::shared_ptr P6 = std::make_shared(); + std::shared_ptr P7 = std::shared_ptr((new int)); P4.reset((((new int())))); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_shared instead [modernize-make-shared] // CHECK-FIXES: P4 = std::make_shared(); + P4.reset((((new int)))); - P4 = std::shared_ptr(((new int))); + P4 = std::shared_ptr(((new int()))); // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_shared instead [modernize-make-shared] // CHECK-FIXES: P4 = std::make_shared(); + P4 = std::shared_ptr(((new int))); { // No std. @@ -87,40 +100,54 @@ shared_ptr Q = shared_ptr(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use std::make_shared instead // CHECK-FIXES: shared_ptr Q = std::make_shared(); + shared_ptr P = shared_ptr(new int); Q = shared_ptr(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_shared instead // CHECK-FIXES: Q = std::make_shared(); + Q = shared_ptr(new int); } std::shared_ptr R(new int()); + std::shared_ptr S(new int); // Create the shared_ptr as a parameter to a function. int T = g(std::shared_ptr(new int())); // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_shared instead // CHECK-FIXES: int T = g(std::make_shared()); + T = g(std::shared_ptr(new int)); // Only replace if the type in the template is the same as the type returned // by the new operator. auto Pderived = std::shared_ptr(new Derived()); + auto PderivedNoparen = std::shared_ptr(new Derived); // OK to replace for reset and assign Pderived.reset(new Derived()); // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use std::make_shared instead // CHECK-FIXES: Pderived = std::make_shared(); + Pderived.reset(new Derived); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use std::make_shared instead + // CHECK-FIXES: Pderived = std::make_shared(); Pderived = std::shared_ptr(new Derived()); // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use std::make_shared instead // CHECK-FIXES: Pderived = std::make_shared(); + Pderived = std::shared_ptr(new Derived); + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use std::make_shared instead + // CHECK-FIXES: Pderived = std::make_shared(); // FIXME: OK to replace if assigned to shared_ptr Pderived = std::shared_ptr(new Derived()); + Pderived = std::shared_ptr(new Derived); // FIXME: OK to replace when auto is not used std::shared_ptr PBase = std::shared_ptr(new Derived()); + std::shared_ptr PBase2 = std::shared_ptr(new Derived); // The pointer is returned by the function, nothing to do. std::shared_ptr RetPtr = getPointer(); + std::shared_ptr RetPtr2 = getPointerValue(); // This emulates std::move. std::shared_ptr Move = static_cast &&>(P1); @@ -130,6 +157,10 @@ std::shared_ptr Placement = std::shared_ptr(new (PInt) int{3}); Placement.reset(new (PInt) int{3}); Placement = std::shared_ptr(new (PInt) int{3}); + + std::shared_ptr PlacementNoparen = std::shared_ptr(new (PInt) int); + PlacementNoparen.reset(new (PInt) int); + PlacementNoparen = std::shared_ptr(new (PInt) int); } // Calling make_smart_ptr from within a member function of a type with a @@ -231,15 +262,17 @@ void aliases() { typedef std::shared_ptr IntPtr; - IntPtr Typedef = IntPtr(new int); + IntPtr Typedef = IntPtr(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use std::make_shared instead // CHECK-FIXES: IntPtr Typedef = std::make_shared(); + IntPtr Typedef2 = IntPtr(new int); // We use 'bool' instead of '_Bool'. typedef std::shared_ptr BoolPtr; - BoolPtr BoolType = BoolPtr(new bool); + BoolPtr BoolType = BoolPtr(new bool()); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use std::make_shared instead // CHECK-FIXES: BoolPtr BoolType = std::make_shared(); + BoolPtr BoolType2 = BoolPtr(new bool); // We use 'Base' instead of 'struct Base'. typedef std::shared_ptr BasePtr; @@ -248,14 +281,16 @@ // CHECK-FIXES: BasePtr StructType = std::make_shared(); #define PTR shared_ptr - std::shared_ptr Macro = std::PTR(new int); -// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_shared instead -// CHECK-FIXES: std::shared_ptr Macro = std::make_shared(); + std::shared_ptr Macro = std::PTR(new int()); + // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_shared instead + // CHECK-FIXES: std::shared_ptr Macro = std::make_shared(); + std::shared_ptr Macro2 = std::PTR(new int); #undef PTR - std::shared_ptr Using = shared_ptr_(new int); + std::shared_ptr Using = shared_ptr_(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: use std::make_shared instead // CHECK-FIXES: std::shared_ptr Using = std::make_shared(); + std::shared_ptr Using2 = shared_ptr_(new int); } void whitespaces() { @@ -263,10 +298,12 @@ auto Space = std::shared_ptr (new int()); // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use std::make_shared instead // CHECK-FIXES: auto Space = std::make_shared(); + auto Space2 = std::shared_ptr (new int); auto Spaces = std :: shared_ptr (new int()); // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use std::make_shared instead // CHECK-FIXES: auto Spaces = std::make_shared(); + auto Spaces2 = std :: shared_ptr (new int); // clang-format on } @@ -277,9 +314,10 @@ Nest.reset(new std::shared_ptr(new int)); // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_shared instead // CHECK-FIXES: Nest = std::make_shared>(new int); - Nest->reset(new int); + Nest->reset(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_shared instead // CHECK-FIXES: *Nest = std::make_shared(); + Nest->reset(new int); } void reset() { @@ -289,9 +327,11 @@ P.reset(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use std::make_shared instead // CHECK-FIXES: P = std::make_shared(); + P.reset(new int); auto Q = &P; Q->reset(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_shared instead // CHECK-FIXES: *Q = std::make_shared(); + Q->reset(new int); } diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique-default-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique-default-init.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique-default-init.cpp @@ -0,0 +1,73 @@ +// RUN: %check_clang_tidy -std=c++14-or-later %s modernize-make-unique %t -- \ +// RUN: -config="{CheckOptions: \ +// RUN: [{key: modernize-make-unique.IgnoreDefaultInitialization, \ +// RUN: value: 'false'}] \ +// RUN: }" \ +// RUN: -- -I %S/Inputs/modernize-smart-ptr + +#include "initializer_list.h" +#include "unique_ptr.h" +// CHECK-FIXES: #include + +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(); + 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(); + + P1.reset(new int()); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique] + // CHECK-FIXES: P1 = std::make_unique(); + P2.reset(new int); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique] + // CHECK-FIXES: P2 = std::make_unique(); + + P1 = std::unique_ptr(new int()); + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique] + // CHECK-FIXES: P1 = std::make_unique(); + P2 = std::unique_ptr(new int); + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique] + // CHECK-FIXES: 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(); + auto P4 = std::unique_ptr(new int); + // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead + // CHECK-FIXES: auto P4 = std::make_unique(); + + std::unique_ptr P5 = std::unique_ptr((new int())); + // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique] + // CHECK-FIXES: std::unique_ptr P5 = std::make_unique(); + std::unique_ptr P6 = std::unique_ptr((new int)); + // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique] + // CHECK-FIXES: std::unique_ptr P6 = std::make_unique(); + + P5.reset((new int())); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique] + // CHECK-FIXES: P5 = std::make_unique(); + P6.reset((new int)); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique] + // CHECK-FIXES: P6 = std::make_unique(); + + std::unique_ptr P7, P8; + P7.reset(new int[5]()); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique] + // CHECK-FIXES: P7 = std::make_unique(5); + + P8.reset(new int[5]); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique] + // CHECK-FIXES: P8 = std::make_unique(5); + + int Num = 3; + P7.reset(new int[Num]); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique] + // CHECK-FIXES: P7 = std::make_unique(Num); + + P8.reset(new int[Num]); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique] + // CHECK-FIXES: P8 = std::make_unique(Num); +} diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize-make-unique.cpp @@ -83,49 +83,60 @@ // CHECK-FIXES: return std::make_unique(); } +std::unique_ptr getPointerValue() { + 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(); + std::unique_ptr P2 = std::unique_ptr(new int); P1.reset(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique] // CHECK-FIXES: P1 = std::make_unique(); + P2.reset(new int); P1 = std::unique_ptr(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique] // CHECK-FIXES: P1 = std::make_unique(); + P1 = std::unique_ptr(new int); - // 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(); + // Without parenthesis, default initialization. + std::unique_ptr P3 = std::unique_ptr(new int); P2.reset(new int); - // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique] - // CHECK-FIXES: P2 = std::make_unique(); P2 = std::unique_ptr(new int); - // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead [modernize-make-unique] - // CHECK-FIXES: P2 = std::make_unique(); // With auto. - auto P3 = std::unique_ptr(new int()); + auto P4 = std::unique_ptr(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead - // CHECK-FIXES: auto P3 = std::make_unique(); + // CHECK-FIXES: auto P4 = std::make_unique(); + auto P5 = std::unique_ptr(new int); - std::unique_ptr P4 = std::unique_ptr((new int)); + std::unique_ptr P6 = std::unique_ptr((new int())); // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique] - // CHECK-FIXES: std::unique_ptr P4 = std::make_unique(); - P4.reset((new int)); + // CHECK-FIXES: std::unique_ptr P6 = std::make_unique(); + std::unique_ptr P7 = std::unique_ptr((new int)); + + P4.reset((new int())); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique] // CHECK-FIXES: P4 = std::make_unique(); - std::unique_ptr P5 = std::unique_ptr((((new int)))); + P5.reset((new int)); + + std::unique_ptr P8 = std::unique_ptr((((new int())))); // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use std::make_unique instead [modernize-make-unique] - // CHECK-FIXES: std::unique_ptr P5 = std::make_unique(); - P5.reset(((((new int))))); + // CHECK-FIXES: std::unique_ptr P8 = std::make_unique(); + std::unique_ptr P9 = std::unique_ptr((((new int)))); + + P5.reset(((((new int()))))); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead [modernize-make-unique] // CHECK-FIXES: P5 = std::make_unique(); + P6.reset(((((new int))))); { // No std. @@ -133,40 +144,55 @@ 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(); + unique_ptr P = unique_ptr(new int); Q = unique_ptr(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead // CHECK-FIXES: Q = std::make_unique(); + + P = unique_ptr(new int); } std::unique_ptr R(new int()); + std::unique_ptr S(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()); + T = g(std::unique_ptr(new int)); // Only replace if the type in the template is the same as the type returned // by the new operator. auto Pderived = std::unique_ptr(new Derived()); + auto PderivedNoparen = std::unique_ptr(new Derived); // OK to replace for reset and assign Pderived.reset(new Derived()); // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use std::make_unique instead // CHECK-FIXES: Pderived = std::make_unique(); + PderivedNoparen.reset(new Derived); + // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use std::make_unique instead + // CHECK-FIXES: PderivedNoparen = std::make_unique(); Pderived = std::unique_ptr(new Derived()); // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use std::make_unique instead // CHECK-FIXES: Pderived = std::make_unique(); + PderivedNoparen = std::unique_ptr(new Derived); + // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use std::make_unique instead + // CHECK-FIXES: PderivedNoparen = std::make_unique(); // FIXME: OK to replace if assigned to unique_ptr Pderived = std::unique_ptr(new Derived()); + Pderived = std::unique_ptr(new Derived); // FIXME: OK to replace when auto is not used std::unique_ptr PBase = std::unique_ptr(new Derived()); + std::unique_ptr PBaseNoparen = std::unique_ptr(new Derived); // The pointer is returned by the function, nothing to do. std::unique_ptr RetPtr = getPointer(); + RetPtr = getPointerValue(); // This emulates std::move. std::unique_ptr Move = static_cast &&>(P1); @@ -176,6 +202,10 @@ std::unique_ptr Placement = std::unique_ptr(new (PInt) int{3}); Placement.reset(new (PInt) int{3}); Placement = std::unique_ptr(new (PInt) int{3}); + + std::unique_ptr PlacementNoparen = std::unique_ptr(new (PInt) int); + PlacementNoparen.reset(new (PInt) int); + PlacementNoparen = std::unique_ptr(new (PInt) int); } // Calling make_smart_ptr from within a member function of a type with a @@ -446,12 +476,12 @@ // CHECK-FIXES: FFs = std::make_unique(Num2); std::unique_ptr FI; - FI.reset(new int[5]()); // default initialization. + FI.reset(new int[5]()); // value initialization. // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: // CHECK-FIXES: FI = std::make_unique(5); // The check doesn't give warnings and fixes for cases where the original new - // expression doesn't do any initialization. + // expression does default initialization. FI.reset(new int[5]); FI.reset(new int[Num]); FI.reset(new int[Num2]); @@ -459,15 +489,17 @@ void aliases() { typedef std::unique_ptr IntPtr; - IntPtr Typedef = IntPtr(new int); + IntPtr Typedef = IntPtr(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use std::make_unique instead // CHECK-FIXES: IntPtr Typedef = std::make_unique(); + IntPtr Typedef2 = IntPtr(new int); // We use 'bool' instead of '_Bool'. typedef std::unique_ptr BoolPtr; - BoolPtr BoolType = BoolPtr(new bool); + BoolPtr BoolType = BoolPtr(new bool()); // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: use std::make_unique instead // CHECK-FIXES: BoolPtr BoolType = std::make_unique(); + BoolPtr BoolType2 = BoolPtr(new bool); // We use 'Base' instead of 'struct Base'. typedef std::unique_ptr BasePtr; @@ -476,14 +508,16 @@ // 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(); + 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(); + std::unique_ptr Macro2 = std::PTR(new int); #undef PTR - std::unique_ptr Using = unique_ptr_(new int); + 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(); + std::unique_ptr Using2 = unique_ptr_(new int); } void whitespaces() { @@ -491,10 +525,12 @@ 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 Space2 = std::unique_ptr (new int); 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(); + auto Spaces2 = std :: unique_ptr (new int); // clang-format on } @@ -505,7 +541,7 @@ Nest.reset(new std::unique_ptr(new int)); // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use std::make_unique instead // CHECK-FIXES: Nest = std::make_unique>(new int); - Nest->reset(new int); + Nest->reset(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use std::make_unique instead // CHECK-FIXES: *Nest = std::make_unique(); } @@ -517,11 +553,13 @@ P.reset(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: use std::make_unique instead // CHECK-FIXES: P = std::make_unique(); + P.reset(new int); auto Q = &P; Q->reset(new int()); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use std::make_unique instead // CHECK-FIXES: *Q = std::make_unique(); + Q->reset(new int); } #define DEFINE(...) __VA_ARGS__ @@ -542,9 +580,15 @@ this->reset(new Foo); // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use std::make_unique instead // CHECK-FIXES: *this = std::make_unique(); + this->reset(new Foo()); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use std::make_unique instead + // CHECK-FIXES: *this = std::make_unique(); (*this).reset(new Foo); // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead // CHECK-FIXES: (*this) = std::make_unique(); + (*this).reset(new Foo()); + // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use std::make_unique instead + // CHECK-FIXES: (*this) = std::make_unique(); } }; @@ -552,7 +596,9 @@ template void template_fun(T* t) { std::unique_ptr t2 = std::unique_ptr(new T); + std::unique_ptr t3 = std::unique_ptr(new T()); t2.reset(new T); + t3.reset(new T()); } void invoke_template() {