Skip to content

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

Closed
@nindalf

Description

@nindalf

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

Metadata

Metadata

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions