cberner / redb

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

Ban `as` casts #887

Closed casey closed 3 weeks ago

casey commented 3 weeks 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 3 weeks 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?

cberner commented 3 weeks ago

I assume this is fixed by https://github.com/cberner/redb/pull/886

casey commented 2 weeks ago

Yah I think that got rid of all the problematic uses.