Skip to content

Commit 38268ec

Browse files
authored
[clang-tidy] Support direct initialization in modernize smart pointer (#154732)
Support for direct initialization detection in modernize smart pointer checks.
1 parent 3cac332 commit 38268ec

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ using namespace clang::ast_matchers;
1717
namespace clang::tidy::modernize {
1818

1919
static constexpr char ConstructorCall[] = "constructorCall";
20+
static constexpr char DirectVar[] = "directVar";
2021
static constexpr char ResetCall[] = "resetCall";
2122
static 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

146148
void 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.

clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class MakeSmartPtrCheck : public ClangTidyCheck {
5151
const bool IgnoreDefaultInitialization;
5252

5353
void checkConstruct(SourceManager &SM, ASTContext *Ctx,
54-
const CXXConstructExpr *Construct, const QualType *Type,
55-
const CXXNewExpr *New);
54+
const CXXConstructExpr *Construct, const VarDecl *DVar,
55+
const QualType *Type, const CXXNewExpr *New);
5656
void checkReset(SourceManager &SM, ASTContext *Ctx,
5757
const CXXMemberCallExpr *Reset, const CXXNewExpr *New);
5858

clang-tools-extra/test/clang-tidy/checkers/modernize/make-shared.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ void basic() {
109109
}
110110

111111
std::shared_ptr<int> R(new int());
112+
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_shared instead
113+
// CHECK-FIXES: std::shared_ptr<int> R = std::make_shared<int>();
112114
std::shared_ptr<int> S(new int);
113115

114116
// Create the shared_ptr as a parameter to a function.

clang-tools-extra/test/clang-tidy/checkers/modernize/make-unique.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ void basic() {
154154
}
155155

156156
std::unique_ptr<int> R(new int());
157+
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead
158+
// CHECK-FIXES: std::unique_ptr<int> R = std::make_unique<int>();
157159
std::unique_ptr<int> S(new int);
158160

159161
// Create the unique_ptr as a parameter to a function.

0 commit comments

Comments
 (0)