Skip to content

[clang-tidy] [bugprone-unchecked-optional-access] should have an option to assume '.value()' is safe #163831

@dunhor

Description

@dunhor

The docs for bugprone-unchecked-optional-access say:

Given that value() has well-defined behavior (either throwing an exception or terminating the program), why treat it the same as operator*() which causes undefined behavior (UB)? That is, why is it considered unsafe to access an optional with value(), if it’s not provably populated with a value? For that matter, why is CHECK() followed by operator*() any better than value(), given that they are semantically equivalent (on configurations that disable exceptions)?
The answer is that we assume most users do not realize the difference between value() and operator*().

That may be a fair assessment for the general case, however it's not unreasonable to assume that some projects do use .value() in a safe way. It would be nice if there was an option to enable this check only for operator*, similar to how there's similar types of options for most other checks as this allows for this check to remain enabled in more projects.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions