[clang] [LifetimeSafety] Detect use of a reference type as a use of underlying origin (PR #184295)

Zhijie Wang via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 29 00:06:21 PDT 2026


================
@@ -259,14 +259,24 @@ namespace ElementReferences {
 
 void ReferenceToVectorElement() {
   std::vector<int> v = {1, 2, 3};
-  int& ref = v[0];
-  v.push_back(4);
-  // FIXME: Detect this as a use of 'ref'.
-  // https://github.com/llvm/llvm-project/issues/180187
-  ref = 10;
+  int& ref = v[0]; // expected-warning {{object whose reference is captured is later invalidated}}
+  v.push_back(4);  // expected-note {{invalidated here}}
+  ref = 10;        // expected-note {{later used here}}
   (void)ref;
 }
 
+void PointerRefToVectorElement() {
+  std::vector<int*> v = {nullptr, nullptr};
+  int*& ref = v[0];
+  v.push_back(nullptr);
+  // FIXME: Writing through a reference to an invalidated vector element
+  // should be detected. Origin_outer (reference binding) is incorrectly
+  // killed by markUseAsWrite.
+  int y;
+  ref = &y;
----------------
aeft wrote:

Add an `IsReferenceWrite` field to `UseFact` to keep the outer origin (reference binding) live and kill only inner origins.

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


More information about the cfe-commits mailing list