rust-lang / rust-lang/rust-clippy

Lint suggestion: `manual_lowest_one`

Open
#16,986 0 comments 0 reactions 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

Checks for manual implementations that returns the index of the lowest bit set to one in x, and suggests using x.lowest_one() instead.

The lowest_one method will be stabilized in Rust 1.97.0 and will be available for all signed and unsigned primitive integer types and NonZero<T> where T is any signed and unsigned primitive integer type.

Advantage
  • Improve readability and clarity compared to manual implementations.
  • Eliminate the overflow when x is zero.
Drawbacks

Requires MSRV 1.97.0.

Example
let x: u32 = 5;
let index = if x == 0 {
    None
} else {
    Some(x.trailing_zeros())
};

Could be written as:

let x: u32 = 5;
let index = x.lowest_one();
Comparison with existing lints

No response

Additional Context

The lowest_one method for NonZero<T> returns the same result as the x.trailing_zeros(), so I don't think it should be included in this lint.

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 source file, test, or entry point is identified. Start by finding existing Clippy lint registrations and tests for analogous integer-pattern lints; done means the lint recognizes the described manual pattern, suggests x.lowest_one(), respects the Rust 1.97.0 MSRV, and excludes NonZero<T> cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.