Skip to content

Lint bit width#16902

Open
alv-around wants to merge 6 commits into
rust-lang:masterfrom
alv-around:lint-bit-width
Open

Lint bit width#16902
alv-around wants to merge 6 commits into
rust-lang:masterfrom
alv-around:lint-bit-width

Conversation

@alv-around

@alv-around alv-around commented Apr 22, 2026

Copy link
Copy Markdown

Fixes #16876

Current state:

  • suggests change of T::BITS - x.leading_zeros() for uints
  • suggests change of NonZero::<T>::BITS - x.leading_zeros() for NonZero (see comment)

Description

rust 1.97 introduces the method bit_width() for uints and NonZero (docs), this will make the manual computation redundant and unnecessary.

Example

let x: u32 = b'101';
let bit_width = u32::BITS - x.leading_zeros();

Can be replaced with:

let x: u32 = b'101';
let bit_width = x.bit_width();

changelog: [manual_bit_width]: Added a lint to detect bit_width implementations.

I have

  • Followed [lint naming conventions][lint_naming]
  • Added passing UI tests (including committed .stderr file)
  • cargo test passes locally
  • Executed cargo dev update_lints
  • Added lint documentation
  • Run cargo dev fmt

@rustbot rustbot added the needs-fcp PRs that add, remove, or rename lints and need an FCP label Apr 22, 2026
@alv-around alv-around marked this pull request as ready for review April 24, 2026 07:40
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 24, 2026
@rustbot

rustbot commented Apr 24, 2026

Copy link
Copy Markdown
Collaborator

r? @dswij

rustbot has assigned @dswij.
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, dswij, llogiq

@alv-around alv-around force-pushed the lint-bit-width branch 2 times, most recently from 8b811db to 236b137 Compare April 24, 2026 08:10
Comment thread clippy_lints/src/manual_bit_width.rs
Comment thread clippy_lints/src/manual_bit_width.rs Outdated
Comment on lines +74 to +75
ty::Uint(ty::UintTy::Usize) => Some(cx.tcx.data_layout.pointer_size().bits()),
ty::Uint(uint_ty) => uint_ty.bit_width(),

@alv-around alv-around Apr 24, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Same as in the previous comment

View changes since the review

@sorairolake

Copy link
Copy Markdown

NonZero::<T>::BITS - x.leading_zeros() returns u32, and x.bit_width() returns NonZero<u32>, where x is NonZero<T>. Therefore, these are not equivalent.

@alv-around

Copy link
Copy Markdown
Author

NonZero::<T>::BITS - x.leading_zeros() returns u32, and x.bit_width() returns NonZero<u32>, where x is NonZero<T>. Therefore, these are not equivalent.

Good catch! I remove support NonZero::<T>::BITS - x.leading_zeros() case.

@rustbot

This comment has been minimized.

@dswij dswij 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.

Sorry for the delay. Overall it looks good, just a small comment and waiting FCP.

@rustbot label lint-nominated

View changes since this review

Comment thread tests/ui/manual_bit_width.rs
@rustbot rustbot added the lint-nominated Create an FCP-thread on Zulip for this PR label Jun 10, 2026
@rustbot

rustbot commented Jun 10, 2026

Copy link
Copy Markdown
Collaborator

This lint has been nominated for inclusion.

A FCP topic has been created on Zulip.

@ada4a

ada4a commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

NonZero::<T>::BITS - x.leading_zeros() returns u32, and x.bit_width() returns NonZero<u32>, where x is NonZero<T>. Therefore, these are not equivalent.

You could solve this by suggesting x.bit_width().get() when x is a NonZero, no?

@ada4a ada4a left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Very nicely written lint:) Left some small comments

View changes since this review

Comment thread clippy_lints/src/manual_bit_width.rs Outdated
Comment thread clippy_lints/src/manual_bit_width.rs Outdated
Comment thread tests/ui/manual_bit_width.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 Jun 11, 2026
@rustbot

rustbot commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

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

@rustbot

rustbot commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@alv-around

Copy link
Copy Markdown
Author

NonZero::<T>::BITS - x.leading_zeros() returns u32, and x.bit_width() returns NonZero<u32>, where x is NonZero<T>. Therefore, these are not equivalent.

You could solve this by suggesting x.bit_width().get() when x is a NonZero, no?

@ada4a fair point, I will look into this over the week. In case this make sense I maybe put the changes on a separate PR, on top of this one. What do you think? :)

@ada4a

ada4a commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

@ada4a fair point, I will look into this over the week. In case this make sense I maybe put the changes on a separate PR, on top of this one. What do you think? :)

Hm, I'm not fundamentally opposed to the idea – a smaller PR is easier to get merged – but given that the FCP usually takes ~2 weeks anyway, we might as well use the time to bring that new pattern in, as I don't think it's very controversial.

I'd say try implementing the nonzero thing (at your own pace); if you see that that requires a large restructuring of the lint, then consider squashing that change into this PR – because it'd be silly to have a reviewer approve the code just for it to get changed right after; otherwise, feel free to leave it as a separate PR

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 S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lint suggestion: manual_bit_width

5 participants