Skip to content
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

docs: fix verbose-bit-mask example #14055

Merged
merged 1 commit into from
Jan 24, 2025

Conversation

jwong101
Copy link
Contributor

changelog: none

x & 15 == 0 is not equivalent to x.trailing_zeros() > 4, as x = 0b10000 is true for the former and false for the latter.

In fact, clippy itself suggests the following:

pub fn src(x: i32) -> bool {
    x & 15 == 0 // ~error: bit mask could be simplified with a call to `trailing_zeros`
    ^^^^^^^^^^^ help: try: `x.trailing_zeros() >= 4`
}

changelog: none

`x & 15 == 0` is not equivalent to `x.trailing_zeros() > 4`, as `x = 0b10000` is true for
the former and false for the latter.

In fact, clippy itself suggests the following:

```rust
pub fn src(x: i32) -> bool {
    x & 15 == 0 // ~error: bit mask could be simplified with a call to `trailing_zeros`
    ^^^^^^^^^^^ help: try: `x.trailing_zeros() >= 4`
}
```
@rustbot
Copy link
Collaborator

rustbot commented Jan 22, 2025

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 Jan 22, 2025
@jwong101
Copy link
Contributor Author

jwong101 commented Jan 22, 2025

Also, this comment seems to be out of date:

/// ### Known problems
/// llvm generates better code for `x & 15 == 0` on x86

As LLVM seems to compile x.trailing_zeros() >= 4 and x & 15 == 0 to the same thing(when optimizations are enabled).

Godbolt for proof.

Copy link
Member

@Alexendoo Alexendoo left a comment

Choose a reason for hiding this comment

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

Good catch, thanks!

@Alexendoo Alexendoo added this pull request to the merge queue Jan 24, 2025
Merged via the queue into rust-lang:master with commit d7e20a9 Jan 24, 2025
11 checks passed
@jwong101 jwong101 deleted the verbose-bitmask-docs branch January 24, 2025 16:35
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.

3 participants