Add new lint manual_isolate_lowest_one#17010
Open
b9nn wants to merge 2 commits into
Open
Conversation
Collaborator
|
r? @llogiq rustbot has assigned @llogiq. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
61de550 to
bbd262a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What it does
Closes #16967.
Adds a new lint
manual_isolate_lowest_one(category:complexity) that flagsx & -xorx & x.wrapping_neg()(and the reversed-operand variants) and suggestsx.isolate_lowest_one().Rationale
isolate_lowest_onewas stabilized in Rust 1.97.0 (MSRV constant added inclippy_utils::msrvs). The manualx & -xtrick is opaque to readers unfamiliar with it, and the-form overflows whenx == T::MINfor signed types — the method does not. The issue body has a full motivation including theNonZero<T>case.Scope of this PR
x & -x,x & x.wrapping_neg(), and both reversed orderings.SpanlessEq(matches the pattern used bymanual_is_power_of_two).&, but the explicitis_integral()guard is defensive in case adjustments produce something unexpected.The
NonZero<T>case (NonZeroU32::new(x.get() & x.get().wrapping_neg()).unwrap()) mentioned in the issue is intentionally left out of this initial PR — it requires matching a multi-node pattern acrossNonZeroXXX::new(...).unwrap()and is best as a follow-up to keep the initial review focused.Files
clippy_lints/src/manual_isolate_lowest_one.rs(new) — lint implementationclippy_lints/src/lib.rs—modand pass registrationclippy_lints/src/declared_lints.rs— lint info registeredclippy_utils/src/msrvs.rs— addedISOLATE_LOWEST_ONE(1.97.0)clippy_utils/src/sym.rs— addedwrapping_negtests/ui/manual_isolate_lowest_one.rs+.fixed— UI test (covers signed, unsigned, both orderings, different-operand negative case, macro non-expansion)Known limitation
I could not run
cargo uitest --blesslocally — my Windows toolchain is missing the MSVC linker (link.exe), and Clippy depends on the host rustc toolchain to build. The.stderrsnapshot is therefore not included in this PR. Please bless it as part of review, or let me know and I'll generate it from a Linux box. The lint logic itself is modeled directly onmanual_is_power_of_two(SpanlessEq+Sugg::hir_with_context) andmanual_ilog2(MSRV +from_expansionguards).AI disclosure
I used AI assistance to study the existing
manual_is_power_of_twoandmanual_ilog2lints, design the matching shape forisolate_lowest_one, and draft the implementation + ui-test. I have reviewed all changes myself and believe they are correct.changelog: new lint [
manual_isolate_lowest_one]