[clang] [LifetimeSafety] Suggest lifetime annotations (PR #169767)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 28 06:38:59 PST 2025


================
@@ -0,0 +1,81 @@
+// RUN: %clang_cc1 -fsyntax-only -fexperimental-lifetime-safety -Wexperimental-lifetime-safety-suggestions -verify %s
+
+struct MyObj {
+  int id;
+  ~MyObj() {}  // Non-trivial destructor
+  MyObj operator+(MyObj);
+};
+
+struct [[gsl::Pointer()]] View {
+  View(const MyObj&); // Borrows from MyObj
+  View();
+  void use() const;
+};
+
+//===----------------------------------------------------------------------===//
+// Lifetimebound Annotation Suggestion Tests
+//===----------------------------------------------------------------------===//
+
+View return_view_directly (View a) {    // expected-warning {{param should be marked [[clang::lifetimebound]]}}.
+  return a;                             // expected-note {{param returned here}}
+}
+
+View conditional_return_view (
----------------
usx95 wrote:

One more idea about control flow:

```
View test (

    View a,
    View b) {         // expected-warning {{param should be marked [[clang::lifetimebound]]}}.
  a = b;
  return a;
}
```

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


More information about the cfe-commits mailing list