Skip to content

Commit

Permalink
rust-lang#9231 suggest TryFrom when truncation possible
Browse files Browse the repository at this point in the history
  • Loading branch information
navh committed Oct 20, 2022
1 parent 122c374 commit f476f44
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 63 deletions.
12 changes: 11 additions & 1 deletion src/docs/cast_possible_truncation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ checks could be beneficial, and suggests implementing TryFrom trait.
fn as_u8(x: u64) -> u8 {
x as u8
}
```
```

will silently truncate requiring detection and handling after the fact.

```
fn as_u8(x: u64) -> u8 {
u8::try_from(x).unwrap()
}
```

does not, but can now panic.
71 changes: 9 additions & 62 deletions tests/ui/cast_size.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ error: casting `isize` to `i8` may truncate the value
--> $DIR/cast_size.rs:12:5
|
LL | 1isize as i8;
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^ help: avoid silent truncation by using: `i8::try_from(1isize).unwrap()`
|
= help: if this is intentional allow the lint with `#[allow(clippy::cast_precision_loss)]` ...
= note: `-D clippy::cast-possible-truncation` implied by `-D warnings`
help: ... or use `try_from` and handle the error accordingly
|
LL | i8::try_from(1isize);
| ~~~~~~~~~~~~~~~~~~~~

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

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

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

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

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

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

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

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

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

0 comments on commit f476f44

Please sign in to comment.