Open
Description
Capturing a local variable by reference in a static lambda is a recipe for undefined behaviour:
int foo() {
int result = 0;
static auto lambda = [&result] {
++result;
};
lambda();
return result;
}
int main() {
foo();
foo(); // oops, the lambda is modifying the `result` variable from the first call
}
It would be nice if clang warned about this.
(Such a bug was caught recently in the LLVM codebase itself, #109367.)