Skip to content

Suggest size_of_val(a) for a.len() * size_of::<T>() #10518

Closed
@scottmcm

Description

@scottmcm

What it does

When a is &[T], detect a.len() * size_of::<T>() and suggest size_of_val(a) instead.

Lint Name

manual_slice_size_calculation

Category

complexity, perf

Advantage

  • Shorter to write
  • Removes the need for the human and the compiler to worry about overflow in the multiplication
  • Potentially faster at runtime as rust emits special no-wrapping flags when it calculates the byte length
  • Less turbofishing

Drawbacks

No response

Example

https://github.com/rust-lang/rust/blob/13afbdaa0655dda23d7129e59ac48f1ec88b2084/library/core/src/hash/mod.rs#L837

let newlen = data.len() * mem::size_of::<$ty>();

Could be written as:

let newlen = mem::size_of_val(data);

Metadata

Metadata

Assignees

Labels

A-lintArea: New lintsgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions