Skip to content

Commit 834c413

Browse files
committed
Fix cast_sign_loss false positive for float to uint casts
The cast_sign_loss lint was incorrectly triggering on float to unsigned integer casts, which cannot actually lose sign information since floats don't have the same sign semantics as signed integers. This change modifies the should_lint function to exclude float to uint casts from the cast_sign_loss lint. changelog: Fix [] false positive for float to unsigned integer casts fixed tests
1 parent fab7eab commit 834c413

File tree

3 files changed

+86
-115
lines changed

3 files changed

+86
-115
lines changed

clippy_lints/src/casts/cast_sign_loss.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ fn should_lint<'cx>(cx: &LateContext<'cx>, cast_op: &Expr<'_>, cast_from: Ty<'cx
7676
true
7777
},
7878

79-
(false, true) => !cast_to.is_signed(),
80-
81-
(_, _) => false,
79+
// Don't lint float -> int casts as they have different semantics
80+
_ => false,
8281
}
8382
}
8483

tests/ui/cast.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ fn main() {
4747

4848
1f32 as u32;
4949
//~^ cast_possible_truncation
50-
//~| cast_sign_loss
5150

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

6463
1f64 as usize;
6564
//~^ cast_possible_truncation
66-
//~| cast_sign_loss
6765

6866
1f32 as u32 as u16;
6967
//~^ cast_possible_truncation
7068
//~| cast_possible_truncation
71-
//~| cast_sign_loss
7269

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

8380
1f32 as u8;
8481
//~^ cast_possible_truncation
85-
//~| cast_sign_loss
8682
}
8783
// Test clippy::cast_possible_wrap
8884
1u8 as i8;

0 commit comments

Comments
 (0)