diff --git a/src/lib.rs b/src/lib.rs index b6013a844c..a30c82c470 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,7 +46,7 @@ //! //! [simd-layout]: https://rust-lang.github.io/unsafe-code-guidelines/layout/packed-simd-vectors.html -#![deny(missing_docs)] +#![deny(missing_docs, clippy::indexing_slicing)] #![cfg_attr(not(test), no_std)] #![cfg_attr(feature = "simd-nightly", feature(stdsimd))] #![recursion_limit = "2048"] @@ -492,11 +492,7 @@ pub unsafe trait AsBytes { /// of `bytes`. If `bytes.len() < size_of_val(self)`, it returns `None`. fn write_to_prefix(&self, mut bytes: B) -> Option<()> { let size = mem::size_of_val(self); - if bytes.len() < size { - return None; - } - - bytes[..size].copy_from_slice(self.as_bytes()); + bytes.get_mut(..size)?.copy_from_slice(self.as_bytes()); Some(()) } @@ -506,7 +502,10 @@ pub unsafe trait AsBytes { /// `bytes`. If `bytes.len() < size_of_val(self)`, it returns `None`. fn write_to_suffix(&self, mut bytes: B) -> Option<()> { let start = bytes.len().checked_sub(mem::size_of_val(self))?; - bytes[start..].copy_from_slice(self.as_bytes()); + bytes + .get_mut(start..) + .expect("`start` should be in-bounds of `bytes`") + .copy_from_slice(self.as_bytes()); Some(()) } } diff --git a/zerocopy-derive/src/repr.rs b/zerocopy-derive/src/repr.rs index 84941eba7e..6ded70b4a5 100644 --- a/zerocopy-derive/src/repr.rs +++ b/zerocopy-derive/src/repr.rs @@ -47,14 +47,13 @@ impl Config { metas_reprs.sort_by(|a: &(NestedMeta, R), b| a.1.partial_cmp(&b.1).unwrap()); if self.derive_unaligned { - match metas_reprs.iter().find(|&repr: &&(NestedMeta, R)| repr.1.is_align_gt_one()) { - Some((meta, _)) => { - return Err(vec![Error::new_spanned( - meta, - "cannot derive Unaligned with repr(align(N > 1))", - )]) - } - None => (), + if let Some((meta, _)) = + metas_reprs.iter().find(|&repr: &&(NestedMeta, R)| repr.1.is_align_gt_one()) + { + return Err(vec![Error::new_spanned( + meta, + "cannot derive Unaligned with repr(align(N > 1))", + )]); } } @@ -80,12 +79,12 @@ impl Config { Ok(reprs) } else if self.disallowed_but_legal_combinations.contains(&reprs.as_slice()) { Err(vec![Error::new( - err_span.unwrap_or(input.span()), + err_span.unwrap_or_else(|| input.span()), self.allowed_combinations_message, )]) } else { Err(vec![Error::new( - err_span.unwrap_or(input.span()), + err_span.unwrap_or_else(|| input.span()), "conflicting representation hints", )]) } @@ -189,7 +188,7 @@ impl Repr { NestedMeta::Meta(Meta::Path(path)) => { let ident = path .get_ident() - .ok_or(Error::new_spanned(meta, "unrecognized representation hint"))?; + .ok_or_else(|| Error::new_spanned(meta, "unrecognized representation hint"))?; match format!("{}", ident).as_str() { "u8" => return Ok(Repr::U8), "u16" => return Ok(Repr::U16),