<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/60661>60661</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang-tidy: bugprone-unchecked-optional-access does not understand custom assertion functions
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mineo
</td>
</tr>
</table>
<pre>
See https://godbolt.org/z/dqeEd5bc8, reproduced here:
```
#include <optional>
#include <assert.h>
extern void noreturn1() __attribute__((__noreturn__));
[[noreturn]] extern void noreturn2();
void intermediate1(bool cond) {
if (!cond) {noreturn1();}
}
void intermediate2(bool cond) {
if (!cond) {noreturn2();}
}
#define intermediate3(cond) (static_cast<bool>(cond) ? void(0) : noreturn1())
void useSafely1(std::optional<int> opt) {
intermediate1(opt.has_value());
*opt;
}
void useSafely2(std::optional<int> opt) {
intermediate2(opt.has_value());
*opt;
}
void useSafely3(std::optional<int> opt) {
intermediate3(opt.has_value());
*opt;
}
```
clang-tidy warns about an unsafe access of `opt` in `useSafely1` and `useSafely2`, although they behave similar to the assert macro.
It would be great if it was possible to at least configure clang-tidy to understand that some assert-like functions exist that make accessing the value in an optional safe, similar to how some other checks (like `cppcoreguidelines-owning-memory` and `cppcoreguidelines-no-malloc`) can be configured to know about more functions.
(It would be best if no such configuration was necessary, but I understand that it's impossible for clang-tidy to know about function definitions of assert-like functions in other translation units)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVtuO4ygQ_RryUkqEoZM4D3mY6e5I8zwfEGEo22xjyELRmezXr3Bu7kyvVtpeCZFwq3PqHC5WKdnOI27Z8jtbvsxUpj7E7WA9hlkTzGn7ExF6okNi8hsTOyZ2XTBNcLQIsWNi9xcTO_Mnvpplo2smniHiIQaTNRroMWJZxl8Yv9YrfinnppDWa5cNApPP4UA2eOWYfP1sWKWEkRb9fXis8Rdh9PAerAEfI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