[clang] [LifetimeSafety] Detect use-after-return (PR #165370)

Kashika Akhouri via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 7 00:52:44 PST 2025


================
@@ -582,3 +714,28 @@ void lifetimebound_ctor() {
   }
   (void)v;
 }
+
+View lifetimebound_return_of_local(){
+  MyObj stack;
+  return Identity(stack); // expected-warning {{returning reference to stack allocated object}}
+                          // expected-note at -1 {{returned here}}
+}
+
+const MyObj& lifetimebound_return_ref_to_local() {
+  MyObj stack;
+  return IdentityRef(stack); // expected-warning {{returning reference to stack allocated object}}
+                             // expected-note at -1 {{returned here}}
+                             // expected-warning at -2 {{reference to stack memory associated with local variable 'stack' returned}}
+}
+
+// FIXME: The analysis does not currently model the lifetime of by-value
+// parameters, so it fails to diagnose this UAR violation.
+View lifetimebound_return_of_by_value_param(MyObj stack_param) {
+  return Identity(stack_param); 
+}
+
+// FIXME: The analysis does not currently model the lifetime of by-value
----------------
kashika0112 wrote:

Done, updated the FIXME comment

https://github.com/llvm/llvm-project/pull/165370


More information about the cfe-commits mailing list