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

Conversation

MATSMACKE
Copy link
Contributor

Fixes #12721

changelog: [cast_possible_truncation]: Separated checking whether integer constant has sufficient leading zeroes to be safely casted when getting remainder from bitwise and, since the latter allows a constant on either side of the operator to increase the number of leading zeroes that can be guaranteed.

@rustbot
Copy link
Collaborator

rustbot commented Apr 27, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Alexendoo (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 27, 2024
@MATSMACKE MATSMACKE requested a review from Alexendoo April 28, 2024 08:09
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, 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

Fixed formatting

Added tests for issue rust-lang#12721

Checking for reduction on RHS
@Alexendoo
Copy link
Member

Great, thanks!

@bors r+

@bors
Copy link
Contributor

bors commented Apr 28, 2024

📌 Commit 0b1f09e has been approved by Alexendoo

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Apr 28, 2024

⌛ Testing commit 0b1f09e with merge ca1ddc0...

@bors
Copy link
Contributor

bors commented Apr 28, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: Alexendoo
Pushing ca1ddc0 to master...

@bors bors merged commit ca1ddc0 into rust-lang:master Apr 28, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clippy::cast_possible_truncation is inconsistent in recognizing where values will never be truncated
4 participants