Skip to content

bugprone-unchecked-optional-access false-positive with structured bindings #57252

@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:61:14: warning: unchecked access to optional value [bugprone-unchecked-optional-access]
  content ? *content : "";
             ^

Creduce'd repro:

namespace {
namespace {
template <class a, a b> struct c { static const a value = b; };
} // namespace
} // namespace
typedef int d;
namespace std {
namespace {
template <class a> struct al : c<bool, __is_final(a)> {};
} // namespace
inline namespace am {
template <class> struct tuple_size;
template <d, class> struct tuple_element;
template <class...> class ao;
template <class... a> struct tuple_size<ao<a...>> : c<d, sizeof...(a)> {};
template <class...> struct ap;
template <d aq, class... ar> struct tuple_element<aq, ap<ar...>> {
  typedef __type_pack_element<aq, ar...> type;
};
template <d aq, class... a> struct tuple_element<aq, ao<a...>> {
  typedef typename tuple_element<aq, ap<a...>>::type type;
};
template <class, class, class> class at;
typedef at<char, char, char> au;
template <class...> class ao {};
template <d aq, class... a>
typename tuple_element<aq, ao<a...>>::type get(ao<a...>) {}
struct av {};
template <class a, bool = al<a>::value> struct aw { aw(av); };
template <class ax, class ay> class az : aw<ax>, aw<ay, 1> {
public:
  using ba = aw<ax>;
  using bb = aw<ay, 1>;
  template <class bc, class bd> az(bc be, bd bf) : ba(be), bb(bf) {}
};
template <class bi, class, class bj> class at {
public:
  typedef bj bk;
  struct bl {};
  az<bl, bk> bm;
  at(bi *) : bm(av(), av()) {}
};
} // namespace am
namespace bv {
enum bw { bx };
template <bw> class by {};
template <bw> class bz {};
template <typename> struct ca { static constexpr bw cb = bx; };
template <typename> struct cc { static constexpr bw cb = bx; };
} // namespace bv
template <typename bp>
class optional {
public:
  bp operator*();
  operator bool();
};
namespace {
ao<optional<const char *>, int> get_opt();
void ch() {
  auto [content, ck] = get_opt();
  content ? *content : "";
}
} // namespace
} // namespace std

Note that changing the last function to:

void ch() {
  auto [content, ck] = get_opt();
  auto c2 = content;
  c2 ? *c2 : "";
}

Results in clang-tidy not emitting a warning.

Metadata

Metadata

Assignees

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