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

via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 12 11:49:42 PDT 2026


================
@@ -2531,3 +2531,45 @@ int *noreturn_dead_nested(bool cond, bool cond2, int *num) {
 }
 
 } // namespace conditional_operator_control_flow
+
+namespace method_call_uses_field_origins {
+// https://github.com/llvm/llvm-project/issues/182945
+
+int val;
+
+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}}
+  }
+};
+
+// FIXME: False-positive: the analysis tracks a, but not that it belongs to s1.
+void foo() {
+  S s;
+  {
+    int num;
+    s.p_ = #
+  }
+  s.bar();
+  s.p_ = &val;
+}
----------------
NeKon69 wrote:

The comment is mostly self-explanatory. What I'm unsure about is whether this needs to be fixed in this PR, since it appears to require a more complicated change, well, at least based on my initial attempt.

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


More information about the cfe-commits mailing list