[clang-tools-extra] [clang-tidy] Add bugprone-lambda-capture-lifetime check (PR #203757)
Utkarsh Saxena via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 15 05:54:23 PDT 2026
https://github.com/usx95 commented:
To a certain extent, it is possible to do this more systematically via LifetimeSafety.
```cpp
void test_thread() {
int x = 0;
std::thread t1([&x]() { x = 1; });
}
```
We can annotate `std::thread` ctor of `lifetime_capture_by(GLOBAL)` for lambdas.
Supporting more general container of functions should also be possible.
```cpp
void local_vector() {
std::vector<std::function<int()>> LocalFns;
{
int z;
LocalFns.emplace_back([&z]() -> int { return z; });
}
(void)LocalFns; // use-after-scope of 'z'
}
```
cc: @kashika0112 who is working on capture_by annotation and @aeft who recently extended lifetime safety for lambdas and `std::function`
Filed https://github.com/llvm/llvm-project/issues/203886 and https://github.com/llvm/llvm-project/issues/203888 to track this.
That said, I expect some road bumps because of multi-level ref types.
https://github.com/llvm/llvm-project/pull/203757
More information about the cfe-commits
mailing list