Skip to content

Fix cast_sign_loss false positive for float to uint casts #15382

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions clippy_lints/src/casts/cast_sign_loss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ fn should_lint<'cx>(cx: &LateContext<'cx>, cast_op: &Expr<'_>, cast_from: Ty<'cx
true
},

(false, true) => !cast_to.is_signed(),

(_, _) => false,
// Don't lint float -> int casts as they have different semantics
_ => false,
}
}

Expand Down
4 changes: 0 additions & 4 deletions tests/ui/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ fn main() {

1f32 as u32;
//~^ cast_possible_truncation
//~| cast_sign_loss

1f64 as f32;
//~^ cast_possible_truncation
Expand All @@ -63,12 +62,10 @@ fn main() {

1f64 as usize;
//~^ cast_possible_truncation
//~| cast_sign_loss

1f32 as u32 as u16;
//~^ cast_possible_truncation
//~| cast_possible_truncation
//~| cast_sign_loss

{
let _x: i8 = 1i32 as _;
Expand All @@ -82,7 +79,6 @@ fn main() {

1f32 as u8;
//~^ cast_possible_truncation
//~| cast_sign_loss
}
// Test clippy::cast_possible_wrap
1u8 as i8;
Expand Down
Loading