<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/109462>109462</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
bugprone-unchecked-optional-access false positive
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
moar55
</td>
</tr>
</table>
<pre>
For the following snippet using std::optional monadic operations:
```cpp
struct S {
std::optional<int> a;
std::string b;
}
void test(std::optional<S> s) {
if (s.and_then(&S::a).has_value()) {
std::cout << "The value is " << *s->a << std::endl;
}
}
```
I get this clang-tidy warning:
```
unchecked access to optional value [bugprone-unchecked-optional-access]
123 TestOptional(std::optional<S> s)
124 {
125 if (s.and_then(&S::a).has_value()) {
>>> 126 std::cout << "The value is " << *s->a << std::endl;
127 }
128 }
```
This is a false positive since i do check that `s`, and subsequently its member `a`, has values, yet I get `bugprone-unchecked-optional-access` warning on the de-referencing of `s` and `a`.
Is this something feasible to support? Also, is it worth the effort, since we wouldn't be fully supporting emulating any lambda within the `and_then`, but just projections?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VE2P4jgT_jXmUgIFJwRyyIGmG2lO76H7PnKcSuJ5HTubKoP49ysnhGanZzV72I0scH09Va6nVIrItA6xFLsXsXtdqcCdH8veq3G3W1W-vpVnPwJ3CI231l-Na4GcGQZkCDRJXIv0KNKjH9h4pyz03qnaaPADjirqKDokryI5ijyZjx6GWUM8Bs3wDmL_MmvgK6RIT8axSN9AiXRxe3gRj7GQ6mES-9f7Zfq9eFMDI7GQh19Bv0dgErJ4rgFMA9F_o1z9nTt0Qh6EzN_naCVksekUfb8oG3AyFT8DxO-RTvvAINKTSE8gpPzoEKZQMBTlT9OR1iJ9U4viAYCutp9vB_h84-Oy9HYWv0GLDNwZAm2Va9ds6htc1eiMa78SMovB6Q71_7EGpTUSAXt48DoXLHYvVWiH0TtcP9zXi9N6jhO7vzAAz99WpvCBxP9bGPgNK7-EyL60erHs_gXmRPo2nwU0_4_ZfH7A_pld2MoD_B3FH5FcQ6CgUZYQBk-GzQWBjNMIBmoPE0HAnWIQeUIxVp5AuRooVIR_BHRsb2CYoMe-wjG6qbtbp2h-GEXphgzzVIk8-QczkCfLuIF30w6pcT1igyM6PWmbpaapoHvizfPkfKN5hMn3yF0MalCRqSzG0aQwDH5kkZ7haMnHImM_DMPVj9xNObFpoos83btyRbj6YGsn5J6hQmiCtbcFKmbAPlg13ZS7gVV9VSu4mph-Qox1LqM196kKDD8CMQyj_4H6vvPOq7pM6yIt1ArL7V7mh3ybZ8WqKxOVVqgOukZ1qA_bQlfpLsekks1W5WmWrUwpE5klhUy2RbaXxSaTB53sD01a6SKpdC6yBHtl7MbaS7_xY7syRAHLbVJkuVxZVaGlaatL6fAKk1VIGZf8WMagdRVaElliDTF9wrBhi-Xv2f1p5FZhtGXHPEy7Xp6FPLeGu1BttO-FPEf8-9_63iQhz1NVJOT5XvallH8GAAD__zCf8vA">