Skip to content

bugprone-unchecked-optional-access false-positive with absl::nullopt comparison #57253

@gburgessiv

Description

@gburgessiv

On clang-tidy at bdb859c, I believe bugprone-unchecked-optional-access has a false-positive.

$ $clang_tidy '-checks=-*,bugprone-unchecked-optional-access' file_name.cpp -- clang++ -x c++ -std=c++17 file_name.cpp
file_name.cpp:19:52: warning: unchecked access to optional value [bugprone-unchecked-optional-access]
  return (optional_value != absl::nullopt) ? *optional_value : 0;

Creduce'd repro:

namespace absl {
namespace a {
enum au { av };
template <au> class aw {};
template <au> class ax {};
template <typename> struct ay { static constexpr au az = av; };
template <typename> struct b { static constexpr au az = av; };
} // namespace a
struct nullopt_t {
} nullopt;
template <typename am>
class optional : a::aw<a::ay<am>::az>, a::ax<a::b<am>::az> {
public:
  am operator*();
};
template <typename am> bool operator!=(am, nullopt_t);
} // namespace absl
int c(absl::optional<int> optional_value) {
  return (optional_value != absl::nullopt) ? *optional_value : 0;
}

Note that if the condition is changed to return optional_value ? *optional_value : 0; (and explicit operator bool() const; is added to the optional class above), the diagnostic is not issued.

cc @ymand

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang-tidyclang:dataflowClang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.htmlfalse-positiveWarning fires when it should not

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions