Skip to content

Commit

Permalink
Enable clippy::expect_used, address warnings
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
jrvanwhy committed Mar 26, 2024
1 parent 77aff77 commit 23eea59
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
clippy::dbg_macro,
clippy::decimal_literal_representation,
clippy::double_must_use,
clippy::expect_used,
clippy::get_unwrap,
clippy::indexing_slicing,
clippy::missing_inline_in_public_items,
Expand Down Expand Up @@ -248,6 +249,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)]
Expand Down Expand Up @@ -1874,6 +1876,7 @@ pub unsafe trait FromZeros: TryFromBytes {
#[must_use = "has no side effects (other than allocation)"]
#[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
Expand Down Expand Up @@ -3313,10 +3316,7 @@ pub unsafe trait IntoBytes {
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(())
}

Expand Down Expand Up @@ -5445,6 +5445,7 @@ where
pub fn into_ref(self) -> &'a T {
// PANICS: By invariant on `Ref`, `self.0`'s size and alignment are
// valid for `T`, and so this `unwrap` will not panic.
#[allow(clippy::expect_used)]
let ptr = Ptr::from_ref(self.0.into())
.try_cast_into_no_leftover::<T>()
.expect("zerocopy internal error: into_ref should be infallible");
Expand All @@ -5466,6 +5467,7 @@ where
pub fn into_mut(self) -> &'a mut T {
// PANICS: By invariant on `Ref`, `self.0`'s size and alignment are
// valid for `T`, and so this `unwrap` will not panic.
#[allow(clippy::expect_used)]
let ptr = Ptr::from_mut(self.0.into())
.try_cast_into_no_leftover::<T>()
.expect("zerocopy internal error: into_ref should be infallible");
Expand Down Expand Up @@ -5546,6 +5548,7 @@ where
fn deref(&self) -> &T {
// PANICS: By invariant on `Ref`, `self.0`'s size and alignment are
// valid for `T`, and so this `unwrap` will not panic.
#[allow(clippy::expect_used)]
let ptr = Ptr::from_ref(self.0.deref())
.try_cast_into_no_leftover::<T>()
.expect("zerocopy internal error: Deref::deref should be infallible");
Expand All @@ -5563,6 +5566,7 @@ where
fn deref_mut(&mut self) -> &mut T {
// PANICS: By invariant on `Ref`, `self.0`'s size and alignment are
// valid for `T`, and so this `unwrap` will not panic.
#[allow(clippy::expect_used)]
let ptr = Ptr::from_mut(self.0.deref_mut())
.try_cast_into_no_leftover::<T>()
.expect("zerocopy internal error: DerefMut::deref_mut should be infallible");
Expand Down

0 comments on commit 23eea59

Please sign in to comment.