Skip to content

Commit f476f44

Browse files
author
navh
committed
rust-lang#9231 suggest TryFrom when truncation possible
1 parent 122c374 commit f476f44

File tree

2 files changed

+20
-63
lines changed

2 files changed

+20
-63
lines changed

src/docs/cast_possible_truncation.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,14 @@ checks could be beneficial, and suggests implementing TryFrom trait.
1313
fn as_u8(x: u64) -> u8 {
1414
x as u8
1515
}
16-
```
16+
```
17+
18+
will silently truncate requiring detection and handling after the fact.
19+
20+
```
21+
fn as_u8(x: u64) -> u8 {
22+
u8::try_from(x).unwrap()
23+
}
24+
```
25+
26+
does not, but can now panic.

tests/ui/cast_size.stderr

Lines changed: 9 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@ error: casting `isize` to `i8` may truncate the value
22
--> $DIR/cast_size.rs:12:5
33
|
44
LL | 1isize as i8;
5-
| ^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^ help: avoid silent truncation by using: `i8::try_from(1isize).unwrap()`
66
|
7-
= help: if this is intentional allow the lint with `#[allow(clippy::cast_precision_loss)]` ...
87
= note: `-D clippy::cast-possible-truncation` implied by `-D warnings`
9-
help: ... or use `try_from` and handle the error accordingly
10-
|
11-
LL | i8::try_from(1isize);
12-
| ~~~~~~~~~~~~~~~~~~~~
138

149
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)
1510
--> $DIR/cast_size.rs:15:5
@@ -41,49 +36,25 @@ error: casting `isize` to `i32` may truncate the value on targets with 64-bit wi
4136
--> $DIR/cast_size.rs:19:5
4237
|
4338
LL | 1isize as i32;
44-
| ^^^^^^^^^^^^^
45-
|
46-
= help: if this is intentional allow the lint with `#[allow(clippy::cast_precision_loss)]` ...
47-
help: ... or use `try_from` and handle the error accordingly
48-
|
49-
LL | i32::try_from(1isize);
50-
| ~~~~~~~~~~~~~~~~~~~~~
39+
| ^^^^^^^^^^^^^ help: avoid silent truncation by using: `i32::try_from(1isize).unwrap()`
5140

5241
error: casting `isize` to `u32` may truncate the value on targets with 64-bit wide pointers
5342
--> $DIR/cast_size.rs:20:5
5443
|
5544
LL | 1isize as u32;
56-
| ^^^^^^^^^^^^^
57-
|
58-
= help: if this is intentional allow the lint with `#[allow(clippy::cast_precision_loss)]` ...
59-
help: ... or use `try_from` and handle the error accordingly
60-
|
61-
LL | u32::try_from(1isize);
62-
| ~~~~~~~~~~~~~~~~~~~~~
45+
| ^^^^^^^^^^^^^ help: avoid silent truncation by using: `u32::try_from(1isize).unwrap()`
6346

6447
error: casting `usize` to `u32` may truncate the value on targets with 64-bit wide pointers
6548
--> $DIR/cast_size.rs:21:5
6649
|
6750
LL | 1usize as u32;
68-
| ^^^^^^^^^^^^^
69-
|
70-
= help: if this is intentional allow the lint with `#[allow(clippy::cast_precision_loss)]` ...
71-
help: ... or use `try_from` and handle the error accordingly
72-
|
73-
LL | u32::try_from(1usize);
74-
| ~~~~~~~~~~~~~~~~~~~~~
51+
| ^^^^^^^^^^^^^ help: avoid silent truncation by using: `u32::try_from(1usize).unwrap()`
7552

7653
error: casting `usize` to `i32` may truncate the value on targets with 64-bit wide pointers
7754
--> $DIR/cast_size.rs:22:5
7855
|
7956
LL | 1usize as i32;
80-
| ^^^^^^^^^^^^^
81-
|
82-
= help: if this is intentional allow the lint with `#[allow(clippy::cast_precision_loss)]` ...
83-
help: ... or use `try_from` and handle the error accordingly
84-
|
85-
LL | i32::try_from(1usize);
86-
| ~~~~~~~~~~~~~~~~~~~~~
57+
| ^^^^^^^^^^^^^ help: avoid silent truncation by using: `i32::try_from(1usize).unwrap()`
8758

8859
error: casting `usize` to `i32` may wrap around the value on targets with 32-bit wide pointers
8960
--> $DIR/cast_size.rs:22:5
@@ -97,37 +68,19 @@ error: casting `i64` to `isize` may truncate the value on targets with 32-bit wi
9768
--> $DIR/cast_size.rs:24:5
9869
|
9970
LL | 1i64 as isize;
100-
| ^^^^^^^^^^^^^
101-
|
102-
= help: if this is intentional allow the lint with `#[allow(clippy::cast_precision_loss)]` ...
103-
help: ... or use `try_from` and handle the error accordingly
104-
|
105-
LL | isize::try_from(1i64);
106-
| ~~~~~~~~~~~~~~~~~~~~~
71+
| ^^^^^^^^^^^^^ help: avoid silent truncation by using: `isize::try_from(1i64).unwrap()`
10772

10873
error: casting `i64` to `usize` may truncate the value on targets with 32-bit wide pointers
10974
--> $DIR/cast_size.rs:25:5
11075
|
11176
LL | 1i64 as usize;
112-
| ^^^^^^^^^^^^^
113-
|
114-
= help: if this is intentional allow the lint with `#[allow(clippy::cast_precision_loss)]` ...
115-
help: ... or use `try_from` and handle the error accordingly
116-
|
117-
LL | usize::try_from(1i64);
118-
| ~~~~~~~~~~~~~~~~~~~~~
77+
| ^^^^^^^^^^^^^ help: avoid silent truncation by using: `usize::try_from(1i64).unwrap()`
11978

12079
error: casting `u64` to `isize` may truncate the value on targets with 32-bit wide pointers
12180
--> $DIR/cast_size.rs:26:5
12281
|
12382
LL | 1u64 as isize;
124-
| ^^^^^^^^^^^^^
125-
|
126-
= help: if this is intentional allow the lint with `#[allow(clippy::cast_precision_loss)]` ...
127-
help: ... or use `try_from` and handle the error accordingly
128-
|
129-
LL | isize::try_from(1u64);
130-
| ~~~~~~~~~~~~~~~~~~~~~
83+
| ^^^^^^^^^^^^^ help: avoid silent truncation by using: `isize::try_from(1u64).unwrap()`
13184

13285
error: casting `u64` to `isize` may wrap around the value on targets with 64-bit wide pointers
13386
--> $DIR/cast_size.rs:26:5
@@ -139,13 +92,7 @@ error: casting `u64` to `usize` may truncate the value on targets with 32-bit wi
13992
--> $DIR/cast_size.rs:27:5
14093
|
14194
LL | 1u64 as usize;
142-
| ^^^^^^^^^^^^^
143-
|
144-
= help: if this is intentional allow the lint with `#[allow(clippy::cast_precision_loss)]` ...
145-
help: ... or use `try_from` and handle the error accordingly
146-
|
147-
LL | usize::try_from(1u64);
148-
| ~~~~~~~~~~~~~~~~~~~~~
95+
| ^^^^^^^^^^^^^ help: avoid silent truncation by using: `usize::try_from(1u64).unwrap()`
14996

15097
error: casting `u32` to `isize` may wrap around the value on targets with 32-bit wide pointers
15198
--> $DIR/cast_size.rs:28:5

0 commit comments

Comments
 (0)