Skip to content

Fix false positive in cast_possible_truncation #12722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion clippy_lints/src/casts/cast_possible_truncation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ fn apply_reductions(cx: &LateContext<'_>, nbits: u64, expr: &Expr<'_>, signed: b
get_constant_bits(cx, right).map_or(0, |b| b.saturating_sub(1))
})
},
BinOpKind::Rem | BinOpKind::BitAnd => get_constant_bits(cx, right)
BinOpKind::Rem => get_constant_bits(cx, right)
.unwrap_or(u64::MAX)
.min(apply_reductions(cx, nbits, left, signed)),
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)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should apply reductions to right as well, e.g. in

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

let _ = ((x() & 255) & 999999) as u8;
let _ = (999999 & (x() & 255)) as u8; // it would stop this one linting

That & the above added to the tests should be good to go, if you could squash the commits that would be great also

A bit of a nit but a commit message that doesn't require looking up the issue # would be ideal

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())),
_ => nbits,
Expand Down
21 changes: 20 additions & 1 deletion tests/ui/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
clippy::cast_abs_to_unsigned,
clippy::no_effect,
clippy::unnecessary_operation,
clippy::unnecessary_literal_unwrap
clippy::unnecessary_literal_unwrap,
clippy::identity_op
)]

fn main() {
Expand Down Expand Up @@ -479,3 +480,21 @@ fn issue12506() -> usize {
let bar: Result<Option<i64>, u32> = Ok(Some(10));
bar.unwrap().unwrap() as 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;
//~^ ERROR: casting `u64` to `u8` may truncate the value
}
Loading