Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ban as casts #887

Closed
casey opened this issue Nov 1, 2024 · 3 comments
Closed

Ban as casts #887

casey opened this issue Nov 1, 2024 · 3 comments

Comments

@casey
Copy link
Contributor

casey commented Nov 1, 2024

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
Copy link
Owner

cberner commented Nov 2, 2024

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
Copy link
Owner

cberner commented Nov 8, 2024

I assume this is fixed by #886

@cberner cberner closed this as completed Nov 8, 2024
@casey
Copy link
Contributor Author

casey commented Nov 9, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants