Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace std::mem::size_of<T>() with ::BITS constant #6670

Closed
nindalf opened this issue Feb 3, 2021 · 3 comments · Fixed by #8213
Closed

Replace std::mem::size_of<T>() with ::BITS constant #6670

nindalf opened this issue Feb 3, 2021 · 3 comments · Fixed by #8213
Assignees
Labels
A-lint Area: New lints

Comments

@nindalf
Copy link
Contributor

nindalf commented Feb 3, 2021

What it does

Starting in Rust 1.51, all integer types will have a ::BITS constant for the number of bits in the type. std::mem::size_of::<usize>() * 8 can be replaced with usize::BITS and similarly for all signed and unisigned integers. This lint suggests such a change.

Categories (optional)

  • Kind: clippy::style

What is the advantage of the recommended code over the original code

For example:

  • One fewer function call (saving several nanoseconds)
  • Easier to read

Drawbacks

None.

Example

std::mem::size_of::<usize>() * 8

Could be written as:

usize::BITS
@nindalf nindalf added the A-lint Area: New lints label Feb 3, 2021
@m-ou-se
Copy link
Member

m-ou-se commented Feb 3, 2021

Starting in Rust 1.51

Note that we're reverting stabilization because of some breakage: rust-lang/rust#81727. So this will probably be 1.52 rather than 1.51.

@nindalf
Copy link
Contributor Author

nindalf commented Feb 4, 2021

That sounds fine.

I’d be happy to work on this issue. Does anyone know what’s the deadline to make it for the Clippy release of 1.51.0 or 1.52.0?

@paolobarbolini
Copy link
Contributor

I'd like to work on this issue

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants