Skip to content

Lint bit width#16902

Open
alv-around wants to merge 4 commits intorust-lang:masterfrom
alv-around:lint-bit-width
Open

Lint bit width#16902
alv-around wants to merge 4 commits intorust-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 T::BITS - x.leading_zeros() for NonZero

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 on lines +66 to +67
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.

this two lines could be replaced by the function int_ty_to_nbits util function. But the functions should be move from clippy_lints/src/casts/utils.rs to clippy_utils and I was unsure whether this is desired. I think that would be a good choice overall, as ty::Uint(uint_ty) => uint_ty.bitwidth(), return None for usize.

View changes since the review

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

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

3 participants