rust-lang / rust-lang/rust-clippy
Clippy suggests !RangeInclusive::contains for exclusive float range comparisons
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
I tried this code:
if humidity_percentage < 0.0 || humidity_percentage > 100.0 {
// ...
}
I expected to see this happen: no lint
Instead, this happened:
Clippy suggested to replace the "manual range implementation" with this: !(0.0..=100.0).contains(&humidity_percentage)
However, as some floating point numbers might not be exactly representable in IEEE-754, I think creating a range that does contain them (independently of what approximation the compiler actually settles for, the code suggests an exact number range) and presumably checking for equality on the edges (only to negate the whole thing afterwards) is not a nice suggestion.
I think it would be better not to suggest this in this case and maybe also not on similar cases concerning floating point numbers.
Meta
cargo clippy -V: clippy 0.0.212 (1700ca0 2020-12-08)rustc -Vv:
rustc 1.50.0-nightly (1700ca07c 2020-12-08)
binary: rustc
commit-hash: 1700ca07c6dd7becff85678409a5df6ad4cf4f47
commit-date: 2020-12-08
host: x86_64-unknown-linux-gnu
release: 1.50.0-nightly
This lint has also already made it to the beta channel.
Backtrace
warning: manual `!RangeInclusive::contains` implementation
--> src/app_mode.rs:65:12
|
65 | if humidity_percentage < 0.0
| ____________^
66 | | || humidity_percentage > 100.0
| |__________________________________________^ help: use: `!(0.0..=100.0).contains(&humidity_percentage)`
|
= note: `#[warn(clippy::manual_range_contains)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_range_contains
warning: 1 warning emitted
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
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 by reproducing the shown snippet and locating Clippy's manual_range_contains lint implementation and its existing tests. Check how exclusive float comparisons are handled; done means the lint no longer suggests the inclusive range form for this case and the behavior is covered by a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100