From 41d6bc6e2290d973974e3bba699faced497e121b Mon Sep 17 00:00:00 2001 From: Johnathan Van Why Date: Tue, 24 Oct 2023 14:06:37 -0700 Subject: [PATCH] Enable `clippy::expect_used`, address warnings This enables Clippy's [`expect_used`](https://rust-lang.github.io/rust-clippy/master/index.html#/expect_used) lint, which errors on calls to `Option::expect` and `Result::{expect, expect_err}`. The intent of enabling this lint is to reduce the number of panics emitted in `zerocopy`'s code (issue #202). --- src/lib.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 427f943ab4..46eff1baa6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -187,6 +187,7 @@ clippy::correctness, clippy::dbg_macro, clippy::decimal_literal_representation, + clippy::expect_used, clippy::get_unwrap, clippy::indexing_slicing, clippy::missing_inline_in_public_items, @@ -225,6 +226,7 @@ // production code introduce the possibly of code panicking unexpectedly "in // the field". clippy::arithmetic_side_effects, + clippy::expect_used, clippy::indexing_slicing, ))] #![cfg_attr(not(test), no_std)] @@ -1550,6 +1552,7 @@ pub unsafe trait FromZeros { /// * Panics if allocation of `size_of::() * len` bytes fails. #[cfg(feature = "alloc")] #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] + #[allow(clippy::expect_used)] #[inline] fn new_box_slice_zeroed(len: usize) -> Box<[Self]> where @@ -2992,10 +2995,7 @@ pub unsafe trait AsBytes { Self: NoCell, { let start = bytes.len().checked_sub(mem::size_of_val(self))?; - bytes - .get_mut(start..) - .expect("`start` should be in-bounds of `bytes`") - .copy_from_slice(self.as_bytes()); + bytes.get_mut(start..)?.copy_from_slice(self.as_bytes()); Some(()) } } @@ -4350,6 +4350,7 @@ where /// `new_slice` panics if `T` is a zero-sized type. #[inline] pub fn new_slice(bytes: B) -> Option> { + #[allow(clippy::expect_used)] let remainder = bytes .len() .checked_rem(mem::size_of::()) @@ -4874,6 +4875,7 @@ where // PANICS: By invariant, `self.0` satisfies `T`'s alignment and is a // valid size for `T`, which ensures that `try_cast_into_no_leftover` // will not panic. + #[allow(clippy::expect_used)] Ptr::from(self.0) .try_cast_into_no_leftover::() .expect("Ref::deref_unchecked: internal error (pointer cast should never fail)")