[clang] [LifetimeSafety] Fix false negative for GSL Owner type with arrow operator (PR #184725)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 5 03:44:20 PST 2026


================
@@ -1746,3 +1746,32 @@ View test3(std::string a) {
   return b;                 // expected-note {{returned here}}
 }
 } // namespace non_trivial_views
+
+namespace OwnerArrowOperator {
+void test_optional_arrow() {
+  const char* p;
+  {
+    std::optional<std::string> opt;
+    p = opt->data();  // expected-warning {{object whose reference is captured does not live long enough}}
+  }                   // expected-note {{destroyed here}}
+  (void)*p;           // expected-note {{later used here}}
+}
+
+void test_optional_arrow_lifetimebound() {
+  View v;
+  {
+    std::optional<MyObj> opt;
+    v = opt->getView();  // expected-warning {{object whose reference is captured does not live long enough}}
+  }                      // expected-note {{destroyed here}}
+  v.use();               // expected-note {{later used here}}
+}
+
+void test_unique_ptr_arrow() {
----------------
usx95 wrote:

Oh wow. We were actually missing this https://godbolt.org/z/M87nqah85. 

This should be much more prevalent. 

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


More information about the cfe-commits mailing list