Skip to content

Commit

Permalink
Enable clippy::missing_const_for_fn (#1883)
Browse files Browse the repository at this point in the history
* Release 0.9.0-alpha.0

Upgrade our MSRV to 1.65 and remove version detection logic prior to
that version.

* Enable clippy::missing_const_for_fn

While we're here, remove defensive programming against bug in
`Layout::from_size_align` which is no longer needed on our new MSRV.
  • Loading branch information
joshlf authored Oct 12, 2024
1 parent e7217b5 commit 2b56c07
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/byteorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@ module!(network_endian, NetworkEndian, "network-endian");
module!(native_endian, NativeEndian, "native-endian");

#[cfg(any(test, kani))]
#[allow(clippy::missing_const_for_fn)]
mod tests {
use super::*;

Expand Down
7 changes: 4 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl<Src, Dst: ?Sized> AlignmentError<Src, Dst> {
AlignmentError { src: f(self.src), dst: SendSyncPhantomData::default() }
}

pub(crate) fn into<S, V>(self) -> ConvertError<Self, S, V> {
pub(crate) const fn into<S, V>(self) -> ConvertError<Self, S, V> {
ConvertError::Alignment(self)
}

Expand Down Expand Up @@ -463,7 +463,7 @@ impl<Src, Dst: ?Sized> SizeError<Src, Dst> {
}

/// Converts the error into a general [`ConvertError`].
pub(crate) fn into<A, V>(self) -> ConvertError<A, Self, V> {
pub(crate) const fn into<A, V>(self) -> ConvertError<A, Self, V> {
ConvertError::Size(self)
}

Expand Down Expand Up @@ -595,7 +595,7 @@ impl<Src, Dst: ?Sized + TryFromBytes> ValidityError<Src, Dst> {
}

/// Converts the error into a general [`ConvertError`].
pub(crate) fn into<A, S>(self) -> ConvertError<A, S, Self> {
pub(crate) const fn into<A, S>(self) -> ConvertError<A, S, Self> {
ConvertError::Validity(self)
}

Expand Down Expand Up @@ -957,6 +957,7 @@ pub type AlignedTryCastError<Src, Dst: ?Sized + TryFromBytes> =
pub struct AllocError;

#[cfg(test)]
#[allow(clippy::missing_const_for_fn)]
mod tests {
use super::*;

Expand Down
19 changes: 6 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@
clippy::double_must_use,
clippy::get_unwrap,
clippy::indexing_slicing,
clippy::missing_const_for_fn,
clippy::missing_inline_in_public_items,
clippy::missing_safety_doc,
clippy::must_use_candidate,
Expand Down Expand Up @@ -3030,17 +3031,6 @@ pub unsafe trait FromZeros: TryFromBytes {
};

let align = Self::LAYOUT.align.get();
// On stable Rust versions <= 1.64.0, `Layout::from_size_align` has a
// bug in which sufficiently-large allocations (those which, when
// rounded up to the alignment, overflow `isize`) are not rejected,
// which can cause undefined behavior. See #64 for details.
//
// TODO(#67): Once our MSRV is > 1.64.0, remove this assertion.
#[allow(clippy::as_conversions)]
let max_alloc = (isize::MAX as usize).saturating_sub(align);
if size > max_alloc {
return Err(AllocError);
}

// TODO(https://github.com/rust-lang/rust/issues/55724): Use
// `Layout::repeat` once it's stabilized.
Expand All @@ -3055,7 +3045,6 @@ pub unsafe trait FromZeros: TryFromBytes {
None => return Err(AllocError),
}
} else {
let align = Self::LAYOUT.align.get();
// We use `transmute` instead of an `as` cast since Miri (with
// strict provenance enabled) notices and complains that an `as`
// cast creates a pointer with no provenance. Miri isn't smart
Expand Down Expand Up @@ -5398,7 +5387,11 @@ mod alloc_support {
pub use alloc_support::*;

#[cfg(test)]
#[allow(clippy::assertions_on_result_states, clippy::unreadable_literal)]
#[allow(
clippy::assertions_on_result_states,
clippy::unreadable_literal,
clippy::missing_const_for_fn
)]
mod tests {
use static_assertions::assert_impl_all;

Expand Down
29 changes: 15 additions & 14 deletions src/pointer/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ mod def {
/// [`I::Alignment`](invariant::Alignment).
/// 8. `ptr` conforms to the validity invariant of
/// [`I::Validity`](invariant::Validity).
pub(super) unsafe fn new(ptr: NonNull<T>) -> Ptr<'a, T, I> {
pub(super) const unsafe fn new(ptr: NonNull<T>) -> Ptr<'a, T, I> {
// SAFETY: The caller has promised to satisfy all safety invariants
// of `Ptr`.
Self { ptr, _invariants: PhantomData }
Expand All @@ -114,7 +114,7 @@ mod def {
/// Note that this method does not consume `self`. The caller should
/// watch out for `unsafe` code which uses the returned `NonNull` in a
/// way that violates the safety invariants of `self`.
pub(crate) fn as_non_null(&self) -> NonNull<T> {
pub(crate) const fn as_non_null(&self) -> NonNull<T> {
self.ptr
}
}
Expand Down Expand Up @@ -717,7 +717,7 @@ mod _transitions {
/// # Safety
///
/// The caller promises that `self` satisfies the invariants `H`.
unsafe fn assume_invariants<H: Invariants>(self) -> Ptr<'a, T, H> {
const unsafe fn assume_invariants<H: Invariants>(self) -> Ptr<'a, T, H> {
// SAFETY: The caller has promised to satisfy all parameterized
// invariants of `Ptr`. `Ptr`'s other invariants are satisfied
// by-contract by the source `Ptr`.
Expand All @@ -726,7 +726,7 @@ mod _transitions {

/// Helps the type system unify two distinct invariant types which are
/// actually the same.
pub(crate) fn unify_invariants<
pub(crate) const fn unify_invariants<
H: Invariants<Aliasing = I::Aliasing, Alignment = I::Alignment, Validity = I::Validity>,
>(
self,
Expand All @@ -743,7 +743,7 @@ mod _transitions {
/// The caller promises that `self` satisfies the aliasing requirement
/// of `A`.
#[inline]
pub(crate) unsafe fn assume_aliasing<A: Aliasing>(
pub(crate) const unsafe fn assume_aliasing<A: Aliasing>(
self,
) -> Ptr<'a, T, (A, I::Alignment, I::Validity)> {
// SAFETY: The caller promises that `self` satisfies the aliasing
Expand All @@ -760,7 +760,7 @@ mod _transitions {
///
/// [`Exclusive`]: invariant::Exclusive
#[inline]
pub(crate) unsafe fn assume_exclusive(
pub(crate) const unsafe fn assume_exclusive(
self,
) -> Ptr<'a, T, (Exclusive, I::Alignment, I::Validity)> {
// SAFETY: The caller promises that `self` satisfies the aliasing
Expand All @@ -776,7 +776,7 @@ mod _transitions {
/// The caller promises that `self`'s referent conforms to the alignment
/// invariant of `T` if required by `A`.
#[inline]
pub(crate) unsafe fn assume_alignment<A: Alignment>(
pub(crate) const unsafe fn assume_alignment<A: Alignment>(
self,
) -> Ptr<'a, T, (I::Aliasing, A, I::Validity)> {
// SAFETY: The caller promises that `self`'s referent is
Expand Down Expand Up @@ -804,7 +804,7 @@ mod _transitions {
#[inline]
// TODO(#859): Reconsider the name of this method before making it
// public.
pub(crate) fn bikeshed_recall_aligned(
pub(crate) const fn bikeshed_recall_aligned(
self,
) -> Ptr<'a, T, (I::Aliasing, Aligned, I::Validity)>
where
Expand All @@ -825,7 +825,7 @@ mod _transitions {
#[doc(hidden)]
#[must_use]
#[inline]
pub unsafe fn assume_validity<V: Validity>(
pub const unsafe fn assume_validity<V: Validity>(
self,
) -> Ptr<'a, T, (I::Aliasing, I::Alignment, V)> {
// SAFETY: The caller promises that `self`'s referent conforms to
Expand All @@ -842,7 +842,7 @@ mod _transitions {
#[doc(hidden)]
#[must_use]
#[inline]
pub unsafe fn assume_initialized(
pub const unsafe fn assume_initialized(
self,
) -> Ptr<'a, T, (I::Aliasing, I::Alignment, Initialized)> {
// SAFETY: The caller has promised to uphold the safety
Expand All @@ -859,7 +859,7 @@ mod _transitions {
#[doc(hidden)]
#[must_use]
#[inline]
pub unsafe fn assume_valid(self) -> Ptr<'a, T, (I::Aliasing, I::Alignment, Valid)> {
pub const unsafe fn assume_valid(self) -> Ptr<'a, T, (I::Aliasing, I::Alignment, Valid)> {
// SAFETY: The caller has promised to uphold the safety
// preconditions.
unsafe { self.assume_validity::<Valid>() }
Expand All @@ -871,7 +871,7 @@ mod _transitions {
#[inline]
// TODO(#859): Reconsider the name of this method before making it
// public.
pub fn bikeshed_recall_valid(self) -> Ptr<'a, T, (I::Aliasing, I::Alignment, Valid)>
pub const fn bikeshed_recall_valid(self) -> Ptr<'a, T, (I::Aliasing, I::Alignment, Valid)>
where
T: crate::FromBytes,
I: Invariants<Validity = Initialized>,
Expand Down Expand Up @@ -921,7 +921,7 @@ mod _transitions {
#[doc(hidden)]
#[must_use]
#[inline]
pub fn forget_exclusive(self) -> Ptr<'a, T, (Shared, I::Alignment, I::Validity)>
pub const fn forget_exclusive(self) -> Ptr<'a, T, (Shared, I::Alignment, I::Validity)>
where
I::Aliasing: AtLeast<Shared>,
{
Expand All @@ -933,7 +933,7 @@ mod _transitions {
#[doc(hidden)]
#[must_use]
#[inline]
pub fn forget_aligned(self) -> Ptr<'a, T, (I::Aliasing, Any, I::Validity)> {
pub const fn forget_aligned(self) -> Ptr<'a, T, (I::Aliasing, Any, I::Validity)> {
// SAFETY: `Any` is less restrictive than `Aligned`.
unsafe { self.assume_invariants() }
}
Expand Down Expand Up @@ -1641,6 +1641,7 @@ mod _project {
}

#[cfg(test)]
#[allow(clippy::missing_const_for_fn)]
mod tests {
use core::mem::{self, MaybeUninit};

Expand Down
2 changes: 1 addition & 1 deletion src/ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ mod def {
/// [`deref`]: core::ops::Deref::deref
/// [`deref_mut`]: core::ops::DerefMut::deref_mut
/// [`into`]: core::convert::Into::into
pub(crate) unsafe fn new_unchecked(bytes: B) -> Ref<B, T> {
pub(crate) const unsafe fn new_unchecked(bytes: B) -> Ref<B, T> {
// INVARIANTS: The caller has promised that `bytes`'s referent is
// validly-aligned and has a valid size.
Ref(bytes, PhantomData)
Expand Down
4 changes: 2 additions & 2 deletions src/util/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ macro_rules! impl_for_transparent_wrapper {
impl_for_transparent_wrapper!(@define_is_transparent_wrapper $trait);

#[cfg_attr(coverage_nightly, coverage(off))]
fn f<I: Invariants, $($tyvar $(: $(? $optbound +)* $($bound +)*)?)?>() {
const fn f<I: Invariants, $($tyvar $(: $(? $optbound +)* $($bound +)*)?)?>() {
is_transparent_wrapper::<I, $ty>();
}
}
Expand Down Expand Up @@ -327,7 +327,7 @@ macro_rules! impl_for_transparent_wrapper {
};
(@define_is_transparent_wrapper $trait:ident, $variance:ident) => {
#[cfg_attr(coverage_nightly, coverage(off))]
fn is_transparent_wrapper<I: Invariants, W: TransparentWrapper<I, $variance = Covariant> + ?Sized>()
const fn is_transparent_wrapper<I: Invariants, W: TransparentWrapper<I, $variance = Covariant> + ?Sized>()
where
W::Inner: $trait,
{}
Expand Down
2 changes: 2 additions & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@ pub(crate) mod polyfills {
}

#[cfg(test)]
#[allow(clippy::missing_const_for_fn)]
pub(crate) mod testutil {
use crate::*;

Expand Down Expand Up @@ -845,6 +846,7 @@ pub(crate) mod testutil {
}

#[cfg(test)]
#[allow(clippy::missing_const_for_fn)]
mod tests {
use super::*;

Expand Down
3 changes: 1 addition & 2 deletions src/wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,8 @@ impl<T> Unalign<T> {

impl<T: Copy> Unalign<T> {
/// Gets a copy of the inner `T`.
// TODO(https://github.com/rust-lang/rust/issues/57349): Make this `const`.
#[inline(always)]
pub fn get(&self) -> T {
pub const fn get(&self) -> T {
let Unalign(val) = *self;
val
}
Expand Down

0 comments on commit 2b56c07

Please sign in to comment.