| Issue |
174551
|
| Summary |
bugprone-unchecked-optional-access: should const indexing operator access be considered stable?
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
avaneyev
|
Consider the following example:
```
#include <array>
#include <optional>
bool g(const std::array<std::optional<int>, 2>& array, const int index) {
if (array[index].has_value()) {
return *array[index] > 0;
}
return false;
}
void f() {
std::array<std::optional<int>, 2> myArray {};
myArray[0] = 10;
g(myArray, 0);
g(myArray, 1);
}
```
It generates an unsafe optional access warning:
```
<source>: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?
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs