Skip to content

Add manual_isolate_lowest_one lint#17037

Merged
samueltardieu merged 1 commit into
rust-lang:masterfrom
arunsingh:add-manual-isolate-lowest-one
Jun 4, 2026
Merged

Add manual_isolate_lowest_one lint#17037
samueltardieu merged 1 commit into
rust-lang:masterfrom
arunsingh:add-manual-isolate-lowest-one

Conversation

@arunsingh

@arunsingh arunsingh commented May 19, 2026

Copy link
Copy Markdown
Contributor

View all comments

changelog: new lint: [manual_isolate_lowest_one]

Adds a new manual_isolate_lowest_one lint for manual lowest-set-bit isolation patterns that can be written with .isolate_lowest_one().

This detects simple local expressions in the following forms:

  • x & x.wrapping_neg()
  • x.wrapping_neg() & x
  • x & -x for signed integers
  • -x & x for signed integers
  • nz.get() & nz.get().wrapping_neg() for NonZero<T> integer locals
  • nz.get() & -nz.get() for signed NonZero<T> integer locals

The implementation intentionally limits suggestions to simple local paths so rustfix does not change evaluation count for non-trivial expressions. For NonZero<T>::get() forms, the suggestion preserves the original primitive result type by using nz.isolate_lowest_one().get().

Validation:

  • cargo fmt
  • cargo check -p clippy_lints
  • TESTNAME=manual_isolate_lowest_one cargo uitest
  • cargo dev fmt --check
  • git diff --check

Note: cargo test --test dogfood was attempted locally but hit the existing Windows tikv_jemalloc_sys crate resolution issue before reaching a PR-specific failure. The focused lint UI test and clippy_lints check pass.

@rustbot rustbot added needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 19, 2026
@rustbot

rustbot commented May 19, 2026

Copy link
Copy Markdown
Collaborator

r? @samueltardieu

rustbot has assigned @samueltardieu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, llogiq, samueltardieu

@samueltardieu samueltardieu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the lint highest level operation is a BitAnd, it should probably belong to the operators module.

Also, please include tests where the various parts come from macros since you exclude them from the lint.

View changes since this review

Comment thread clippy_lints/src/manual_isolate_lowest_one.rs Outdated
Comment thread clippy_lints/src/manual_isolate_lowest_one.rs Outdated
Comment thread clippy_lints/src/manual_isolate_lowest_one.rs Outdated
Comment thread clippy_lints/src/manual_isolate_lowest_one.rs Outdated
Comment thread clippy_lints/src/manual_isolate_lowest_one.rs Outdated
Comment thread clippy_lints/src/manual_isolate_lowest_one.rs Outdated
Comment thread clippy_lints/src/manual_isolate_lowest_one.rs Outdated
Comment thread clippy_lints/src/manual_isolate_lowest_one.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 19, 2026
@rustbot

rustbot commented May 19, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@arunsingh arunsingh force-pushed the add-manual-isolate-lowest-one branch 3 times, most recently from bdc6c6b to 45572cf Compare May 19, 2026 18:26
@arunsingh

Copy link
Copy Markdown
Contributor Author

Addressed review feedback in 45572cf:

  • Moved manual_isolate_lowest_one into the operators module since the top-level operation is BitAnd.
  • Added macro-origin negative UI cases for the whole & expression, lhs, rhs, and unary negation forms.
  • Updated version/MSRV to 1.98.0 and category to complexity.
  • Moved the MSRV gate behind the structural/type checks.
  • Removed syntax-context plumbing and hir_with_context; matching now uses local ids and the diagnostic helper receives the span directly.

Verification run locally:

  • cargo check -p clippy_lints
  • TESTNAME=manual_isolate_lowest_one cargo uitest
  • cargo dev fmt --check
  • git diff --check

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 19, 2026
@arunsingh arunsingh force-pushed the add-manual-isolate-lowest-one branch from 45572cf to 4525d1b Compare May 19, 2026 19:26
@arunsingh

Copy link
Copy Markdown
Contributor Author

Added explicit MSRV compatibility coverage in manual_isolate_lowest_one UI tests:

  • #[clippy::msrv = 1.97] stays silent, so the lint does not suggest an API unavailable to older MSRV projects.
  • #[clippy::msrv = 1.98] lints and rustfixes to .isolate_lowest_one().

Local focused verification:

  • TESTNAME=manual_isolate_lowest_one cargo uitest
  • cargo check -p clippy_lints
  • cargo dev fmt --check
  • git diff --check

@rustbot ready

@samueltardieu samueltardieu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NonZero support is intentionally left as follow-up work; this PR limits detection to primitive integer locals to keep the first implementation conservative and rustfix-safe.

Why would NonZero support add rustfix unsafety?

View changes since this review

Comment thread clippy_utils/src/msrvs.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 20, 2026
@arunsingh arunsingh force-pushed the add-manual-isolate-lowest-one branch from 4525d1b to af1af72 Compare May 21, 2026 10:13
@arunsingh

arunsingh commented May 21, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the latest review in af1af72.

  • On NonZero<T>: agreed, my previous PR-body wording was imprecise. NonZero does not add rustfix unsafety for simple local .get() forms, so I added support for nz.get() & nz.get().wrapping_neg() / reversed order and signed nz.get() & -nz.get() / reversed order. The suggestion preserves the original primitive result type as nz.isolate_lowest_one().get().
  • On MSRV: updated the alias and lint metadata to 1.97.0, and changed the UI test boundary to verify 1.96 is silent while 1.97 lints.
  • I kept the non-simple-expression guard: calls such as next_nonzero_u32().get() & next_nonzero_u32().get().wrapping_neg() remain intentionally silent so rustfix does not change evaluation count.

Verification after the changes:

  • cargo fmt
  • cargo check -p clippy_lints
  • TESTNAME=manual_isolate_lowest_one cargo uibless
  • TESTNAME=manual_isolate_lowest_one cargo uitest
  • cargo dev fmt --check
  • git diff --check

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 21, 2026

@samueltardieu samueltardieu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lint should also be added to the list of lints that check the MSRV in clippy_config::conf.

View changes since this review

Comment thread clippy_lints/src/operators/manual_isolate_lowest_one.rs Outdated
Comment thread clippy_lints/src/operators/manual_isolate_lowest_one.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 21, 2026
@arunsingh arunsingh force-pushed the add-manual-isolate-lowest-one branch from af1af72 to a9f8638 Compare May 21, 2026 10:51
@arunsingh

arunsingh commented May 21, 2026

Copy link
Copy Markdown
Contributor Author

I have addressed the latest review in a9f8638.

based on @samueltardieu comments:

  • I had add manual_isolate_lowest_one to clippy_config::conf's msrv lint list so the generated config metadata correctly shows that this lint observes the msrv setting.
  • Replaced the duplicated primitive/NonZero diagnostic helpers with one emit_lint(..., add_get) helper, keeping the only behavior difference as the .get() suffix in the suggestion.
  • Switched from Sugg::hir to Sugg::hir_with_applicability, so if the source snippet cannot be recovered the suggestion is no longer reported as blindly machine-applicable.

Fresh verification after the patch:

  • cargo fmt
  • cargo dev fmt
  • cargo check -p clippy_lints
  • TESTNAME=manual_isolate_lowest_one cargo uitest
  • cargo dev fmt --check
  • git diff --chec

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 21, 2026
@arunsingh arunsingh force-pushed the add-manual-isolate-lowest-one branch from a9f8638 to 029457b Compare May 21, 2026 11:06
@arunsingh

Copy link
Copy Markdown
Contributor Author

Small follow-up for CI: Clippy Test / base caught that book/src/lint_configuration.md needed to be regenerated after adding the lint to the msrv config list. I ran cargo bless --test config-metadata and amended the generated one-line book update in 029457bc14d0a2ab699a3b8c187d39d2c11573f2.

Additional verification:

  • cargo bless --test config-metadata
  • cargo dev fmt --check
  • git diff --check

@samueltardieu

Copy link
Copy Markdown
Member
  • I kept the non-simple-expression guard: calls such as next_nonzero_u32().get() & next_nonzero_u32().get().wrapping_neg() remain intentionally silent so rustfix does not change evaluation count.

What do you mean by that?

(please no LLM-generated response, I'd like to speak to a human)

@arunsingh

Copy link
Copy Markdown
Contributor Author
  • I kept the non-simple-expression guard: calls such as next_nonzero_u32().get() & next_nonzero_u32().get().wrapping_neg() remain intentionally silent so rustfix does not change evaluation count.

What do you mean by that?

(please no LLM-generated response, I'd like to speak to a human)

Sorry, I meant that the lint only handles locals because rewriting f().get() & f().get().wrapping_neg() to f().isolate_lowest_one().get() would call f() once instead of twice.

@samueltardieu samueltardieu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's start the FCP.

@rustbot label lint-nominated

View changes since this review

Comment thread clippy_lints/src/operators/manual_isolate_lowest_one.rs Outdated
Comment thread clippy_lints/src/operators/mod.rs Outdated
@rustbot rustbot added lint-nominated Create an FCP-thread on Zulip for this PR S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 28, 2026
@rustbot

rustbot commented May 28, 2026

Copy link
Copy Markdown
Collaborator

This lint has been nominated for inclusion.

A FCP topic has been created on Zulip.

ada4a
ada4a previously requested changes May 28, 2026
Comment thread clippy_lints/src/operators/manual_isolate_lowest_one.rs Outdated
Comment thread clippy_lints/src/operators/manual_isolate_lowest_one.rs
Comment thread clippy_lints/src/operators/manual_isolate_lowest_one.rs Outdated
@arunsingh arunsingh force-pushed the add-manual-isolate-lowest-one branch from 029457b to b8953cb Compare May 28, 2026 17:45
@arunsingh

arunsingh commented May 28, 2026

Copy link
Copy Markdown
Contributor Author

I had addressed the review comments. please review @rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 28, 2026
@samueltardieu samueltardieu added this pull request to the merge queue Jun 4, 2026
Merged via the queue into rust-lang:master with commit 9e94337 Jun 4, 2026
11 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lint-nominated Create an FCP-thread on Zulip for this PR needs-fcp PRs that add, remove, or rename lints and need an FCP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants