Skip to content

Conversation

@Sockke
Copy link
Contributor

@Sockke Sockke commented Aug 21, 2025

Support for direct initialization detection in modernize smart pointer checks.

@llvmbot
Copy link
Member

llvmbot commented Aug 21, 2025

@llvm/pr-subscribers-clang-tidy

@llvm/pr-subscribers-clang-tools-extra

Author: Liu Ke (Sockke)

Changes

Support for direct initialization detection in modernize smart pointer checks.


Full diff: https://github.com/llvm/llvm-project/pull/154732.diff

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp (+16-13)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/modernize/make-shared.cpp (+2)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/modernize/make-unique.cpp (+2)
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.

@Sockke Sockke requested a review from njames93 August 21, 2025 11:03
@github-actions
Copy link

github-actions bot commented Aug 21, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@Sockke Sockke force-pushed the support-direct-initialization branch from d09ad72 to e7589f4 Compare August 22, 2025 09:30
@movie-travel-code movie-travel-code self-requested a review August 25, 2025 09:04
@Sockke
Copy link
Contributor Author

Sockke commented Aug 27, 2025

@vbvictor Thanks for review, I've updated the code based on your comments.

@Sockke Sockke merged commit 38268ec into llvm:main Sep 1, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants