rust-lang / rust-lang/rust-clippy
Lint for bit shifts by a possibly negative value
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
The behavior of bit shifts by negative numbers is highly unintuitive as they are masking so you have 3u32 << -1 ⇒ 3u32 << (-1 & 0b11111) ⇒ 3u32 << 31 ⇒ 4294967295 instead of the likely expected behavior that when the shift is negative, it simply goes in the opposite direction.
Example
fn inverse_product_log2(y: i32) -> i32 {
y << y
}
Was probably meant to be
fn inverse_product_log2(y: i32) -> i32 {
if y < 0 {
y >> -y
} else
y << y
}
}
Additional Context
the product logarithm for a base b is the inverse of the function x * bᕽ over x, so the inverse of the product log itself is x * bᕽ.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No implementation files or tests are named. Start by locating existing Rust Clippy lints and tests for shift operations, then compare their structure with the requested case. Done means the lint reliably identifies shifts by possibly negative values and has coverage for the example and expected diagnostic behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100