Fd has operator int() and operator!(), but no operator bool. In a
boolean context such as "if (fd)", "fd && x" or "fd ? a : b", the
compiler converts through operator int, and an empty Fd is -1, which
is true. "!fd" works, which makes the positive form look safe when it
is not. Several such tests have crept in.
Declare an explicit, deleted operator bool. An explicit conversion is
only considered in boolean contexts, where it is an exact match and
wins over operator int, so those uses now fail to compile. Conversions
to int are unaffected. Test an Fd with "!!fd" or "!fd".
Assisted-by: Claude-Code:claude-opus-5-5
Fd &operator=(const shared_ptr<IOHandle> &);
operator int() const;
bool operator!() const;
+ /// Without this, `if (fd)` converts through operator int, and
+ /// an empty Fd (-1) is true. Test with `!!fd` or `!fd` instead.
+ explicit operator bool() const = delete;
shared_ptr<IOHandle> operator->() const;
string name() const;
string leaf_name() const;