Skip to content

Commit 5dad517

Browse files
committed
Auto merge of #8778 - sunfishcode:main, r=giraffate
Fix `cast_lossless` to avoid warning on `usize` to `f64` conversion. Previously, the `cast_lossless` lint would issue a warning on code that converted a `usize` value to `f64`, on 32-bit targets. `usize` to `f64` is a lossless cast on 32-bit targets, however there is no corresponding `f64::from` that takes a `usize`, so `cast_lossless`'s suggested replacement does not compile. This PR disables the lint in the case of casting from `usize` or `isize`. Fixes #3689. changelog: [`cast_lossless`] no longer gives wrong suggestion on usize,isize->f64
2 parents bf7182c + 6ff77b9 commit 5dad517

File tree

2 files changed

+1
-15
lines changed

2 files changed

+1
-15
lines changed

clippy_lints/src/casts/cast_lossless.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn should_lint(
9393
} else {
9494
64
9595
};
96-
from_nbits < to_nbits
96+
!is_isize_or_usize(cast_from) && from_nbits < to_nbits
9797
},
9898
(false, true) if matches!(cast_from.kind(), ty::Bool) && meets_msrv(msrv.as_ref(), &msrvs::FROM_BOOL) => true,
9999
(_, _) => {

tests/ui/cast_size_32bit.stderr

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,12 @@ LL | x0 as f64;
1414
|
1515
= note: `-D clippy::cast-precision-loss` implied by `-D warnings`
1616

17-
error: casting `isize` to `f64` may become silently lossy if you later change the type
18-
--> $DIR/cast_size_32bit.rs:15:5
19-
|
20-
LL | x0 as f64;
21-
| ^^^^^^^^^ help: try: `f64::from(x0)`
22-
|
23-
= note: `-D clippy::cast-lossless` implied by `-D warnings`
24-
2517
error: casting `usize` to `f64` causes a loss of precision on targets with 64-bit wide pointers (`usize` is 64 bits wide, but `f64`'s mantissa is only 52 bits wide)
2618
--> $DIR/cast_size_32bit.rs:16:5
2719
|
2820
LL | x1 as f64;
2921
| ^^^^^^^^^
3022

31-
error: casting `usize` to `f64` may become silently lossy if you later change the type
32-
--> $DIR/cast_size_32bit.rs:16:5
33-
|
34-
LL | x1 as f64;
35-
| ^^^^^^^^^ help: try: `f64::from(x1)`
36-
3723
error: casting `isize` to `f32` causes a loss of precision (`isize` is 32 or 64 bits wide, but `f32`'s mantissa is only 23 bits wide)
3824
--> $DIR/cast_size_32bit.rs:17:5
3925
|

0 commit comments

Comments
 (0)