[clang] [LifetimeSafety] Catch intra-method lifetime_capture_by(this) dangles (PR #204630)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 30 01:15:40 PDT 2026


================
@@ -4085,3 +4047,30 @@ void test_loop_cond_bind(bool cond) {
     consume_loop_cond_bind(cond ? &x : &y); // no-warning
   }
 }
+
+// A borrow captured into the implicit object via
+// [[clang::lifetime_capture_by(this)]] that outlives the captured local is
+// caught at the capturing method's exit.
+struct CaptureByThis {
+  int member;
+  const int *p;
+  void store(const int &x [[clang::lifetime_capture_by(this)]]);
+  void captures_dangling_local() {
+    int local = 0;
+    store(local); // expected-warning {{local variable 'local' does not live long enough}}
+  } // expected-note {{local variable 'local' is destroyed here}} expected-note {{later used here}}
----------------
NeKon69 wrote:

nit: maybe align the comments?

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


More information about the cfe-commits mailing list