avoid linting possible_truncation on bit-reducing operations

This commit is contained in:
Andre Bogus 2021-10-13 13:45:53 +02:00
parent df65291edd
commit 00ea73e162
3 changed files with 101 additions and 9 deletions

View file

@ -100,4 +100,19 @@ fn main() {
let s = x.signum();
let _ = s as i32;
// Test for signed min
(-99999999999i64).min(1) as i8; // should be linted because signed
// Test for various operations that remove enough bits for the result to fit
(999999u64 & 1) as u8;
(999999u64 % 15) as u8;
(999999u64 / 0x1_0000_0000_0000) as u16;
({ 999999u64 >> 56 }) as u8;
({
let x = 999999u64;
x.min(1)
}) as u8;
999999u64.clamp(0, 255) as u8;
999999u64.clamp(0, 256) as u8; // should still be linted
}

View file

@ -138,5 +138,17 @@ error: casting `isize` to `usize` may lose the sign of the value
LL | -1isize as usize;
| ^^^^^^^^^^^^^^^^
error: aborting due to 22 previous errors
error: casting `i64` to `i8` may truncate the value
--> $DIR/cast.rs:105:5
|
LL | (-99999999999i64).min(1) as i8; // should be linted because signed
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: casting `u64` to `u8` may truncate the value
--> $DIR/cast.rs:117:5
|
LL | 999999u64.clamp(0, 256) as u8; // should still be linted
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 24 previous errors