Use size_of from the prelude instead of imported

Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the
prelude instead of importing or qualifying them.

These functions were added to all preludes in Rust 1.80.
This commit is contained in:
Thalia Archibald 2025-03-04 20:28:38 -08:00
parent 9538e5bcd0
commit e8cd3d4752
6 changed files with 46 additions and 47 deletions

View file

@ -14,7 +14,7 @@ use rustc_span::{Span, sym};
declare_clippy_lint! {
/// ### What it does
/// Checks for usage of `std::mem::size_of::<T>() * 8` when
/// Checks for usage of `size_of::<T>() * 8` when
/// `T::BITS` is available.
///
/// ### Why is this bad?
@ -22,7 +22,7 @@ declare_clippy_lint! {
///
/// ### Example
/// ```no_run
/// std::mem::size_of::<usize>() * 8;
/// size_of::<usize>() * 8;
/// ```
/// Use instead:
/// ```no_run
@ -68,7 +68,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualBits {
cx,
MANUAL_BITS,
expr.span,
"usage of `mem::size_of::<T>()` to obtain the size of `T` in bits",
"usage of `size_of::<T>()` to obtain the size of `T` in bits",
"consider using",
sugg,
app,

View file

@ -24,12 +24,12 @@ declare_clippy_lint! {
/// ### Example
/// ```no_run
/// # let data : &[i32] = &[1, 2, 3];
/// let newlen = data.len() * std::mem::size_of::<i32>();
/// let newlen = data.len() * size_of::<i32>();
/// ```
/// Use instead:
/// ```no_run
/// # let data : &[i32] = &[1, 2, 3];
/// let newlen = std::mem::size_of_val(data);
/// let newlen = size_of_val(data);
/// ```
#[clippy::version = "1.70.0"]
pub MANUAL_SLICE_SIZE_CALCULATION,

View file

@ -18,7 +18,6 @@ declare_clippy_lint! {
/// ### Example
/// ```rust,no_run
/// # use std::ptr::copy_nonoverlapping;
/// # use std::mem::size_of;
/// const SIZE: usize = 128;
/// let x = [2u8; SIZE];
/// let mut y = [2u8; SIZE];

View file

@ -8,7 +8,7 @@ use rustc_span::sym;
declare_clippy_lint! {
/// ### What it does
///
/// Checks for calls to `std::mem::size_of_val()` where the argument is
/// Checks for calls to `size_of_val()` where the argument is
/// a reference to a reference.
///
/// ### Why is this bad?
@ -29,7 +29,7 @@ declare_clippy_lint! {
/// // is already a reference, `&self` is a double-reference.
/// // The return value of `size_of_val()` therefore is the
/// // size of the reference-type, not the size of `self`.
/// std::mem::size_of_val(&self)
/// size_of_val(&self)
/// }
/// }
/// ```
@ -42,14 +42,14 @@ declare_clippy_lint! {
/// impl Foo {
/// fn size(&self) -> usize {
/// // Correct
/// std::mem::size_of_val(self)
/// size_of_val(self)
/// }
/// }
/// ```
#[clippy::version = "1.68.0"]
pub SIZE_OF_REF,
suspicious,
"Argument to `std::mem::size_of_val()` is a double-reference, which is almost certainly unintended"
"Argument to `size_of_val()` is a double-reference, which is almost certainly unintended"
}
declare_lint_pass!(SizeOfRef => [SIZE_OF_REF]);
@ -65,9 +65,9 @@ impl LateLintPass<'_> for SizeOfRef {
cx,
SIZE_OF_REF,
expr.span,
"argument to `std::mem::size_of_val()` is a reference to a reference",
"argument to `size_of_val()` is a reference to a reference",
None,
"dereference the argument to `std::mem::size_of_val()` to get the size of the value instead of the size of the reference-type",
"dereference the argument to `size_of_val()` to get the size of the value instead of the size of the reference-type",
);
}
}