Skip to content

Document / adjust operator precedence lint #13966

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

Open
dhardy opened this issue Jan 9, 2025 · 2 comments
Open

Document / adjust operator precedence lint #13966

dhardy opened this issue Jan 9, 2025 · 2 comments

Comments

@dhardy
Copy link

dhardy commented Jan 9, 2025

Description

The precedence lint flags some stuff which is (IMO) unintuitive:

`1 << 2 + 3`

But also some stuff which is (again, IMO) clearer:

`u64::from(data[1]) << 32 | u64::from(data[0])`

The lint does not flag some stuff which is widely understood:

`2 * 3 - 1`

The lint documentation could be more precise about which operator combinations get flagged, with a link to the reference on operator expression precedence.

Further, the lint could be disagglomerated or adjusted to allow the bit-shift with binary-and/or/xor case above.

Version


Additional Labels

No response

@dhardy
Copy link
Author

dhardy commented Jan 9, 2025

Sorry, I missed that this was changed recently: #13743.

Would you consider moving the extended precedence checks for bitmasking and shift to a new lint?

@dhardy
Copy link
Author

dhardy commented Jan 9, 2025

Also, is this lint deprecated, while still being reported?

$ cargo clippy --all-targets -- -A precedence
    Checking rand_core v0.9.0-beta.1 (/home/dhardy/projects/rand/rand/rand_core)
warning: lint name `precedence` is deprecated and may not have an effect in the future
  |
  = help: change it to clippy::precedence
  = note: requested on the command line with `-A precedence`
  = note: `#[warn(renamed_and_removed_lints)]` on by default

warning: operator precedence can trip the unwary
   --> rand_core/src/block.rs:200:13
    |
200 |             u64::from(data[1]) << 32 | u64::from(data[0])
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider parenthesizing your expression: `(u64::from(data[1]) << 32) | u64::from(data[0])`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#precedence
    = note: `#[warn(clippy::precedence)]` on by default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant