Skip to content

Commit 2b56c07

Browse files
authored
Enable clippy::missing_const_for_fn (#1883)
* 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.
1 parent e7217b5 commit 2b56c07

File tree

8 files changed

+32
-35
lines changed

8 files changed

+32
-35
lines changed

src/byteorder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,7 @@ module!(network_endian, NetworkEndian, "network-endian");
967967
module!(native_endian, NativeEndian, "native-endian");
968968

969969
#[cfg(any(test, kani))]
970+
#[allow(clippy::missing_const_for_fn)]
970971
mod tests {
971972
use super::*;
972973

src/error.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ impl<Src, Dst: ?Sized> AlignmentError<Src, Dst> {
302302
AlignmentError { src: f(self.src), dst: SendSyncPhantomData::default() }
303303
}
304304

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

@@ -463,7 +463,7 @@ impl<Src, Dst: ?Sized> SizeError<Src, Dst> {
463463
}
464464

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

@@ -595,7 +595,7 @@ impl<Src, Dst: ?Sized + TryFromBytes> ValidityError<Src, Dst> {
595595
}
596596

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

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

959959
#[cfg(test)]
960+
#[allow(clippy::missing_const_for_fn)]
960961
mod tests {
961962
use super::*;
962963

src/lib.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@
248248
clippy::double_must_use,
249249
clippy::get_unwrap,
250250
clippy::indexing_slicing,
251+
clippy::missing_const_for_fn,
251252
clippy::missing_inline_in_public_items,
252253
clippy::missing_safety_doc,
253254
clippy::must_use_candidate,
@@ -3030,17 +3031,6 @@ pub unsafe trait FromZeros: TryFromBytes {
30303031
};
30313032

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

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

54005389
#[cfg(test)]
5401-
#[allow(clippy::assertions_on_result_states, clippy::unreadable_literal)]
5390+
#[allow(
5391+
clippy::assertions_on_result_states,
5392+
clippy::unreadable_literal,
5393+
clippy::missing_const_for_fn
5394+
)]
54025395
mod tests {
54035396
use static_assertions::assert_impl_all;
54045397

src/pointer/ptr.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ mod def {
103103
/// [`I::Alignment`](invariant::Alignment).
104104
/// 8. `ptr` conforms to the validity invariant of
105105
/// [`I::Validity`](invariant::Validity).
106-
pub(super) unsafe fn new(ptr: NonNull<T>) -> Ptr<'a, T, I> {
106+
pub(super) const unsafe fn new(ptr: NonNull<T>) -> Ptr<'a, T, I> {
107107
// SAFETY: The caller has promised to satisfy all safety invariants
108108
// of `Ptr`.
109109
Self { ptr, _invariants: PhantomData }
@@ -114,7 +114,7 @@ mod def {
114114
/// Note that this method does not consume `self`. The caller should
115115
/// watch out for `unsafe` code which uses the returned `NonNull` in a
116116
/// way that violates the safety invariants of `self`.
117-
pub(crate) fn as_non_null(&self) -> NonNull<T> {
117+
pub(crate) const fn as_non_null(&self) -> NonNull<T> {
118118
self.ptr
119119
}
120120
}
@@ -717,7 +717,7 @@ mod _transitions {
717717
/// # Safety
718718
///
719719
/// The caller promises that `self` satisfies the invariants `H`.
720-
unsafe fn assume_invariants<H: Invariants>(self) -> Ptr<'a, T, H> {
720+
const unsafe fn assume_invariants<H: Invariants>(self) -> Ptr<'a, T, H> {
721721
// SAFETY: The caller has promised to satisfy all parameterized
722722
// invariants of `Ptr`. `Ptr`'s other invariants are satisfied
723723
// by-contract by the source `Ptr`.
@@ -726,7 +726,7 @@ mod _transitions {
726726

727727
/// Helps the type system unify two distinct invariant types which are
728728
/// actually the same.
729-
pub(crate) fn unify_invariants<
729+
pub(crate) const fn unify_invariants<
730730
H: Invariants<Aliasing = I::Aliasing, Alignment = I::Alignment, Validity = I::Validity>,
731731
>(
732732
self,
@@ -743,7 +743,7 @@ mod _transitions {
743743
/// The caller promises that `self` satisfies the aliasing requirement
744744
/// of `A`.
745745
#[inline]
746-
pub(crate) unsafe fn assume_aliasing<A: Aliasing>(
746+
pub(crate) const unsafe fn assume_aliasing<A: Aliasing>(
747747
self,
748748
) -> Ptr<'a, T, (A, I::Alignment, I::Validity)> {
749749
// SAFETY: The caller promises that `self` satisfies the aliasing
@@ -760,7 +760,7 @@ mod _transitions {
760760
///
761761
/// [`Exclusive`]: invariant::Exclusive
762762
#[inline]
763-
pub(crate) unsafe fn assume_exclusive(
763+
pub(crate) const unsafe fn assume_exclusive(
764764
self,
765765
) -> Ptr<'a, T, (Exclusive, I::Alignment, I::Validity)> {
766766
// SAFETY: The caller promises that `self` satisfies the aliasing
@@ -776,7 +776,7 @@ mod _transitions {
776776
/// The caller promises that `self`'s referent conforms to the alignment
777777
/// invariant of `T` if required by `A`.
778778
#[inline]
779-
pub(crate) unsafe fn assume_alignment<A: Alignment>(
779+
pub(crate) const unsafe fn assume_alignment<A: Alignment>(
780780
self,
781781
) -> Ptr<'a, T, (I::Aliasing, A, I::Validity)> {
782782
// SAFETY: The caller promises that `self`'s referent is
@@ -804,7 +804,7 @@ mod _transitions {
804804
#[inline]
805805
// TODO(#859): Reconsider the name of this method before making it
806806
// public.
807-
pub(crate) fn bikeshed_recall_aligned(
807+
pub(crate) const fn bikeshed_recall_aligned(
808808
self,
809809
) -> Ptr<'a, T, (I::Aliasing, Aligned, I::Validity)>
810810
where
@@ -825,7 +825,7 @@ mod _transitions {
825825
#[doc(hidden)]
826826
#[must_use]
827827
#[inline]
828-
pub unsafe fn assume_validity<V: Validity>(
828+
pub const unsafe fn assume_validity<V: Validity>(
829829
self,
830830
) -> Ptr<'a, T, (I::Aliasing, I::Alignment, V)> {
831831
// SAFETY: The caller promises that `self`'s referent conforms to
@@ -842,7 +842,7 @@ mod _transitions {
842842
#[doc(hidden)]
843843
#[must_use]
844844
#[inline]
845-
pub unsafe fn assume_initialized(
845+
pub const unsafe fn assume_initialized(
846846
self,
847847
) -> Ptr<'a, T, (I::Aliasing, I::Alignment, Initialized)> {
848848
// SAFETY: The caller has promised to uphold the safety
@@ -859,7 +859,7 @@ mod _transitions {
859859
#[doc(hidden)]
860860
#[must_use]
861861
#[inline]
862-
pub unsafe fn assume_valid(self) -> Ptr<'a, T, (I::Aliasing, I::Alignment, Valid)> {
862+
pub const unsafe fn assume_valid(self) -> Ptr<'a, T, (I::Aliasing, I::Alignment, Valid)> {
863863
// SAFETY: The caller has promised to uphold the safety
864864
// preconditions.
865865
unsafe { self.assume_validity::<Valid>() }
@@ -871,7 +871,7 @@ mod _transitions {
871871
#[inline]
872872
// TODO(#859): Reconsider the name of this method before making it
873873
// public.
874-
pub fn bikeshed_recall_valid(self) -> Ptr<'a, T, (I::Aliasing, I::Alignment, Valid)>
874+
pub const fn bikeshed_recall_valid(self) -> Ptr<'a, T, (I::Aliasing, I::Alignment, Valid)>
875875
where
876876
T: crate::FromBytes,
877877
I: Invariants<Validity = Initialized>,
@@ -921,7 +921,7 @@ mod _transitions {
921921
#[doc(hidden)]
922922
#[must_use]
923923
#[inline]
924-
pub fn forget_exclusive(self) -> Ptr<'a, T, (Shared, I::Alignment, I::Validity)>
924+
pub const fn forget_exclusive(self) -> Ptr<'a, T, (Shared, I::Alignment, I::Validity)>
925925
where
926926
I::Aliasing: AtLeast<Shared>,
927927
{
@@ -933,7 +933,7 @@ mod _transitions {
933933
#[doc(hidden)]
934934
#[must_use]
935935
#[inline]
936-
pub fn forget_aligned(self) -> Ptr<'a, T, (I::Aliasing, Any, I::Validity)> {
936+
pub const fn forget_aligned(self) -> Ptr<'a, T, (I::Aliasing, Any, I::Validity)> {
937937
// SAFETY: `Any` is less restrictive than `Aligned`.
938938
unsafe { self.assume_invariants() }
939939
}
@@ -1641,6 +1641,7 @@ mod _project {
16411641
}
16421642

16431643
#[cfg(test)]
1644+
#[allow(clippy::missing_const_for_fn)]
16441645
mod tests {
16451646
use core::mem::{self, MaybeUninit};
16461647

src/ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ mod def {
7575
/// [`deref`]: core::ops::Deref::deref
7676
/// [`deref_mut`]: core::ops::DerefMut::deref_mut
7777
/// [`into`]: core::convert::Into::into
78-
pub(crate) unsafe fn new_unchecked(bytes: B) -> Ref<B, T> {
78+
pub(crate) const unsafe fn new_unchecked(bytes: B) -> Ref<B, T> {
7979
// INVARIANTS: The caller has promised that `bytes`'s referent is
8080
// validly-aligned and has a valid size.
8181
Ref(bytes, PhantomData)

src/util/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ macro_rules! impl_for_transparent_wrapper {
255255
impl_for_transparent_wrapper!(@define_is_transparent_wrapper $trait);
256256

257257
#[cfg_attr(coverage_nightly, coverage(off))]
258-
fn f<I: Invariants, $($tyvar $(: $(? $optbound +)* $($bound +)*)?)?>() {
258+
const fn f<I: Invariants, $($tyvar $(: $(? $optbound +)* $($bound +)*)?)?>() {
259259
is_transparent_wrapper::<I, $ty>();
260260
}
261261
}
@@ -327,7 +327,7 @@ macro_rules! impl_for_transparent_wrapper {
327327
};
328328
(@define_is_transparent_wrapper $trait:ident, $variance:ident) => {
329329
#[cfg_attr(coverage_nightly, coverage(off))]
330-
fn is_transparent_wrapper<I: Invariants, W: TransparentWrapper<I, $variance = Covariant> + ?Sized>()
330+
const fn is_transparent_wrapper<I: Invariants, W: TransparentWrapper<I, $variance = Covariant> + ?Sized>()
331331
where
332332
W::Inner: $trait,
333333
{}

src/util/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ pub(crate) mod polyfills {
756756
}
757757

758758
#[cfg(test)]
759+
#[allow(clippy::missing_const_for_fn)]
759760
pub(crate) mod testutil {
760761
use crate::*;
761762

@@ -845,6 +846,7 @@ pub(crate) mod testutil {
845846
}
846847

847848
#[cfg(test)]
849+
#[allow(clippy::missing_const_for_fn)]
848850
mod tests {
849851
use super::*;
850852

src/wrappers.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,8 @@ impl<T> Unalign<T> {
393393

394394
impl<T: Copy> Unalign<T> {
395395
/// Gets a copy of the inner `T`.
396-
// TODO(https://github.com/rust-lang/rust/issues/57349): Make this `const`.
397396
#[inline(always)]
398-
pub fn get(&self) -> T {
397+
pub const fn get(&self) -> T {
399398
let Unalign(val) = *self;
400399
val
401400
}

0 commit comments

Comments
 (0)