Skip to content

Commit 5656dc4

Browse files
committed
Fix an incorrect error message regarding the size of isize and usize
1 parent 35f8bff commit 5656dc4

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

clippy_lints/src/casts/cast_precision_loss.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,21 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, ca
2323

2424
let cast_to_f64 = to_nbits == 64;
2525
let mantissa_nbits = if cast_to_f64 { 52 } else { 23 };
26-
let arch_dependent = is_isize_or_usize(cast_from) && cast_to_f64;
27-
let arch_dependent_str = "on targets with 64-bit wide pointers ";
28-
let from_nbits_str = if arch_dependent {
29-
"64".to_owned()
30-
} else if is_isize_or_usize(cast_from) {
31-
// FIXME: handle 16 bits `usize` type
32-
"32 or 64".to_owned()
26+
27+
let has_width = if is_isize_or_usize(cast_from) {
28+
"can be up to 64 bits wide depending on the target architecture".to_owned()
3329
} else {
34-
from_nbits.to_string()
30+
format!("is {from_nbits} bits wide")
3531
};
3632

3733
span_lint(
3834
cx,
3935
CAST_PRECISION_LOSS,
4036
expr.span,
4137
format!(
42-
"casting `{0}` to `{1}` causes a loss of precision {2}(`{0}` is {3} bits wide, \
43-
but `{1}`'s mantissa is only {4} bits wide)",
44-
cast_from,
45-
if cast_to_f64 { "f64" } else { "f32" },
46-
if arch_dependent { arch_dependent_str } else { "" },
47-
from_nbits_str,
48-
mantissa_nbits
38+
"casting `{cast_from}` to `{cast_to}` may cause a loss of precision \
39+
(`{cast_from}` {has_width}, \
40+
but `{cast_to}`'s mantissa is only {mantissa_nbits} bits wide)",
4941
),
5042
);
5143
}

tests/ui/cast.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: casting `i32` to `f32` causes a loss of precision (`i32` is 32 bits wide, but `f32`'s mantissa is only 23 bits wide)
1+
error: casting `i32` to `f32` may cause a loss of precision (`i32` is 32 bits wide, but `f32`'s mantissa is only 23 bits wide)
22
--> tests/ui/cast.rs:23:5
33
|
44
LL | x0 as f32;
@@ -7,31 +7,31 @@ LL | x0 as f32;
77
= note: `-D clippy::cast-precision-loss` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::cast_precision_loss)]`
99

10-
error: casting `i64` to `f32` causes a loss of precision (`i64` is 64 bits wide, but `f32`'s mantissa is only 23 bits wide)
10+
error: casting `i64` to `f32` may cause a loss of precision (`i64` is 64 bits wide, but `f32`'s mantissa is only 23 bits wide)
1111
--> tests/ui/cast.rs:27:5
1212
|
1313
LL | x1 as f32;
1414
| ^^^^^^^^^
1515

16-
error: casting `i64` to `f64` causes a loss of precision (`i64` is 64 bits wide, but `f64`'s mantissa is only 52 bits wide)
16+
error: casting `i64` to `f64` may cause a loss of precision (`i64` is 64 bits wide, but `f64`'s mantissa is only 52 bits wide)
1717
--> tests/ui/cast.rs:30:5
1818
|
1919
LL | x1 as f64;
2020
| ^^^^^^^^^
2121

22-
error: casting `u32` to `f32` causes a loss of precision (`u32` is 32 bits wide, but `f32`'s mantissa is only 23 bits wide)
22+
error: casting `u32` to `f32` may cause a loss of precision (`u32` is 32 bits wide, but `f32`'s mantissa is only 23 bits wide)
2323
--> tests/ui/cast.rs:34:5
2424
|
2525
LL | x2 as f32;
2626
| ^^^^^^^^^
2727

28-
error: casting `u64` to `f32` causes a loss of precision (`u64` is 64 bits wide, but `f32`'s mantissa is only 23 bits wide)
28+
error: casting `u64` to `f32` may cause a loss of precision (`u64` is 64 bits wide, but `f32`'s mantissa is only 23 bits wide)
2929
--> tests/ui/cast.rs:38:5
3030
|
3131
LL | x3 as f32;
3232
| ^^^^^^^^^
3333

34-
error: casting `u64` to `f64` causes a loss of precision (`u64` is 64 bits wide, but `f64`'s mantissa is only 52 bits wide)
34+
error: casting `u64` to `f64` may cause a loss of precision (`u64` is 64 bits wide, but `f64`'s mantissa is only 52 bits wide)
3535
--> tests/ui/cast.rs:41:5
3636
|
3737
LL | x3 as f64;

tests/ui/cast_size.64bit.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL - 1isize as i8;
1313
LL + i8::try_from(1isize);
1414
|
1515

16-
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)
16+
error: casting `isize` to `f32` may cause a loss of precision (`isize` can be up to 64 bits wide depending on the target architecture, but `f32`'s mantissa is only 23 bits wide)
1717
--> tests/ui/cast_size.rs:24:5
1818
|
1919
LL | x0 as f32;
@@ -22,19 +22,19 @@ LL | x0 as f32;
2222
= note: `-D clippy::cast-precision-loss` implied by `-D warnings`
2323
= help: to override `-D warnings` add `#[allow(clippy::cast_precision_loss)]`
2424

25-
error: casting `usize` to `f32` causes a loss of precision (`usize` is 32 or 64 bits wide, but `f32`'s mantissa is only 23 bits wide)
25+
error: casting `usize` to `f32` may cause a loss of precision (`usize` can be up to 64 bits wide depending on the target architecture, but `f32`'s mantissa is only 23 bits wide)
2626
--> tests/ui/cast_size.rs:26:5
2727
|
2828
LL | x1 as f32;
2929
| ^^^^^^^^^
3030

31-
error: casting `isize` to `f64` causes a loss of precision on targets with 64-bit wide pointers (`isize` is 64 bits wide, but `f64`'s mantissa is only 52 bits wide)
31+
error: casting `isize` to `f64` may cause a loss of precision (`isize` can be up to 64 bits wide depending on the target architecture, but `f64`'s mantissa is only 52 bits wide)
3232
--> tests/ui/cast_size.rs:28:5
3333
|
3434
LL | x0 as f64;
3535
| ^^^^^^^^^
3636

37-
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)
37+
error: casting `usize` to `f64` may cause a loss of precision (`usize` can be up to 64 bits wide depending on the target architecture, but `f64`'s mantissa is only 52 bits wide)
3838
--> tests/ui/cast_size.rs:30:5
3939
|
4040
LL | x1 as f64;
@@ -165,13 +165,13 @@ error: casting `u32` to `isize` may wrap around the value on targets with 32-bit
165165
LL | 1u32 as isize;
166166
| ^^^^^^^^^^^^^
167167

168-
error: casting `i32` to `f32` causes a loss of precision (`i32` is 32 bits wide, but `f32`'s mantissa is only 23 bits wide)
168+
error: casting `i32` to `f32` may cause a loss of precision (`i32` is 32 bits wide, but `f32`'s mantissa is only 23 bits wide)
169169
--> tests/ui/cast_size.rs:61:5
170170
|
171171
LL | 999_999_999 as f32;
172172
| ^^^^^^^^^^^^^^^^^^
173173

174-
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)
174+
error: casting `usize` to `f64` may cause a loss of precision (`usize` can be up to 64 bits wide depending on the target architecture, but `f64`'s mantissa is only 52 bits wide)
175175
--> tests/ui/cast_size.rs:63:5
176176
|
177177
LL | 9_999_999_999_999_999usize as f64;

0 commit comments

Comments
 (0)