Skip to content

Lint bit width#16902

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

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

Conversation

@alv-around
Copy link
Copy Markdown

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

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
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 24, 2026

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

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(),
Copy link
Copy Markdown
Author

@alv-around alv-around Apr 24, 2026

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lint suggestion: manual_bit_width

4 participants