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

[lint request] unnecessary_float_transmute #3993

Closed
gnzlbg opened this issue Apr 18, 2019 · 2 comments · Fixed by #4889
Closed

[lint request] unnecessary_float_transmute #3993

gnzlbg opened this issue Apr 18, 2019 · 2 comments · Fixed by #4889
Labels
A-lint Area: New lints

Comments

@gnzlbg
Copy link
Contributor

gnzlbg commented Apr 18, 2019

The f32 and f64 types have pub fn from_bits(u32) -> f32 and pub fn to_bits(f32) -> u32 methods, yet many users don't know about them, and use transmute instead.

We should add a lint unnecessary_float_transmute that:

  • detects a transmute between a floating-point type and the unsigned integer type of the same bit width, and recommends using either from_bits or to_bits instead.

  • detects a transmute between a floating-point type and the signed integer type of the same bit-diwth, and recommends using from_bits/to_bits + an as cast (from unsigned to signed or vice-versa) instead.

When one of the arguments to the transmute is a macro variable, this lint probably can't work correctly (e.g. the macro might be expanded with types that aren't floats), so the lint should bail.

@gnzlbg gnzlbg changed the title transmute floats to ints and vice-versa [lint request] unnecessary_float_transmute Apr 18, 2019
@Centril
Copy link
Contributor

Centril commented Apr 18, 2019

I think this may also be a candidate for rustc uplifting.

@matthiaskrgr matthiaskrgr added the A-lint Area: New lints label Apr 19, 2019
@krishna-veerareddy
Copy link
Contributor

@gnzlbg Hey looks like the lint for int to float conversion already exists. I can tackle the the float to int conversion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants