-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-tidy] Support direct initialization in modernize smart pointer #154732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
|
@llvm/pr-subscribers-clang-tidy @llvm/pr-subscribers-clang-tools-extra Author: Liu Ke (Sockke) ChangesSupport for direct initialization detection in modernize smart pointer checks. Full diff: https://github.com/llvm/llvm-project/pull/154732.diff 3 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
index deef3586628c6..b7e7d5fc049a4 100644
--- a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp
@@ -81,18 +81,16 @@ void MakeSmartPtrCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
auto IsPlacement = hasAnyPlacementArg(anything());
Finder->addMatcher(
- traverse(
- TK_AsIs,
- cxxBindTemporaryExpr(has(ignoringParenImpCasts(
- cxxConstructExpr(
- hasType(getSmartPointerTypeMatcher()), argumentCountIs(1),
- hasArgument(
- 0, cxxNewExpr(hasType(pointsTo(qualType(hasCanonicalType(
- equalsBoundNode(PointerType))))),
- CanCallCtor, unless(IsPlacement))
- .bind(NewExpression)),
- unless(isInTemplateInstantiation()))
- .bind(ConstructorCall))))),
+ traverse(TK_AsIs,
+ cxxConstructExpr(
+ hasType(getSmartPointerTypeMatcher()), argumentCountIs(1),
+ hasArgument(
+ 0, cxxNewExpr(hasType(pointsTo(qualType(hasCanonicalType(
+ equalsBoundNode(PointerType))))),
+ CanCallCtor, unless(IsPlacement))
+ .bind(NewExpression)),
+ unless(isInTemplateInstantiation()))
+ .bind(ConstructorCall)),
this);
Finder->addMatcher(
@@ -190,9 +188,14 @@ void MakeSmartPtrCheck::checkConstruct(SourceManager &SM, ASTContext *Ctx,
ConstructCallEnd = ConstructCallStart.getLocWithOffset(LAngle);
}
+ std::string FinalMakeSmartPtrFunctionName = MakeSmartPtrFunctionName.str();
+ auto Parents = Ctx->getParents(*Construct);
+ if (!Parents.empty() && Parents[0].get<clang::Decl>())
+ FinalMakeSmartPtrFunctionName = ExprStr.str() + " = " + MakeSmartPtrFunctionName.str();
+
Diag << FixItHint::CreateReplacement(
CharSourceRange::getCharRange(ConstructCallStart, ConstructCallEnd),
- MakeSmartPtrFunctionName);
+ FinalMakeSmartPtrFunctionName);
// If the smart_ptr is built with brace enclosed direct initialization, use
// parenthesis instead.
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
index e57f45c4127f9..65ece773b7c1f 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/make-shared.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/make-shared.cpp
@@ -109,6 +109,8 @@ void basic() {
}
std::shared_ptr<int> R(new int());
+ // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_shared instead
+ // CHECK-FIXES: std::shared_ptr<int> R = std::make_shared<int>();
std::shared_ptr<int> S(new int);
// Create the shared_ptr as a parameter to a function.
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
index e665ca0a15a68..13103c735276a 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/make-unique.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/make-unique.cpp
@@ -154,6 +154,8 @@ void basic() {
}
std::unique_ptr<int> R(new int());
+ // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: use std::make_unique instead
+ // CHECK-FIXES: std::unique_ptr<int> R = std::make_unique<int>();
std::unique_ptr<int> S(new int);
// Create the unique_ptr as a parameter to a function.
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
d09ad72 to
e7589f4
Compare
vbvictor
reviewed
Aug 23, 2025
Contributor
Author
|
@vbvictor Thanks for review, I've updated the code based on your comments. |
5chmidti
approved these changes
Aug 31, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Support for direct initialization detection in modernize smart pointer checks.