-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Should lossy conversions be compiler warning #2784
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
Comments
That would break backwards compatibility. |
I know. :\ |
It sounds like the answer to the question is no then, right? |
I still think it's worth discussing it. maybe there are ways to do something similar without doing a breaking change (like generic constraints on the |
Maybe a compile time warning at least. (the compiler can easily check the sizes of the actual types in compile time, even if it's platform dependent) Edit: that's actually interesting IMHO. that way it can be an error opt-in with |
I edited the original post |
Is this clippy lint what you want? https://rust-lang.github.io/rust-clippy/master/#cast_possible_truncation |
A. it doesn't seem to work with all primitives: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4408dede32fead57acb6c9d3f0192073. |
My preferred approach would be to add APIs for all remaining combinations of types that However there is a lot of design work needed to get those APIs right. |
While I always support moving people away from If anyone wishes to make progress on this, I suggest trying to unblock (at least parts of) #2484 |
Hi,
Should lossy conversions (i.e.
let a = 300u16 as u8
) be a compile time warning?that way people can opt in with
#![deny(cast_lossy)]
Otherwise maybe a way to make generic that have size constraints?
I'm working on a new library where I'm casting a bunch of stuff to
usize
and for exampleusize::from(0u32)
isn't a thing. even though it should work on both 32bit and 64bit machines.So right now I have a bunch of static asserts to check that
size_of<usize> => size_of<u32>()
.Edit: Related to #2715
The text was updated successfully, but these errors were encountered: