@@ -17,6 +17,7 @@ using namespace clang::ast_matchers;
1717namespace clang ::tidy::modernize {
1818
1919static constexpr char ConstructorCall[] = " constructorCall" ;
20+ static constexpr char DirectVar[] = " directVar" ;
2021static constexpr char ResetCall[] = " resetCall" ;
2122static constexpr char NewExpression[] = " newExpression" ;
2223
@@ -78,18 +79,18 @@ void MakeSmartPtrCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
7879 auto IsPlacement = hasAnyPlacementArg (anything ());
7980
8081 Finder->addMatcher (
81- traverse (
82- TK_AsIs,
83- cxxBindTemporaryExpr ( has ( ignoringParenImpCasts (
84- cxxConstructExpr (
85- hasType (getSmartPointerTypeMatcher ()), argumentCountIs (1 ),
86- hasArgument (
87- 0 , cxxNewExpr (hasType (pointsTo (qualType (hasCanonicalType (
88- equalsBoundNode (PointerType))))),
89- CanCallCtor, unless (IsPlacement))
90- .bind (NewExpression)),
91- unless (isInTemplateInstantiation ()))
92- .bind (ConstructorCall))) )),
82+ traverse (TK_AsIs,
83+ cxxConstructExpr (
84+ anyOf ( hasParent ( cxxBindTemporaryExpr ()),
85+ hasParent ( varDecl (). bind (DirectVar))),
86+ hasType (getSmartPointerTypeMatcher ()), argumentCountIs (1 ),
87+ hasArgument (
88+ 0 , cxxNewExpr (hasType (pointsTo (qualType (hasCanonicalType (
89+ equalsBoundNode (PointerType))))),
90+ CanCallCtor, unless (IsPlacement))
91+ .bind (NewExpression)),
92+ unless (isInTemplateInstantiation ()))
93+ .bind (ConstructorCall)),
9394 this );
9495
9596 Finder->addMatcher (
@@ -116,6 +117,7 @@ void MakeSmartPtrCheck::check(const MatchFinder::MatchResult &Result) {
116117 SourceManager &SM = *Result.SourceManager ;
117118 const auto *Construct =
118119 Result.Nodes .getNodeAs <CXXConstructExpr>(ConstructorCall);
120+ const auto *DVar = Result.Nodes .getNodeAs <VarDecl>(DirectVar);
119121 const auto *Reset = Result.Nodes .getNodeAs <CXXMemberCallExpr>(ResetCall);
120122 const auto *Type = Result.Nodes .getNodeAs <QualType>(PointerType);
121123 const auto *New = Result.Nodes .getNodeAs <CXXNewExpr>(NewExpression);
@@ -138,13 +140,14 @@ void MakeSmartPtrCheck::check(const MatchFinder::MatchResult &Result) {
138140 if (!Initializes && IgnoreDefaultInitialization)
139141 return ;
140142 if (Construct)
141- checkConstruct (SM, Result.Context , Construct, Type, New);
143+ checkConstruct (SM, Result.Context , Construct, DVar, Type, New);
142144 else if (Reset)
143145 checkReset (SM, Result.Context , Reset, New);
144146}
145147
146148void MakeSmartPtrCheck::checkConstruct (SourceManager &SM, ASTContext *Ctx,
147149 const CXXConstructExpr *Construct,
150+ const VarDecl *DVar,
148151 const QualType *Type,
149152 const CXXNewExpr *New) {
150153 SourceLocation ConstructCallStart = Construct->getExprLoc ();
@@ -187,9 +190,14 @@ void MakeSmartPtrCheck::checkConstruct(SourceManager &SM, ASTContext *Ctx,
187190 ConstructCallEnd = ConstructCallStart.getLocWithOffset (LAngle);
188191 }
189192
193+ std::string FinalMakeSmartPtrFunctionName = MakeSmartPtrFunctionName.str ();
194+ if (DVar)
195+ FinalMakeSmartPtrFunctionName =
196+ ExprStr.str () + " = " + MakeSmartPtrFunctionName.str ();
197+
190198 Diag << FixItHint::CreateReplacement (
191199 CharSourceRange::getCharRange (ConstructCallStart, ConstructCallEnd),
192- MakeSmartPtrFunctionName );
200+ FinalMakeSmartPtrFunctionName );
193201
194202 // If the smart_ptr is built with brace enclosed direct initialization, use
195203 // parenthesis instead.
0 commit comments