[clang] [LifetimeSafety] Add UseFacts for function arguments and assignment RHS (PR #180446)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 9 06:01:17 PST 2026


================
@@ -260,9 +260,22 @@ void PointerToVectorElement() {
 }
 
 void SelfInvalidatingMap() {
-  std::unordered_map<int, int> mp;
-  mp[1] = 1;
-  mp[2] = mp[1];  // FIXME: Detect this. We are mising a UseFact for the assignment params.
+  std::unordered_map<int, std::string> mp;
+  // TODO: We do not have a way to differentiate between pointer stability and iterator stability!
+  // std::unordered_map and other containers provide pointer/reference stability. Therefore the
+  // following is safe in practice.
+  // On the other hand, std::flat_hash_map (since C++23) does not provide pointer stability on
+  // insertion and following is unsafe for this container.
+  mp[1] = "42";
+  mp[2] = mp[1];  // expected-warning {{object whose reference is captured is later invalidated}} \
----------------
usx95 wrote:

This gets interesting. As far as I can think, there is no safe way to write `mp[2] = mp[1]` without introducing a copy

```cpp
std::string val = mp[1]; 
mp[2] = val;

// or 
mp[2] = std::string(mp[1]);
```

:disappointed_relieved:

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


More information about the cfe-commits mailing list