Open leonardo-m opened 2 years ago
Note that this is already partially implemented in clippy as clippy::cast_lossless
. It doesn't catch the const
case.
Note that this is already partially implemented in clippy as
I think type conversions warnings/suggestions about built-in types should be in the core compiler because minimizing the usage of 'as' is sufficiently important.
I think this is too opinionated to be in the core compiler, and is better off in clippy.
And there are a lot of #[allow(clippy::cast_lossless)]
in the wild, and this is presumably in code that deliberately made a decision to turn it on (so you know they care about it some), because it's allowed by default.
Rust is in its good place also because its designers were "opinionated", with good coding taste, to avoid hundreds of un-opinionated parts of C/C++/D languages. My informed opinion is that the current design of "as" casts in Rust is one of the few design mistakes of Rust, and getting away from it is a good thing. Because you have a language where x+y correctly gives you run-time errors in debug builds (and in release builds when you use -C overflow-checks), but where one "as" casts breaks your code because it doesn't perform an overflow test. Currently the compiler suggest to use try_into and similar functions to avoid this source of bugs. So using into() or X::from() is a way to be sure the compiler isn't throwing away some bits of data. If using from/into is too much heavy for you, then others shorter solutions need to be found. I use a to!{} macro that calls try_from only in debug builds.
This is a diagnostic enhancement request in two similar parts. Currently (1.58.0-nightly ff0e14829 2021-10-31) this gives no errors nor warnings:
But I'd like a help message that suggests to replace the "as" with a from, nudging the programmer to write:
And eventually once the two features are stable, nudging to write: