Skip to content

[clang-tidy] bugprone-unchecked-optional-access: support detection of unsafe std::expected access #135045

Open
@BaLiKfromUA

Description

@BaLiKfromUA

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 misuse patterns apply and can be caught using the same logic currently implemented in 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 from 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) because it is widely used in codebases which don't use C++ 23.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions