Skip to content

Commit

Permalink
Fix Clippy lints (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlf committed Aug 3, 2023
1 parent c000fc8 commit 4c7d0ed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
14 changes: 11 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,10 @@ pub unsafe trait FromBytes {
return Box::new(Self::new_zeroed());
}

// TODO(#61): Add a "SAFETY" comment and remove this `allow`.
#[allow(clippy::undocumented_unsafe_blocks)]
unsafe {
let ptr = alloc::alloc::alloc_zeroed(layout) as *mut Self;
let ptr = alloc::alloc::alloc_zeroed(layout).cast::<Self>();
if ptr.is_null() {
alloc::alloc::handle_alloc_error(layout);
}
Expand Down Expand Up @@ -431,9 +433,12 @@ pub unsafe trait FromBytes {
mem::align_of::<Self>(),
)
.expect("total allocation size overflows `isize`");

// TODO(#61): Add a "SAFETY" comment and remove this `allow`.
#[allow(clippy::undocumented_unsafe_blocks)]
unsafe {
if layout.size() != 0 {
let ptr = alloc::alloc::alloc_zeroed(layout) as *mut Self;
let ptr = alloc::alloc::alloc_zeroed(layout).cast::<Self>();
if ptr.is_null() {
alloc::alloc::handle_alloc_error(layout);
}
Expand Down Expand Up @@ -2153,6 +2158,8 @@ mod alloc_support {

#[cfg(test)]
mod tests {
use core::convert::TryFrom as _;

use super::*;

#[test]
Expand Down Expand Up @@ -2360,7 +2367,8 @@ mod alloc_support {
#[test]
#[should_panic(expected = "total allocation size overflows `isize`: LayoutError")]
fn test_new_box_slice_zeroed_panics_isize_overflow() {
let _ = u16::new_box_slice_zeroed((isize::MAX as usize / mem::size_of::<u16>()) + 1);
let max = usize::try_from(isize::MAX).unwrap();
let _ = u16::new_box_slice_zeroed((max / mem::size_of::<u16>()) + 1);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#![deny(clippy::all)]
#![recursion_limit = "128"]

mod ext;
Expand Down Expand Up @@ -616,12 +617,12 @@ mod tests {
}

fn elements_are_sorted_and_deduped<T: Clone + Ord>(lists: &[&[T]]) -> bool {
lists.iter().all(|list| is_sorted_and_deduped(*list))
lists.iter().all(|list| is_sorted_and_deduped(list))
}

fn config_is_sorted<T: KindRepr + Clone>(config: &Config<T>) -> bool {
elements_are_sorted_and_deduped(&config.allowed_combinations)
&& elements_are_sorted_and_deduped(&config.disallowed_but_legal_combinations)
elements_are_sorted_and_deduped(config.allowed_combinations)
&& elements_are_sorted_and_deduped(config.disallowed_but_legal_combinations)
}

assert!(config_is_sorted(&STRUCT_UNION_UNALIGNED_CFG));
Expand Down

0 comments on commit 4c7d0ed

Please sign in to comment.