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

Adding lint to detect lossy whole number floating-point literals #5160

Closed
krishna-veerareddy opened this issue Feb 12, 2020 · 7 comments · Fixed by #5185
Closed

Adding lint to detect lossy whole number floating-point literals #5160

krishna-veerareddy opened this issue Feb 12, 2020 · 7 comments · Fixed by #5185
Labels
A-lint Area: New lints L-correctness Lint: Belongs in the correctness lint group

Comments

@krishna-veerareddy
Copy link
Contributor

krishna-veerareddy commented Feb 12, 2020

Would adding a lint to detect whole number floating-point literals that are lossy when represented as floats be something useful. Here are a few examples:

// f32 example
let _: f32 = 16_777_219.0; // represented as 16_777_220.0
let _: f32 = -16_777_219.0; // represented as -16_777_220.0

// f64 example
let _: f64 = 9_007_199_254_740_993.0; // represented as 9_007_199_254_740_992.0
let _: f64 = -9_007_199_254_740_993.0; // represented as -9_007_199_254_740_992.0

For f32 maybe we could suggest upcasting to an f64. Not sure what we could suggest for f64 but we could at the least warn that the literal would be lossy. Would love to know your thoughts and if it is worth adding this to clippy then I can start working on it but if not feel free to close it.

@flip1995 flip1995 added L-correctness Lint: Belongs in the correctness lint group A-lint Area: New lints labels Feb 12, 2020
@flip1995
Copy link
Member

I think if someone types out a float literal that are so long, that it cannot be represented in a float number, probably he/she wanted the exact number. So I think we can make this even a correctness lint.

@krishna-veerareddy
Copy link
Contributor Author

@clarfon Hey any thoughts on this?

@clarfonthey
Copy link
Contributor

I agree-- don't we already technically have a lint for this? Maybe we'd make a warn-by-default lint for this?

@flip1995
Copy link
Member

We have 3 lints addressing similar issues:

@krishna-veerareddy
Copy link
Contributor Author

krishna-veerareddy commented Feb 13, 2020

Of the three similar issues excessive_precision sounds close enough to what we are trying to achieve so maybe we could extend it to account for whole number floats that are lossy. It is warn by default but its in the style category so maybe that is going to be an issue. Thoughts?

@flip1995
Copy link
Member

I think we can uplift excessive_precision to correctness, because the behavior of the code is different to the behavior the author intended. Or we just keep it in style, but I don't think style is a fitting group for this lint.

@krishna-veerareddy
Copy link
Contributor Author

Yeah style doesn't seem like the right category for this so I uplifted it to the correctness category.

@bors bors closed this as completed in 74f5e34 Feb 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints L-correctness Lint: Belongs in the correctness lint group
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants