Index: clang-tidy/modernize/MakeSmartPtrCheck.cpp =================================================================== --- clang-tidy/modernize/MakeSmartPtrCheck.cpp +++ clang-tidy/modernize/MakeSmartPtrCheck.cpp @@ -29,13 +29,19 @@ if (!getLangOpts().CPlusPlus11) return; + // It is possible to make smart ptr calling private ctor inside of a member + // function. Change to make_smart_ptr will be invalid. + auto CallsPrivateCtor = has( + ignoringImpCasts(cxxConstructExpr(hasDeclaration(decl(isPrivate()))))); + Finder->addMatcher( cxxBindTemporaryExpr(has(ignoringParenImpCasts( cxxConstructExpr( hasType(getSmartPointerTypeMatcher()), argumentCountIs(1), hasArgument(0, cxxNewExpr(hasType(pointsTo(qualType(hasCanonicalType( - equalsBoundNode(PointerType)))))) + equalsBoundNode(PointerType))))), + unless(CallsPrivateCtor)) .bind(NewExpression))) .bind(ConstructorCall)))), this); Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -75,6 +75,13 @@ This check warns about the performance overhead arising from concatenating strings using the ``operator+``, instead of ``operator+=``. +- Bugfix for `modernize-make-unique + `_ + and `modernize-make-shared + `_ + checks + It is invalid to use ``make_{smart_ptr}`` when the called constructor is + private. Improvements to include-fixer ----------------------------- Index: test/clang-tidy/modernize-make-shared.cpp =================================================================== --- test/clang-tidy/modernize-make-shared.cpp +++ test/clang-tidy/modernize-make-shared.cpp @@ -100,6 +100,18 @@ std::shared_ptr Placement = std::shared_ptr(new (PInt) int{3}); } +class Private { +private: + Private(int z) {} + +public: + void create() { + auto ptr = std::shared_ptr(new Private(42)); + } + + ~Private(); +}; + void initialization(int T, Base b) { // Test different kinds of initialization of the pointee. Index: test/clang-tidy/modernize-make-unique.cpp =================================================================== --- test/clang-tidy/modernize-make-unique.cpp +++ test/clang-tidy/modernize-make-unique.cpp @@ -103,6 +103,16 @@ std::unique_ptr Placement = std::unique_ptr(new (PInt) int{3}); } +class Private { +private: + Private(int z) {} + +public: + void create() { + auto ptr = std::unique_ptr(new Private(42)); + } +}; + void initialization(int T, Base b) { // Test different kinds of initialization of the pointee.