[clang] [LifetimeSafety] Detect use-after-scope through fields in member calls (PR #191731)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 13 00:48:18 PDT 2026


================
@@ -2531,3 +2531,51 @@ int *noreturn_dead_nested(bool cond, bool cond2, int *num) {
 }
 
 } // namespace conditional_operator_control_flow
+
+namespace method_call_uses_field_origins {
+int val;
+std::string GLOBAL{"123"};
+
+struct S {
+public:
+  int* p_;
+  void bar();
+  void foo() {
+    {
+      int num;
+      this->p_ = # // expected-warning {{object whose reference is captured does not live long enough}}
+    }                  // expected-note {{destroyed here}}
+    bar();             // expected-note {{later used here}}
+    this->p_ = &val;
+  }
+};
+
+struct T {
+public:
+  std::string_view v;
+  void bar();
+  void foo() {
+    v = std::string("tmp"); // expected-warning {{object whose reference is captured does not live long enough}} expected-note {{destroyed here}}
+    bar();                  // expected-note {{later used here}}
+  }
+  void baz(){
+    std::vector<std::string> vec = {"42"};
+    v = vec[0];         // expected-warning {{object whose reference is captured is later invalidated}}
+    vec.push_back("1"); // expected-note {{invalidated here}}
+    bar();              // expected-note {{later used here}}
+    v = GLOBAL;
+  }
----------------
usx95 wrote:

Can you move this to lifetime-safety-invalidations.cpp

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


More information about the cfe-commits mailing list