[clang] [LifetimeSafety] Warn on implicit this lifetimebound violations (PR #196926)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Mon May 11 06:41:11 PDT 2026


================
@@ -86,3 +86,48 @@ View annotated_decl_but_not_def_not_returned(const MyObj &obj [[clang::lifetimeb
 View annotated_decl_but_not_def_not_returned(const MyObj &obj) {
   return not_lb(obj);
 }
+
+struct BadThisReturn {
+  MyObj data;
+
+  View get() const [[clang::lifetimebound]] { // expected-warning {{could not verify that the return value can be lifetime bound to the implicit this parameter}}
+    return not_lb(data);
+  }
+};
+
+struct GoodThisReturn {
+  MyObj data;
+
+  View get() const [[clang::lifetimebound]] {
+    return data;
+  }
+};
+
+struct RedeclaredThis {
+  MyObj data;
+  View get() const [[clang::lifetimebound]]; // expected-warning {{could not verify that the return value can be lifetime bound to the implicit this parameter}}
----------------
usx95 wrote:

For a separate PR. To make this diagnositc helpful in cases we have diagnostics at decl and not def, we should also attach a note to the function definition we analysed to come to this conclusion (something like "while analysing this function defintion").

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


More information about the cfe-commits mailing list