[clang] [LifetimeSafety] Suppress suggestion/inference for moved loans (PR #193899)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 24 07:00:58 PDT 2026


https://github.com/usx95 updated https://github.com/llvm/llvm-project/pull/193899

>From 57d0dd361e2006005072b36a31e4a7060bcf1868 Mon Sep 17 00:00:00 2001
From: Utkarsh Saxena <usx at google.com>
Date: Fri, 24 Apr 2026 06:01:33 +0000
Subject: [PATCH 1/3] shared_ptr_false_positive

---
 clang/lib/Analysis/LifetimeSafety/Checker.cpp   |  4 ++++
 clang/test/Sema/Inputs/lifetime-analysis.h      | 17 +++++++++++++++++
 .../Sema/warn-lifetime-safety-suggestions.cpp   | 17 +++++++++++++++++
 3 files changed, 38 insertions(+)

diff --git a/clang/lib/Analysis/LifetimeSafety/Checker.cpp b/clang/lib/Analysis/LifetimeSafety/Checker.cpp
index 8164eb15cb6cb..ea914fe2e1f3d 100644
--- a/clang/lib/Analysis/LifetimeSafety/Checker.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/Checker.cpp
@@ -143,6 +143,10 @@ class LifetimeChecker {
           AnnotationWarningsMap.try_emplace(MD, ReturnEsc->getReturnExpr());
     };
     for (LoanID LID : EscapedLoans) {
+      // We do not have enough confidence to suggest annotation if the escaping
+      // storage is also moved, e.g. a shared_ptr constructed from a unique_ptr.
+      if (MovedLoans.getMovedLoans(OEF).lookup(LID))
+        continue;
       const Loan *L = FactMgr.getLoanMgr().getLoan(LID);
       const AccessPath &AP = L->getAccessPath();
       if (const auto *PVD = AP.getAsPlaceholderParam())
diff --git a/clang/test/Sema/Inputs/lifetime-analysis.h b/clang/test/Sema/Inputs/lifetime-analysis.h
index 2836ec00e97cb..14226c7ff1f13 100644
--- a/clang/test/Sema/Inputs/lifetime-analysis.h
+++ b/clang/test/Sema/Inputs/lifetime-analysis.h
@@ -211,6 +211,23 @@ unique_ptr<T> make_unique(Args&&... args) {
   return unique_ptr<T>(new T(args...));
 }
 
+template<typename T>
+struct shared_ptr {
+  shared_ptr();
+  explicit shared_ptr(T*);
+  shared_ptr(const shared_ptr<T>&);
+  shared_ptr(shared_ptr<T>&&);
+  
+  template<typename U>
+  shared_ptr(unique_ptr<U>&& up) : ptr_(up.get()) { up.release(); }
+
+  ~shared_ptr();
+  T &operator*();
+  T *operator->();
+  T *get() const;
+  T* ptr_;
+};
+
 template<typename T>
 struct optional {
   optional();
diff --git a/clang/test/Sema/warn-lifetime-safety-suggestions.cpp b/clang/test/Sema/warn-lifetime-safety-suggestions.cpp
index bdcfe728f6776..973c610eb58ab 100644
--- a/clang/test/Sema/warn-lifetime-safety-suggestions.cpp
+++ b/clang/test/Sema/warn-lifetime-safety-suggestions.cpp
@@ -620,3 +620,20 @@ HasSetterField test_dangling_field_member_fn() {
 }
 
 } // namespace new_allocation_suggestion
+
+namespace GH193747 {
+
+std::unique_ptr<int> create_up();
+std::shared_ptr<int> create_sp() {
+  return create_up();
+}
+
+struct S {
+  int* field;
+  S(std::unique_ptr<int>&& up) : field(up.get()) { up.release(); }
+};
+
+S foo() {
+  return S(create_up());
+}
+} // namespace GH193747

>From d76fcad1881159f088dd0329e2171407eb58f9d2 Mon Sep 17 00:00:00 2001
From: Utkarsh Saxena <usx at google.com>
Date: Fri, 24 Apr 2026 06:05:38 +0000
Subject: [PATCH 2/3] fix docs

---
 clang/lib/Analysis/LifetimeSafety/Checker.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Analysis/LifetimeSafety/Checker.cpp b/clang/lib/Analysis/LifetimeSafety/Checker.cpp
index ea914fe2e1f3d..cc0f2a871b0b3 100644
--- a/clang/lib/Analysis/LifetimeSafety/Checker.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/Checker.cpp
@@ -143,8 +143,8 @@ class LifetimeChecker {
           AnnotationWarningsMap.try_emplace(MD, ReturnEsc->getReturnExpr());
     };
     for (LoanID LID : EscapedLoans) {
-      // We do not have enough confidence to suggest annotation if the escaping
-      // storage is also moved, e.g. a shared_ptr constructed from a unique_ptr.
+      // Skip annotation suggestion for moved loans, as ownership transfer
+      // obscures the lifetime relationship (e.g., shared_ptr from unique_ptr).
       if (MovedLoans.getMovedLoans(OEF).lookup(LID))
         continue;
       const Loan *L = FactMgr.getLoanMgr().getLoan(LID);

>From 38c0b52a57ce5c6bf83b41dc3e3a7f8c93a850d2 Mon Sep 17 00:00:00 2001
From: Utkarsh Saxena <usx at google.com>
Date: Fri, 24 Apr 2026 12:59:54 +0000
Subject: [PATCH 3/3] address comments

---
 clang/lib/Analysis/LifetimeSafety/Checker.cpp | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/clang/lib/Analysis/LifetimeSafety/Checker.cpp b/clang/lib/Analysis/LifetimeSafety/Checker.cpp
index cc0f2a871b0b3..4ae90cf751ec3 100644
--- a/clang/lib/Analysis/LifetimeSafety/Checker.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/Checker.cpp
@@ -114,7 +114,7 @@ class LifetimeChecker {
   void checkAnnotations(const OriginEscapesFact *OEF) {
     OriginID EscapedOID = OEF->getEscapedOriginID();
     LoanSet EscapedLoans = LoanPropagation.getLoans(EscapedOID, OEF);
-    auto CheckParam = [&](const ParmVarDecl *PVD) {
+    auto CheckParam = [&](const ParmVarDecl *PVD, bool IsMoved) {
       // NoEscape param should not escape.
       if (PVD->hasAttr<NoEscapeAttr>()) {
         if (auto *ReturnEsc = dyn_cast<ReturnEscapeFact>(OEF))
@@ -125,8 +125,12 @@ class LifetimeChecker {
           NoescapeWarningsMap.try_emplace(PVD, GlobalEsc->getGlobal());
         return;
       }
-      // Suggest lifetimebound for parameter escaping through return or a field
-      // in constructor.
+      // Skip annotation suggestion for moved loans, as ownership transfer
+      // obscures the lifetime relationship (e.g., shared_ptr from unique_ptr).
+      if (IsMoved)
+        return;
+      // Otherwise, suggest lifetimebound for parameter escaping through return
+      // or a field in constructor.
       if (!PVD->hasAttr<LifetimeBoundAttr>()) {
         if (auto *ReturnEsc = dyn_cast<ReturnEscapeFact>(OEF))
           AnnotationWarningsMap.try_emplace(PVD, ReturnEsc->getReturnExpr());
@@ -142,15 +146,12 @@ class LifetimeChecker {
         if (auto *ReturnEsc = dyn_cast<ReturnEscapeFact>(OEF))
           AnnotationWarningsMap.try_emplace(MD, ReturnEsc->getReturnExpr());
     };
+    auto MovedAtEscape = MovedLoans.getMovedLoans(OEF);
     for (LoanID LID : EscapedLoans) {
-      // Skip annotation suggestion for moved loans, as ownership transfer
-      // obscures the lifetime relationship (e.g., shared_ptr from unique_ptr).
-      if (MovedLoans.getMovedLoans(OEF).lookup(LID))
-        continue;
       const Loan *L = FactMgr.getLoanMgr().getLoan(LID);
       const AccessPath &AP = L->getAccessPath();
       if (const auto *PVD = AP.getAsPlaceholderParam())
-        CheckParam(PVD);
+        CheckParam(PVD, /*IsMoved=*/MovedAtEscape.lookup(LID));
       else if (const auto *MD = AP.getAsPlaceholderThis())
         CheckImplicitThis(MD);
     }



More information about the cfe-commits mailing list