<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/111316>111316</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Warn about static lambda capturing local variable by reference
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang:diagnostics
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          HighCommander4
      </td>
    </tr>
</table>

<pre>
    Capturing a local variable by reference in a static lambda is a recipe for undefined behaviour:

```c++
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, https://github.com/llvm/llvm-project/issues/109367.)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykU02L4zgQ_TXypdggy44dH3zo7hB2ofc0MHMuyWVbgywFfaTJvx_kJN3uHpjLgLBRlar0Xj09DEFPlqhn-2e2PxaY4ux8_6-e5he3LGgH8nUh3XDtX_Ack9d2AgTjFBq4oNcoDYG8gqeRPFlFoC0ghIhRKzC4yAFBB0DwpPSZYHQekh1o1JYGkDTjRbvkWfXE-JHxx7fht6WYeM5rjWobYXSOiQMTHbD2HgbICU8hmQisOgJn1XvqjgRTdA84-UjmK5pbDdsft80Abnfekx-tWHvc7G7Nblg2YU8xeQtfinPphl7Gu6C2vzN5p7dpuY1lcCcmTuDcOTDxAnGmzZgXN-jxmkXKcdbwO46Gf6g1eres6VH7EEGhMV9BPqa_xfxfhDeXTNYMrM5Cj6AM2gne0GctUboUIc467D5JKQ7fkpoBQaZ8NoDCNM1ZMEU2mmt-MRnO6-v3_0G5gSQGAh0DmTEznGM8h_w-VuKTjnOSO-UWJk7GXB6_f87e_SQVmTjpEBIFJk4l76qm3THRFUNfDV3VYUF92YqD2NdtUxZzX5HAtpFjiVRVQh2obZTshvHQ1TUpIQvdCy7qkvNGiLrh5e4wVKobxNgqTlTVyGpOC2qzyyh2zk_Fen9flmVVNoVBSSas_hJinRerngaNk3UhahWYENl4vl9JyDQFVnOjQwwfDaOOhvof6O19yJ_tpd6d-QdfFsmb_i9GeWNz6cWvAAAA___SFkkP">