5 ms·
Rust would have prevented this.
by timvisee 5y ago
Rust would have prevented this.
- pornel 5y agoRust's Option can't be simply dereferenced causing UB, so `val.is_some() & val.kaboom()` doesn't compile. Rust doesn't have implicit conversion between booleans and integers, so a problematic `true & 2` does not compile either. Rust does allow `true & true`, but booleans are strictly 0 or 1 in value, so you wouldn't get a wrong result, apart from eager evaluation. There's needless_bitwise_bool clippy lint for it.
- timvisee 5y agoYes! While the other way around an accidental `&&` when `&` would have failed as well.
- jhasse 5y ago> Rust's Option can't be simply dereferenced causing UB, so `val.is_some() & val.kaboom()` doesn't compile. I guess the equivalent of `operator->` would be `unwrap()`. It isn't UB, so this bug couldn't happen in Rust indeed. > Rust doesn't have implicit conversion between booleans and integers, so a problematic `true & 2` does not compile either. That isn't what's happening here as both `key_data_.has_value()` and `!key_data_->label().empty()` return bool.