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

>From 75506ca01e8db58467fb5a3e639784d10b959826 Mon Sep 17 00:00:00 2001
From: Utkarsh Saxena <[email protected]>
Date: Tue, 14 Jul 2026 20:20:11 +0000
Subject: [PATCH] fields in dtor do not dangle

---
 clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp |  7 +++++--
 clang/test/Sema/LifetimeSafety/dangling-field.cpp    | 10 ++++++++++
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp 
b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
index 095bee8135c20..dd2bffe22f4f5 100644
--- a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
@@ -856,9 +856,12 @@ void FactsGenerator::handleFullExprCleanup(
 }
 
 void FactsGenerator::handleExitBlock() {
+  bool IsDestructor = isa_and_nonnull<CXXDestructorDecl>(AC.getDecl());
   for (const Origin &O : FactMgr.getOriginMgr().getOrigins())
-    if (auto *FD = dyn_cast_if_present<FieldDecl>(O.getDecl()))
-      // Create FieldEscapeFacts for all field origins that remain live at 
exit.
+    // Create FieldEscapeFacts for all field origins that remain live at exit.
+    // Fields in destructors do not escape since the object is being destroyed.
+    if (auto *FD = dyn_cast_if_present<FieldDecl>(O.getDecl());
+        FD && !IsDestructor)
       EscapesInCurrentBlock.push_back(
           FactMgr.createFact<FieldEscapeFact>(O.ID, FD));
     else if (auto *VD = dyn_cast_if_present<VarDecl>(O.getDecl())) {
diff --git a/clang/test/Sema/LifetimeSafety/dangling-field.cpp 
b/clang/test/Sema/LifetimeSafety/dangling-field.cpp
index b1eb31c4ee486..bc73c4f7e8644 100644
--- a/clang/test/Sema/LifetimeSafety/dangling-field.cpp
+++ b/clang/test/Sema/LifetimeSafety/dangling-field.cpp
@@ -245,3 +245,13 @@ struct HasUniquePtrField {
   }
 };
 } // namespace MakeUnique
+
+namespace DtorNoWarn {
+struct DtorSet {
+  std::string_view view;
+  ~DtorSet() {
+    std::string s;
+    view = s;
+  }
+};
+} // namespace DtorNoWarn

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to