[clang-tidy] bugprone-unchecked-optional-access: should const indexing operator access be considered stable?
@dl8sd11 is already working on this.
Since Jan 6, 2026.
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Consider the following example:
```cpp
#include
#include
bool g(const std::array, 2>& array, const int index) {
if (array[index].has_value()) {
return *array[index] > 0;
}
return false;
}
void f() {
std::array, 2> myArray {};
myArray[0] = 10;
g(myArray, 0);
g(myArray, 1);
}
```
It generates an unsafe optional access warning:
```
:6:17: warning: unchecked access to optional value [bugprone-unchecked-optional-access]
6 | return *array[index] > 0;
| ^~~~~~~~~~~~
1 warning generated.
```
[godbolt](https://godbolt.org/z/1qd7q5P1c).
Yes, indexing operator is not included in the [list of documented exceptions](https://clang.llvm.org/extra/clang-tidy/checks/bugprone/unchecked-optional-access.html#exception-accessor-methods), and therefore this behaviour is expected. But this is a very common pattern, and so long as the const operator is used and the index passed in is unmodified (or a constant) this code is about as safe as accessors which are considered stable?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.