rust-lang / rust-lang/rust-clippy
Lint suggestion: `manual_lowest_one`
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
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
xiszero.
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
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 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