rust-lang / rust-lang/rust-clippy

Lint for bit shifts by a possibly negative value

Open
#16,703 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
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 << -13u32 << (-1 & 0b11111)3u32 << 314294967295 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.