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 0aa623e108
commit 0811ca0a61
11 changed files with 24 additions and 34 deletions

View file

@ -260,7 +260,7 @@ pub enum TypeBound {
} }
#[cfg(target_pointer_width = "64")] #[cfg(target_pointer_width = "64")]
const _: [(); 24] = [(); ::std::mem::size_of::<TypeBound>()]; const _: [(); 24] = [(); size_of::<TypeBound>()];
#[derive(Clone, PartialEq, Eq, Hash, Debug)] #[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum UseArgRef { pub enum UseArgRef {

View file

@ -2713,12 +2713,11 @@ fn const_trait_assoc() {
r#" r#"
//- minicore: size_of, fn //- minicore: size_of, fn
//- /a/lib.rs crate:a //- /a/lib.rs crate:a
use core::mem::size_of;
pub struct S<T>(T); pub struct S<T>(T);
impl<T> S<T> { impl<T> S<T> {
pub const X: usize = { pub const X: usize = {
let k: T; let k: T;
let f = || core::mem::size_of::<T>(); let f = || size_of::<T>();
f() f()
}; };
} }

View file

@ -4,7 +4,7 @@
use std::{ use std::{
fmt::{self, Debug}, fmt::{self, Debug},
mem::{self, size_of}, mem,
}; };
use base_db::CrateId; use base_db::CrateId;

View file

@ -302,7 +302,7 @@ impl Address {
} }
} }
fn to_bytes(&self) -> [u8; mem::size_of::<usize>()] { fn to_bytes(&self) -> [u8; size_of::<usize>()] {
usize::to_le_bytes(self.to_usize()) usize::to_le_bytes(self.to_usize())
} }
@ -589,7 +589,7 @@ pub fn interpret_mir(
let ty = body.locals[return_slot()].ty.clone(); let ty = body.locals[return_slot()].ty.clone();
let mut evaluator = Evaluator::new(db, body.owner, assert_placeholder_ty_is_unused, trait_env)?; let mut evaluator = Evaluator::new(db, body.owner, assert_placeholder_ty_is_unused, trait_env)?;
let it: Result<Const> = (|| { let it: Result<Const> = (|| {
if evaluator.ptr_size() != std::mem::size_of::<usize>() { if evaluator.ptr_size() != size_of::<usize>() {
not_supported!("targets with different pointer size from host"); not_supported!("targets with different pointer size from host");
} }
let interval = evaluator.interpret_mir(body.clone(), None.into_iter())?; let interval = evaluator.interpret_mir(body.clone(), None.into_iter())?;

View file

@ -1394,7 +1394,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
.layout_of_ty(ty.clone(), self.db.trait_environment_for_body(self.owner)) .layout_of_ty(ty.clone(), self.db.trait_environment_for_body(self.owner))
.map(|it| it.size.bytes_usize()) .map(|it| it.size.bytes_usize())
}; };
const USIZE_SIZE: usize = mem::size_of::<usize>(); const USIZE_SIZE: usize = size_of::<usize>();
let bytes: Box<[_]> = match l { let bytes: Box<[_]> = match l {
hir_def::hir::Literal::String(b) => { hir_def::hir::Literal::String(b) => {
let b = b.as_str(); let b = b.as_str();

View file

@ -15086,7 +15086,7 @@ cannot be represented as the underlying type without loss."##,
}, },
Lint { Lint {
label: "clippy::manual_bits", label: "clippy::manual_bits",
description: r##"Checks for usage of `std::mem::size_of::<T>() * 8` when description: r##"Checks for usage of `size_of::<T>() * 8` when
`T::BITS` is available."##, `T::BITS` is available."##,
default_severity: Severity::Allow, default_severity: Severity::Allow,
warn_since: None, warn_since: None,
@ -17428,7 +17428,7 @@ count of elements of type `T`"##,
}, },
Lint { Lint {
label: "clippy::size_of_ref", label: "clippy::size_of_ref",
description: r##"Checks for calls to `std::mem::size_of_val()` where the argument is description: r##"Checks for calls to `size_of_val()` where the argument is
a reference to a reference."##, a reference to a reference."##,
default_severity: Severity::Allow, default_severity: Severity::Allow,
warn_since: None, warn_since: None,

View file

@ -24,7 +24,6 @@ use std::{
cmp::Ordering, cmp::Ordering,
fmt, fmt,
hash::{Hash, Hasher}, hash::{Hash, Hasher},
mem,
ops::ControlFlow, ops::ControlFlow,
}; };
@ -299,7 +298,7 @@ impl SymbolIndex {
} }
pub fn memory_size(&self) -> usize { pub fn memory_size(&self) -> usize {
self.map.as_fst().size() + self.symbols.len() * mem::size_of::<FileSymbol>() self.map.as_fst().size() + self.symbols.len() * size_of::<FileSymbol>()
} }
fn range_to_map_value(start: usize, end: usize) -> u64 { fn range_to_map_value(start: usize, end: usize) -> u64 {

View file

@ -268,8 +268,7 @@ struct AttrsStats {
impl fmt::Display for AttrsStats { impl fmt::Display for AttrsStats {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let size = let size = self.entries * size_of::<Attrs>() + self.total * size_of::<Attr>();
self.entries * std::mem::size_of::<Attrs>() + self.total * std::mem::size_of::<Attr>();
let size = Bytes::new(size as _); let size = Bytes::new(size as _);
write!( write!(
fmt, fmt,

View file

@ -18,20 +18,17 @@ use triomphe::Arc;
pub mod symbols; pub mod symbols;
// some asserts for layout compatibility // some asserts for layout compatibility
const _: () = assert!(std::mem::size_of::<Box<str>>() == std::mem::size_of::<&str>()); const _: () = assert!(size_of::<Box<str>>() == size_of::<&str>());
const _: () = assert!(std::mem::align_of::<Box<str>>() == std::mem::align_of::<&str>()); const _: () = assert!(align_of::<Box<str>>() == align_of::<&str>());
const _: () = assert!(std::mem::size_of::<Arc<Box<str>>>() == std::mem::size_of::<&&str>()); const _: () = assert!(size_of::<Arc<Box<str>>>() == size_of::<&&str>());
const _: () = assert!(std::mem::align_of::<Arc<Box<str>>>() == std::mem::align_of::<&&str>()); const _: () = assert!(align_of::<Arc<Box<str>>>() == align_of::<&&str>());
const _: () = const _: () = assert!(size_of::<*const *const str>() == size_of::<TaggedArcPtr>());
assert!(std::mem::size_of::<*const *const str>() == std::mem::size_of::<TaggedArcPtr>()); const _: () = assert!(align_of::<*const *const str>() == align_of::<TaggedArcPtr>());
const _: () =
assert!(std::mem::align_of::<*const *const str>() == std::mem::align_of::<TaggedArcPtr>());
const _: () = assert!(std::mem::size_of::<Arc<Box<str>>>() == std::mem::size_of::<TaggedArcPtr>()); const _: () = assert!(size_of::<Arc<Box<str>>>() == size_of::<TaggedArcPtr>());
const _: () = const _: () = assert!(align_of::<Arc<Box<str>>>() == align_of::<TaggedArcPtr>());
assert!(std::mem::align_of::<Arc<Box<str>>>() == std::mem::align_of::<TaggedArcPtr>());
/// A pointer that points to a pointer to a `str`, it may be backed as a `&'static &'static str` or /// A pointer that points to a pointer to a `str`, it may be backed as a `&'static &'static str` or
/// `Arc<Box<str>>` but its size is that of a thin pointer. The active variant is encoded as a tag /// `Arc<Box<str>>` but its size is that of a thin pointer. The active variant is encoded as a tag
@ -49,9 +46,7 @@ impl TaggedArcPtr {
const BOOL_BITS: usize = true as usize; const BOOL_BITS: usize = true as usize;
const fn non_arc(r: &'static &'static str) -> Self { const fn non_arc(r: &'static &'static str) -> Self {
assert!( assert!(align_of::<&'static &'static str>().trailing_zeros() as usize > Self::BOOL_BITS);
mem::align_of::<&'static &'static str>().trailing_zeros() as usize > Self::BOOL_BITS
);
// SAFETY: The pointer is non-null as it is derived from a reference // SAFETY: The pointer is non-null as it is derived from a reference
// Ideally we would call out to `pack_arc` but for a `false` tag, unfortunately the // Ideally we would call out to `pack_arc` but for a `false` tag, unfortunately the
// packing stuff requires reading out the pointer to an integer which is not supported // packing stuff requires reading out the pointer to an integer which is not supported
@ -64,9 +59,7 @@ impl TaggedArcPtr {
} }
fn arc(arc: Arc<Box<str>>) -> Self { fn arc(arc: Arc<Box<str>>) -> Self {
assert!( assert!(align_of::<&'static &'static str>().trailing_zeros() as usize > Self::BOOL_BITS);
mem::align_of::<&'static &'static str>().trailing_zeros() as usize > Self::BOOL_BITS
);
Self { Self {
packed: Self::pack_arc( packed: Self::pack_arc(
// Safety: `Arc::into_raw` always returns a non null pointer // Safety: `Arc::into_raw` always returns a non null pointer
@ -131,8 +124,8 @@ impl fmt::Debug for Symbol {
} }
} }
const _: () = assert!(std::mem::size_of::<Symbol>() == std::mem::size_of::<NonNull<()>>()); const _: () = assert!(size_of::<Symbol>() == size_of::<NonNull<()>>());
const _: () = assert!(std::mem::align_of::<Symbol>() == std::mem::align_of::<NonNull<()>>()); const _: () = assert!(align_of::<Symbol>() == align_of::<NonNull<()>>());
static MAP: OnceLock<DashMap<SymbolProxy, (), BuildHasherDefault<FxHasher>>> = OnceLock::new(); static MAP: OnceLock<DashMap<SymbolProxy, (), BuildHasherDefault<FxHasher>>> = OnceLock::new();

View file

@ -38,7 +38,7 @@ impl MemoryUsage {
// approximate that by using the Commit Charge value. // approximate that by using the Commit Charge value.
use windows_sys::Win32::System::{Threading::*, ProcessStatus::*}; use windows_sys::Win32::System::{Threading::*, ProcessStatus::*};
use std::mem::{MaybeUninit, size_of}; use std::mem::MaybeUninit;
let proc = unsafe { GetCurrentProcess() }; let proc = unsafe { GetCurrentProcess() };
let mut mem_counters = MaybeUninit::uninit(); let mut mem_counters = MaybeUninit::uninit();

View file

@ -1825,7 +1825,7 @@ macro_rules! impl_int {
($($t:ty)*) => { ($($t:ty)*) => {
$( $(
impl $t { impl $t {
pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self { pub const fn from_ne_bytes(bytes: [u8; size_of::<Self>()]) -> Self {
unsafe { mem::transmute(bytes) } unsafe { mem::transmute(bytes) }
} }
} }