rust-lang / rust-lang/rust-clippy
Suggest using format precision for floats instead of casting to integer
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
What does this lint do?
Linting code where a floating point value (f64/f32) is casted to an integer just for printing/formatting. The intention behind that is to remove the fraction part of that number and only display the integer itself. This is closely related to cast_possible_truncation.
This kind of lint could be applied to everything, that takes a fmt string, e.g. format, panic, write, ... .
Categories (optional)
clippy::pedantic maybe clippy::complexity or clippy::suspicious. I'm not sure here (:
What is the advantage of the recommended code over the original code
If the value should be NaN or INFINITY, it will be printed/formatted as such and easier to detect instead of silently converting it to 0 or min/max of the type.
Drawbacks
Could be a false positive.
Example
fn print_it(a: f64) {
println!("{}", a as i64);
}
Could be written as:
fn print_it(a: f64) {
println!("{:.0}", a);
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start from the issue's Rust examples and compare the proposed formatting precision with the existing cast-related lint behavior. Determine which formatting macros and float-to-integer casts are in scope, then define tests covering normal values, NaN, infinity, and possible false positives. Done means the lint scope, category, diagnostics, and test expectations are agreed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100