<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/135045>135045</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang-tidy] `bugprone-unchecked-optional-access`: support detection of unsafe `std::expected` access
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang-tidy
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
BaLiKfromUA
</td>
</tr>
</table>
<pre>
I'd like to propose extending the existing clang-tidy check `bugprone-unchecked-optional-access` to also detect unsafe access to `std::expected<T, E>` values.
Just like `std::optional<T>`, `std::expected<T, E>` requires validation (e.g. via `.has_value()` or `operator bool()`) before accessing the contained value via `.value()`, `operator*()`, or `operator->()`. The same kind of misuse patterns apply and can be caught using `bugprone-unchecked-optional-access` .
>From my experience with `bugprone-unchecked-optional-access` implementation, code change should be quite straightforward.
**I am planning to work on draft PR in upcoming weeks, but want to start this issue to gather feedback**. I tried to search for existing opened issues but didn't find any.
Main questions for me:
- Would it make sense to include this support directly in `bugprone-unchecked-optional-access`, or would a new check like `bugprone-unchecked-expected-access` be preferable?
- Are there any `std::expected`-specific behaviors or edge cases I may have overlooked?
If we are happy to start supporting `std::expected<T, E>`, IMO we also should support `tl::expected<T,E>` [Link](https://github.com/TartanLlama/expected) because it is widely used in codebases which don't use C++ 23.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyMVVuv4jYQ_jXmZQQKDuHywEPO7iLR7qpVdao-VhN7krg4do4vsPz7yiaci3Ra8QRmmG--uXwz6L3qDNGeVU-s-jrDGHrr9k_4Xf3aOjv8Wc8aK6_7I-MbCVqdCIKF0dnRegL6GchIZToIfXopH9JDaDTdPCh5BdGTOAFbF03sRmcNzaPJv5Gc2zEoa1DPUQjynq2LhI3aW5AUSASIxmNLcLMnI1sXPkhW1qys6edIIpBk5Zdnxr_AN1Z-Sxhn1JH8ghU1K-pfog832u9d75Gza_ZK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