[clang] [LifetimeSafety] Detect iterator invalidation through container aliases (PR #195231)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Sat May 2 00:33:18 PDT 2026


================
@@ -254,6 +253,46 @@ void IteratorUsedAfterPushBack(std::vector<int> v) {
 }
 }  // namespace SimpleInvalidIterators
 
+namespace InvalidatingThroughContainerAliases {
+void IteratorInvalidatedThroughLocalReferenceAlias() {
+  std::vector<int> vv;
+  std::vector<int> &v = vv;
+  auto it = vv.begin(); // expected-warning {{object whose reference is captured is later invalidated}}
+  v.push_back(42);      // expected-note {{invalidated here}}
+  (void)it;             // expected-note {{later used here}}
+}
+
+void IteratorInvalidatedThroughPointerParameter(std::vector<int> *v) { // expected-warning {{parameter is later invalidated}}
+  auto it = v->begin();
+  v->push_back(42); // expected-note {{invalidated here}}
+  (void)it;         // expected-note {{later used here}}
+}
+} // namespace InvalidatingThroughContainerAliases
+
+namespace ContainerObjectAliases {
+// FIXME: Distinguish owner-borrow from content-borrow.
----------------
usx95 wrote:

Agreed with the concern here. But the current state of invalidation is not adoptable in any form. So I am fine with having these false positives for now until we fix "interior" destruction more systematically.

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


More information about the cfe-commits mailing list