<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/91835>91835</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang-static-analyzer] False positive core.NullDefererence when capturing a structured binding in a lambda
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
captaincrutches
</td>
</tr>
</table>
<pre>
Reproducible example:
```cpp
struct S { int x; };
void f(int x) { (void)x; }
int main()
{
S s{42};
auto& [x] = s;
auto g = [x](){ f(x); };
g();
}
```
This is perfectly valid C++20 code AFAIK, and compiles with both gcc (13) and clang (18.1).
The static analyzer, however, gets upset:
```
$ clang++ --version
clang version 18.1.5
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm/18/bin
Configuration file: /etc/clang/x86_64-pc-linux-gnu-clang++.cfg
$ clang++ --analyze --analyzer-output text bindings.cpp
bindings.cpp:9:23: warning: Dereference of undefined pointer value [core.NullDereference]
9 | auto g = [x](){ f(x); };
| ^
bindings.cpp:10:5: note: Calling 'operator()'
10 | g();
| ^~~
bindings.cpp:9:23: note: Dereference of undefined pointer value
9 | auto g = [x](){ f(x); };
| ^
1 warning generated.
```
But `x` in that context is neither a pointer nor undefined, as it's being captured by value into the lambda.
Godbolt to reproduce: https://godbolt.org/z/3zha6nv5f
Perhaps noteworthy:
- The same warning is emitted if `x` is captured by reference, i.e. `[&x]`
- The same warning is emitted if default by-value `[=]` or by-reference `[&]` capture is used
- However, if `x` is captured explicitly using an initializer (`[x = x]` or `[&x = x]`) the warning is not emitted
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVt2umzgQfhrnZhQEJkC4yEVyUnarlVarbe9XBgaYlWMj2-Qk56LPvrIhPz1t1VWjCIHn75vPM2MLa6lXiDuWHVh2XInJDdrsGjE6Qaoxk2sGtKtat9fd3zga3U4N1R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