[llvm-branch-commits] [clang] [LifetimeSafety] Track moved declarations to prevent false positives (PR #170007)

Haojian Wu via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Dec 4 00:35:13 PST 2025


hokein wrote:

> The lifetime safety analysis was previously generating false positives by warning about use-after-lifetime when the original variable was destroyed after being moved. This change prevents those false positives by tracking moved declarations and exempting them from loan expiration checks.

Just a note.

While this fixes false positives, it introduces false negatives. The pointer is not always valid after the owner object is moved, an use-after-free example (when the string uses short string optimization) https://godbolt.org/z/eP7PbaMEn

```
int main() {
    std::string_view s;
    std::string b;
    {
      std::string a = "12345";  // small literal, stored in the string object, rather than the heap.
      s = a;
      b = std::move(a);
    }
    std::cout << s << "\n"; // oops, s refers to a dangling object.

}
```

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


More information about the llvm-branch-commits mailing list