Skip to content

Commit

Permalink
Fixed reduction of & in cast_possible_truncation
Browse files Browse the repository at this point in the history
Fixed formatting

Added tests for issue rust-lang#12721

Checking for reduction on RHS
  • Loading branch information
MATSMACKE committed Apr 28, 2024
1 parent 11e4934 commit fa500ff
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions clippy_lints/src/casts/cast_possible_truncation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fn apply_reductions(cx: &LateContext<'_>, nbits: u64, expr: &Expr<'_>, signed: b
BinOpKind::BitAnd => get_constant_bits(cx, right)
.unwrap_or(u64::MAX)
.min(get_constant_bits(cx, left).unwrap_or(u64::MAX))
.min(apply_reductions(cx, nbits, right, signed))
.min(apply_reductions(cx, nbits, left, signed)),
BinOpKind::Shr => apply_reductions(cx, nbits, left, signed)
.saturating_sub(constant_int(cx, right).map_or(0, |s| u64::try_from(s).unwrap_or_default())),
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
clippy::unnecessary_operation,
clippy::unnecessary_literal_unwrap,
clippy::identity_op
clippy::unnecessary_literal_unwrap,
clippy::identity_op
)]

fn main() {
Expand Down Expand Up @@ -482,8 +484,17 @@ fn issue12506() -> usize {
}

fn issue12721() {
fn x() -> u64 {
u64::MAX
}

// Don't lint.
(255 & 999999u64) as u8;
// Don't lint.
let _ = ((x() & 255) & 999999) as u8;
// Don't lint.
let _ = (999999 & (x() & 255)) as u8;

(256 & 999999u64) as u8;
//~^ ERROR: casting `u64` to `u8` may truncate the value
(255 % 999999u64) as u8;
Expand Down
35 changes: 33 additions & 2 deletions tests/ui/cast.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
error: casting `i32` to `f32` causes a loss of precision (`i32` is 32 bits wide, but `f32`'s mantissa is only 23 bits wide)
--> tests/ui/cast.rs:23:5
--> tests/ui/cast.rs:23:5
|
LL | x0 as f32;
Expand All @@ -8,18 +9,21 @@ LL | x0 as f32;
= help: to override `-D warnings` add `#[allow(clippy::cast_precision_loss)]`

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

error: casting `i64` to `f64` causes a loss of precision (`i64` is 64 bits wide, but `f64`'s mantissa is only 52 bits wide)
--> tests/ui/cast.rs:29:5
--> tests/ui/cast.rs:29:5
|
LL | x1 as f64;
| ^^^^^^^^^

error: casting `u32` to `f32` causes a loss of precision (`u32` is 32 bits wide, but `f32`'s mantissa is only 23 bits wide)
--> tests/ui/cast.rs:32:5
--> tests/ui/cast.rs:32:5
|
LL | x2 as f32;
Expand All @@ -32,12 +36,14 @@ LL | x3 as f32;
| ^^^^^^^^^

error: casting `u64` to `f64` causes a loss of precision (`u64` is 64 bits wide, but `f64`'s mantissa is only 52 bits wide)
--> tests/ui/cast.rs:37:5
--> tests/ui/cast.rs:37:5
|
LL | x3 as f64;
| ^^^^^^^^^

error: casting `f32` to `i32` may truncate the value
--> tests/ui/cast.rs:40:5
--> tests/ui/cast.rs:40:5
|
LL | 1f32 as i32;
Expand All @@ -48,6 +54,7 @@ LL | 1f32 as i32;
= help: to override `-D warnings` add `#[allow(clippy::cast_possible_truncation)]`

error: casting `f32` to `u32` may truncate the value
--> tests/ui/cast.rs:42:5
--> tests/ui/cast.rs:42:5
|
LL | 1f32 as u32;
Expand All @@ -56,6 +63,7 @@ LL | 1f32 as u32;
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...

error: casting `f32` to `u32` may lose the sign of the value
--> tests/ui/cast.rs:42:5
--> tests/ui/cast.rs:42:5
|
LL | 1f32 as u32;
Expand All @@ -65,6 +73,7 @@ LL | 1f32 as u32;
= help: to override `-D warnings` add `#[allow(clippy::cast_sign_loss)]`

error: casting `f64` to `f32` may truncate the value
--> tests/ui/cast.rs:46:5
--> tests/ui/cast.rs:46:5
|
LL | 1f64 as f32;
Expand All @@ -73,6 +82,7 @@ LL | 1f64 as f32;
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...

error: casting `i32` to `i8` may truncate the value
--> tests/ui/cast.rs:48:5
--> tests/ui/cast.rs:48:5
|
LL | 1i32 as i8;
Expand All @@ -85,6 +95,7 @@ LL | i8::try_from(1i32);
| ~~~~~~~~~~~~~~~~~~

error: casting `i32` to `u8` may truncate the value
--> tests/ui/cast.rs:50:5
--> tests/ui/cast.rs:50:5
|
LL | 1i32 as u8;
Expand All @@ -97,6 +108,7 @@ LL | u8::try_from(1i32);
| ~~~~~~~~~~~~~~~~~~

error: casting `f64` to `isize` may truncate the value
--> tests/ui/cast.rs:52:5
--> tests/ui/cast.rs:52:5
|
LL | 1f64 as isize;
Expand All @@ -105,6 +117,7 @@ LL | 1f64 as isize;
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...

error: casting `f64` to `usize` may truncate the value
--> tests/ui/cast.rs:54:5
--> tests/ui/cast.rs:54:5
|
LL | 1f64 as usize;
Expand All @@ -113,12 +126,14 @@ LL | 1f64 as usize;
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...

error: casting `f64` to `usize` may lose the sign of the value
--> tests/ui/cast.rs:54:5
--> tests/ui/cast.rs:54:5
|
LL | 1f64 as usize;
| ^^^^^^^^^^^^^

error: casting `u32` to `u16` may truncate the value
--> tests/ui/cast.rs:57:5
--> tests/ui/cast.rs:57:5
|
LL | 1f32 as u32 as u16;
Expand All @@ -131,6 +146,7 @@ LL | u16::try_from(1f32 as u32);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~

error: casting `f32` to `u32` may truncate the value
--> tests/ui/cast.rs:57:5
--> tests/ui/cast.rs:57:5
|
LL | 1f32 as u32 as u16;
Expand All @@ -139,12 +155,14 @@ LL | 1f32 as u32 as u16;
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...

error: casting `f32` to `u32` may lose the sign of the value
--> tests/ui/cast.rs:57:5
--> tests/ui/cast.rs:57:5
|
LL | 1f32 as u32 as u16;
| ^^^^^^^^^^^

error: casting `i32` to `i8` may truncate the value
--> tests/ui/cast.rs:62:22
--> tests/ui/cast.rs:62:22
|
LL | let _x: i8 = 1i32 as _;
Expand All @@ -157,6 +175,7 @@ LL | let _x: i8 = 1i32.try_into();
| ~~~~~~~~~~~~~~~

error: casting `f32` to `i32` may truncate the value
--> tests/ui/cast.rs:64:9
--> tests/ui/cast.rs:64:9
|
LL | 1f32 as i32;
Expand All @@ -165,6 +184,7 @@ LL | 1f32 as i32;
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...

error: casting `f64` to `i32` may truncate the value
--> tests/ui/cast.rs:66:9
--> tests/ui/cast.rs:66:9
|
LL | 1f64 as i32;
Expand All @@ -173,6 +193,7 @@ LL | 1f64 as i32;
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...

error: casting `f32` to `u8` may truncate the value
--> tests/ui/cast.rs:68:9
--> tests/ui/cast.rs:68:9
|
LL | 1f32 as u8;
Expand All @@ -181,12 +202,14 @@ LL | 1f32 as u8;
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...

error: casting `f32` to `u8` may lose the sign of the value
--> tests/ui/cast.rs:68:9
--> tests/ui/cast.rs:68:9
|
LL | 1f32 as u8;
| ^^^^^^^^^^

error: casting `u8` to `i8` may wrap around the value
--> tests/ui/cast.rs:73:5
--> tests/ui/cast.rs:73:5
|
LL | 1u8 as i8;
Expand All @@ -196,30 +219,35 @@ LL | 1u8 as i8;
= help: to override `-D warnings` add `#[allow(clippy::cast_possible_wrap)]`

error: casting `u16` to `i16` may wrap around the value
--> tests/ui/cast.rs:76:5
--> tests/ui/cast.rs:76:5
|
LL | 1u16 as i16;
| ^^^^^^^^^^^

error: casting `u32` to `i32` may wrap around the value
--> tests/ui/cast.rs:78:5
--> tests/ui/cast.rs:78:5
|
LL | 1u32 as i32;
| ^^^^^^^^^^^

error: casting `u64` to `i64` may wrap around the value
--> tests/ui/cast.rs:80:5
--> tests/ui/cast.rs:80:5
|
LL | 1u64 as i64;
| ^^^^^^^^^^^

error: casting `usize` to `isize` may wrap around the value
--> tests/ui/cast.rs:82:5
--> tests/ui/cast.rs:82:5
|
LL | 1usize as isize;
| ^^^^^^^^^^^^^^^

error: casting `usize` to `i8` may truncate the value
--> tests/ui/cast.rs:85:5
--> tests/ui/cast.rs:85:5
|
LL | 1usize as i8;
Expand All @@ -232,6 +260,7 @@ LL | i8::try_from(1usize);
| ~~~~~~~~~~~~~~~~~~~~

error: casting `usize` to `i16` may truncate the value
--> tests/ui/cast.rs:88:5
--> tests/ui/cast.rs:88:5
|
LL | 1usize as i16;
Expand All @@ -244,6 +273,7 @@ LL | i16::try_from(1usize);
| ~~~~~~~~~~~~~~~~~~~~~

error: casting `usize` to `i16` may wrap around the value on targets with 16-bit wide pointers
--> tests/ui/cast.rs:88:5
--> tests/ui/cast.rs:88:5
|
LL | 1usize as i16;
Expand All @@ -253,6 +283,7 @@ LL | 1usize as i16;
= note: for more information see https://doc.rust-lang.org/reference/types/numeric.html#machine-dependent-integer-types

error: casting `usize` to `i32` may truncate the value on targets with 64-bit wide pointers
--> tests/ui/cast.rs:93:5
--> tests/ui/cast.rs:93:5
|
LL | 1usize as i32;
Expand Down Expand Up @@ -708,7 +739,7 @@ LL | bar.unwrap().unwrap() as usize
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: casting `u64` to `u8` may truncate the value
--> tests/ui/cast.rs:487:5
--> tests/ui/cast.rs:496:5
|
LL | (256 & 999999u64) as u8;
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -720,7 +751,7 @@ LL | u8::try_from(256 & 999999u64);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error: casting `u64` to `u8` may truncate the value
--> tests/ui/cast.rs:489:5
--> tests/ui/cast.rs:498:5
|
LL | (255 % 999999u64) as u8;
| ^^^^^^^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit fa500ff

Please sign in to comment.