Index: clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h =================================================================== --- clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h +++ clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.h @@ -49,7 +49,8 @@ const QualType *Type, const CXXNewExpr *New); void checkReset(SourceManager &SM, const CXXMemberCallExpr *Member, const CXXNewExpr *New); - void replaceNew(DiagnosticBuilder &Diag, const CXXNewExpr *New); + void replaceNew(DiagnosticBuilder &Diag, const CXXNewExpr *New, + SourceManager &SM); }; } // namespace modernize Index: clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp =================================================================== --- clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp +++ clang-tools-extra/trunk/clang-tidy/modernize/MakeSmartPtrCheck.cpp @@ -17,13 +17,17 @@ namespace modernize { namespace { -StringRef GetNewExprName(const CXXNewExpr *NewExpr, - const SourceManager &SM, - const LangOptions &Lang) { - return Lexer::getSourceText( +std::string GetNewExprName(const CXXNewExpr *NewExpr, + const SourceManager &SM, + const LangOptions &Lang) { + StringRef WrittenName = Lexer::getSourceText( CharSourceRange::getTokenRange( NewExpr->getAllocatedTypeSourceInfo()->getTypeLoc().getSourceRange()), SM, Lang); + if (NewExpr->isArray()) { + return WrittenName.str() + "[]"; + } + return WrittenName.str(); } } // namespace @@ -114,7 +118,7 @@ ConstructCallEnd = ConstructCallStart.getLocWithOffset(ExprStr.size()); Diag << FixItHint::CreateInsertion( ConstructCallEnd, - "<" + GetNewExprName(New, SM, getLangOpts()).str() + ">"); + "<" + GetNewExprName(New, SM, getLangOpts()) + ">"); } else { ConstructCallEnd = ConstructCallStart.getLocWithOffset(LAngle); } @@ -137,7 +141,7 @@ ")"); } - replaceNew(Diag, New); + replaceNew(Diag, New, SM); } void MakeSmartPtrCheck::checkReset(SourceManager &SM, @@ -162,23 +166,49 @@ if (Expr->isArrow()) Diag << FixItHint::CreateInsertion(ExprStart, "*"); - replaceNew(Diag, New); + replaceNew(Diag, New, SM); } void MakeSmartPtrCheck::replaceNew(DiagnosticBuilder &Diag, - const CXXNewExpr *New) { + const CXXNewExpr *New, + SourceManager& SM) { SourceLocation NewStart = New->getSourceRange().getBegin(); SourceLocation NewEnd = New->getSourceRange().getEnd(); + + std::string ArraySizeExpr; + if (const auto* ArraySize = New->getArraySize()) { + ArraySizeExpr = Lexer::getSourceText(CharSourceRange::getTokenRange( + ArraySize->getSourceRange()), + SM, getLangOpts()) + .str(); + } + switch (New->getInitializationStyle()) { case CXXNewExpr::NoInit: { - Diag << FixItHint::CreateRemoval(SourceRange(NewStart, NewEnd)); + if (ArraySizeExpr.empty()) { + Diag << FixItHint::CreateRemoval(SourceRange(NewStart, NewEnd)); + } else { + // New array expression without written initializer: + // smart_ptr(new Foo[5]); + Diag << FixItHint::CreateReplacement(SourceRange(NewStart, NewEnd), + ArraySizeExpr); + } break; } case CXXNewExpr::CallInit: { - SourceRange InitRange = New->getDirectInitRange(); - Diag << FixItHint::CreateRemoval( - SourceRange(NewStart, InitRange.getBegin())); - Diag << FixItHint::CreateRemoval(SourceRange(InitRange.getEnd(), NewEnd)); + if (ArraySizeExpr.empty()) { + SourceRange InitRange = New->getDirectInitRange(); + Diag << FixItHint::CreateRemoval( + SourceRange(NewStart, InitRange.getBegin())); + Diag << FixItHint::CreateRemoval(SourceRange(InitRange.getEnd(), NewEnd)); + } + else { + // New array expression with default/value initialization: + // smart_ptr(new int[5]()); + // smart_ptr(new Foo[5]()); + Diag << FixItHint::CreateReplacement(SourceRange(NewStart, NewEnd), + ArraySizeExpr); + } break; } case CXXNewExpr::ListInit: { Index: clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp =================================================================== --- clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp +++ clang-tools-extra/trunk/test/clang-tidy/modernize-make-unique.cpp @@ -18,6 +18,7 @@ type *release(); void reset(); void reset(type *pt); + void reset(type pt); unique_ptr &operator=(unique_ptr &&); template unique_ptr &operator=(unique_ptr &&); @@ -261,6 +262,36 @@ BB.reset(new bar::Bar()); // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: // CHECK-FIXES: BB = std::make_unique(); + + std::unique_ptr FFs; + FFs.reset(new Foo[5]); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: + // CHECK-FIXES: FFs = std::make_unique(5); + FFs.reset(new Foo[5]()); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: + // CHECK-FIXES: FFs = std::make_unique(5); + const int Num = 1; + FFs.reset(new Foo[Num]); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: + // CHECK-FIXES: FFs = std::make_unique(Num); + int Num2 = 1; + FFs.reset(new Foo[Num2]); + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: + // CHECK-FIXES: FFs = std::make_unique(Num2); + + std::unique_ptr FI; + FI.reset(new int[5]); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: + // CHECK-FIXES: FI = std::make_unique(5); + FI.reset(new int[5]()); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: + // CHECK-FIXES: FI = std::make_unique(5); + FI.reset(new int[Num]); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: + // CHECK-FIXES: FI = std::make_unique(Num); + FI.reset(new int[Num2]); + // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: + // CHECK-FIXES: FI = std::make_unique(Num2); } void aliases() {