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

[as_conversions] No alternative exists to float to/from int conversion using as #5122

Closed
casey opened this issue Feb 1, 2020 · 4 comments
Closed

Comments

@casey
Copy link

casey commented Feb 1, 2020

The as_conversions lint warns if the as operator is used for int-to-float and float-to-int conversions, however I don't believe that there is actually an alternative.

It might be good to relax the lint so it doesn't complain if either argument is a float until an alternative exists.

@flip1995
Copy link
Member

flip1995 commented Feb 2, 2020

IMO it is an restriction lint, that should warn exactly about this. A float-to-int or int-to-float conversion is just such a dangerous conversion, that can be silently lossy.
Playgroun

Allowing this, would defeat the purpose of this lint. For the cases, where into/from exists, we have the cast_* lints.

Side note: restriction lints are called like this, because they "restrict" the language, without claiming that the thing is per se bad. In this case the lint removes as from the language and therefore restricts the language.

@flip1995 flip1995 closed this as completed Feb 2, 2020
@casey
Copy link
Author

casey commented Feb 2, 2020

I think that's fair, but looking at the cast_* lints, it's not immediately obvious to me that there's a set of lints which would warn me about casts where a better alternative exists, i.e. try_from and from, but not warn me about casts where no alternative exists. Or am I not seeing it?

I.e. it seems reasonable to want 0u8 as 0u16 to be disallowed (into exists), 0u16 as 0u8 to be disallowed (try_into exists), but u8 as f64 to be allowed, since there's no alternative, but no combination of lints can give you that.

@flip1995
Copy link
Member

flip1995 commented Feb 2, 2020

That is because an int-float-conversion has no alternatives, so we can't warn on int as float code, since it cannot be improved. This is where this lint comes into play. Just disallow every as conversion and if you are sure, that the conversion you're doing is save, add an #[allow(...)]. Kind of like unsafe.

@casey
Copy link
Author

casey commented Feb 2, 2020

I guess that's reasonable. Thanks for the help!

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