bugprone-unchecked-optional-access false-positive with structured bindings
On clang-tidy at bdb859c9a75f0e574ac877c4ca277edcc6db4e46, 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.
@llvm/issue-subscribers-clang-tidy
@llvm/issue-subscribers-bug
And cc @ymand :)
Thanks for reporting this! FWIW, https://reviews.llvm.org/rG0e286b77cf7bebca568b60ac81c1717291040bc3 added support for structured bindings. We'll see if we can replicate this in the tests and get it fixed. @sgatev
Here's a further reduced version that also fails. Basically, it desugars the structured bindings into calls to get, which is the source of the problem. Our code only supports MemberExpr and this example needs CallExpr.
namespace std {
template <class> struct tuple_size;
template <std::size_t, class> struct tuple_element;
template <class...> class tuple;
namespace {
template <class a, a b> struct c { static const a value = b; };
} // namespace
template <class... a> struct tuple_size<tuple<a...>> : c<std::size_t, sizeof...(a)> {};
template <class...> struct ap;
template <std::size_t aq, class... ar> struct tuple_element<aq, ap<ar...>> {
typedef __type_pack_element<aq, ar...> type;
};
template <std::size_t aq, class... a> struct tuple_element<aq, tuple<a...>> {
typedef typename tuple_element<aq, ap<a...>>::type type;
};
template <class...> class tuple {};
template <std::size_t aq, class... a>
typename tuple_element<aq, tuple<a...>>::type get(tuple<a...>) {}
template <typename T>
class optional {
public:
T operator*();
operator bool();
};
} // namespace std
std::tuple<std::optional<const char *>, int> get_opt();
void target() {
auto [content, ck] = get_opt();
content ? *content : "";
}
Fixed in ef4635452f3a23ee29f7586148e19e8c84471a59.