rust-clippy
rust-clippy copied to clipboard
`cast_sign_loss` doesn't recognize positivity guarantees
Summary
cast_sign_loss gives a false positive when casting a guaranteed-positive f64 to u32
Reproducer
I tried this code:
#![warn(clippy::pedantic)]
#![allow(clippy::let_and_return)]
fn main() {
let string = "10.0".to_string();
f64_is_positive(string);
}
fn f64_is_positive(string: String) -> u32 {
let f_64 = string.parse::<f64>().unwrap();
if f_64 > 0.0 {
let u_32 = f_64 as u32;
u_32
} else {
0_u32
}
}
I expected to see this happen:
Don't give warnings of cast_sign_loss since it's checked by a preceding conditional that guarantees that it cannot have negative sign, so it's impossible for it to have a sign to lose in the first place.
Instead, this happened: It gave a warning for no valid reason
Version
rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: x86_64-unknown-linux-gnu
release: 1.75.0
LLVM version: 17.0.6
Additional Labels
No response
@rustbot label I-false-positive