Skip to content

Commit 23eea59

Browse files
committed
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).
1 parent 77aff77 commit 23eea59

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/lib.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
clippy::dbg_macro,
207207
clippy::decimal_literal_representation,
208208
clippy::double_must_use,
209+
clippy::expect_used,
209210
clippy::get_unwrap,
210211
clippy::indexing_slicing,
211212
clippy::missing_inline_in_public_items,
@@ -248,6 +249,7 @@
248249
// production code introduce the possibly of code panicking unexpectedly "in
249250
// the field".
250251
clippy::arithmetic_side_effects,
252+
clippy::expect_used,
251253
clippy::indexing_slicing,
252254
))]
253255
#![cfg_attr(not(test), no_std)]
@@ -1874,6 +1876,7 @@ pub unsafe trait FromZeros: TryFromBytes {
18741876
#[must_use = "has no side effects (other than allocation)"]
18751877
#[cfg(feature = "alloc")]
18761878
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
1879+
#[allow(clippy::expect_used)]
18771880
#[inline]
18781881
fn new_box_slice_zeroed(len: usize) -> Box<[Self]>
18791882
where
@@ -3313,10 +3316,7 @@ pub unsafe trait IntoBytes {
33133316
Self: NoCell,
33143317
{
33153318
let start = bytes.len().checked_sub(mem::size_of_val(self))?;
3316-
bytes
3317-
.get_mut(start..)
3318-
.expect("`start` should be in-bounds of `bytes`")
3319-
.copy_from_slice(self.as_bytes());
3319+
bytes.get_mut(start..)?.copy_from_slice(self.as_bytes());
33203320
Some(())
33213321
}
33223322

@@ -5445,6 +5445,7 @@ where
54455445
pub fn into_ref(self) -> &'a T {
54465446
// PANICS: By invariant on `Ref`, `self.0`'s size and alignment are
54475447
// valid for `T`, and so this `unwrap` will not panic.
5448+
#[allow(clippy::expect_used)]
54485449
let ptr = Ptr::from_ref(self.0.into())
54495450
.try_cast_into_no_leftover::<T>()
54505451
.expect("zerocopy internal error: into_ref should be infallible");
@@ -5466,6 +5467,7 @@ where
54665467
pub fn into_mut(self) -> &'a mut T {
54675468
// PANICS: By invariant on `Ref`, `self.0`'s size and alignment are
54685469
// valid for `T`, and so this `unwrap` will not panic.
5470+
#[allow(clippy::expect_used)]
54695471
let ptr = Ptr::from_mut(self.0.into())
54705472
.try_cast_into_no_leftover::<T>()
54715473
.expect("zerocopy internal error: into_ref should be infallible");
@@ -5546,6 +5548,7 @@ where
55465548
fn deref(&self) -> &T {
55475549
// PANICS: By invariant on `Ref`, `self.0`'s size and alignment are
55485550
// valid for `T`, and so this `unwrap` will not panic.
5551+
#[allow(clippy::expect_used)]
55495552
let ptr = Ptr::from_ref(self.0.deref())
55505553
.try_cast_into_no_leftover::<T>()
55515554
.expect("zerocopy internal error: Deref::deref should be infallible");
@@ -5563,6 +5566,7 @@ where
55635566
fn deref_mut(&mut self) -> &mut T {
55645567
// PANICS: By invariant on `Ref`, `self.0`'s size and alignment are
55655568
// valid for `T`, and so this `unwrap` will not panic.
5569+
#[allow(clippy::expect_used)]
55665570
let ptr = Ptr::from_mut(self.0.deref_mut())
55675571
.try_cast_into_no_leftover::<T>()
55685572
.expect("zerocopy internal error: DerefMut::deref_mut should be infallible");

0 commit comments

Comments
 (0)