<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/94764>94764</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Improve error message for invalid lambda captures
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang:diagnostics
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ilya-biryukov
</td>
</tr>
</table>
<pre>
For the following code (https://gcc.godbolt.org/z/GGoWq45hs) Clang produces an error message that some people find confusing:
```cpp
struct X {
int x;
void foo() {
[x](){};
}
};
// <source>:4:10: error: 'x' in capture list does not name a variable
```
It seems that the error message could indeed be improved, a few ideas that come to mind:
- mentioning what kind of symbol we are actually referring to: `'class member 'x' cannot appear in capture list as it is not a variable'`
- we could suggest fixes in some situations `did you mean to capture 'this'?` or `did you mean to use a capture with initializer?`
- `variable` is technically correct and what Clang is using, but maybe it would be better to say`local variable or parameter`? I might be missing some cases, but it sounds more user-friendly than the [similar error](https://gcc.godbolt.org/z/q7aen8azc) about "automatic storage duration` that we have today:
```
<source>:4:10: error: 'x' cannot be captured because it does not have automatic storage duration
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVN2S2jgTfRpx08WURzY2XPiCmflI5Qm-vW1Lbbs3suToB4Y8_ZZsIJnZ1FYoCixL3X369DnCEHiwRK3YvYjd2wZTHJ1v2Vxx27G_pm_uvOmcvrYn5yGOBL0zxl3YDqCcJhByP8Y4B1EehTwJeRqUehqc7pyJT84PQp5-CHn68sX9_3u1G4OQB3g1aAeYvdNJUQC0QN47DxOFgANBHDFCcBPBTG42BD1bDcrZPgW2Qy5VvIniKOpi_ap5Xt-E6JOK8BeI5mV9A2wjvIvyvgSAs2MNvXNC7jOan0dvH7F7eRe7t3U77zZvH-Lzeq3_y8baPIjyNbjkFYnyf6I8VqI8PheiPK4d5gchm3chG2ALCueYPIHhEEE7CmBdBIsTAcIZPWNn6FOrt-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