Previously, clang emitted no diagnostic for the following:
auto f() {
int loc;
return [&] { return loc; };
}The problem being that this returns a dangling reference to the local variable 'loc'. This patch warns on this by extending -Wreturn-stack-address.
This patch also warns on the following, where the lambda is stored in a variable:
auto f() {
int loc;
auto lam = [&loc] {};
return lam; // warn
}I believe that this is OK, I can't think of any valid way of mutating lam that would make the code not return a dangling pointer to loc.
Also, this diagnoses the following, where a pointer to a local is captured via an init-capture:
auto f() {
int local;
return [x = &local] {}; // warn
}But not here, because we would have to verify that the pointer in lam wasn't mutated in a previous call of the lambda:
auto f() {
int local;
auto lam = [x = &local] {};
return lam; // no warn
}Thanks for taking a look!
We prefer while (true) over for (;;) (but I agree the existing do ... while (true); is terrible).