cberner / redb

An embedded key-value database in pure Rust
https://www.redb.org
Apache License 2.0
3.28k stars 153 forks source link

Ban `as` casts #887

Open casey opened 6 hours ago

casey commented 6 hours ago

I think that since there are so many tricky integer conversions, as casts should be linted for and denied throughout the codebase.

I didn't see any in particular, but they're so fickle that I think they're worth avoiding entirely, so you don't even have to think about whether or not an individual instance is correct.

One thing you can do to make this easier is to add a trait like this:

trait IntoUsize {
  fn into_usize(self) -> usize;
}

And then implement it for all types smaller or equal to than minimum usize that redb supports. So since I think redb can only run on 32-bit-or-better systems, u8, u16, and u32 could all implement IntoUsize, which would get rid of a ton of the remaining as conversions.

cberner commented 5 hours ago

I enabled these denies a while back:

    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap,
    clippy::cast_precision_loss,
    clippy::cast_sign_loss,

That doesn't completely ban as, but it seems like it avoids the problematic uses. Are there additional cases you see?