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

Lint masking bits and shift #6632

Closed
tesuji opened this issue Jan 23, 2021 · 3 comments · Fixed by #13743
Closed

Lint masking bits and shift #6632

tesuji opened this issue Jan 23, 2021 · 3 comments · Fixed by #13743
Assignees
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy T-AST Type: Requires working with the AST

Comments

@tesuji
Copy link
Contributor

tesuji commented Jan 23, 2021

What it does

Consider this code snippet:

pub const fn to_4_be_nibles(op: u16) -> [u8; 4] {
    [
        (op & 0xF000 >> 12) as u8,
        (op & 0x0F00 >> 8) as u8,
        (op & 0x00F0 >> 4) as u8,
        (op & 0x000F) as u8,
    ]
}

We'd realize that the code is wrong. >> has stronger precedence than & operator.
So we has to use () to group (op & 0F000) and the like.
I'd like to have a lint about this.

Categories (optional)

  • Kind: clippy::correctness

Drawbacks

None.

@tesuji tesuji added the A-lint Area: New lints label Jan 23, 2021
@llogiq
Copy link
Contributor

llogiq commented Jan 23, 2021

This is not a correctness lint. It should not even lint like this, because shifting the mask may well be the intended outcome of the code.

I'm still somewhat sympathetic to introducing brackets here, and the lint should point out the current meaning and ask if that was really intended.

We actually have the precedence lint which could be extended to cover this case.

@camsteffen camsteffen added good-first-issue These issues are a good way to get started with Clippy T-AST Type: Requires working with the AST labels Jan 26, 2021
@goth-turtle
Copy link
Contributor

@rustbot claim

@Natsume-Neko
Copy link
Contributor

@rustbot claim

@rustbot rustbot assigned Natsume-Neko and unassigned goth-turtle Nov 28, 2024
github-merge-queue bot pushed a commit that referenced this issue Dec 3, 2024
Now we can lint for the expressions like `_&_>>_`, `_<<_^_`, etc. And
will suggest to add parentheses like `_&(_>>_)` and `(_<<_)^_`.
I get implementation suggestions from
[https://github.com/rust-lang/rust-clippy/pull/8735#pullrequestreview-954273477](https://github.com/rust-lang/rust-clippy/pull/8735#pullrequestreview-954273477).
changelog: extended [`precedence`] to lint for bit masking and bit
shifting without parentheses
fixes #6632
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy T-AST Type: Requires working with the AST
Projects
None yet
5 participants