From b7f67b01de0a8a042d65d85841958f66b6b547b6 Mon Sep 17 00:00:00 2001 From: Julien Cretin Date: Mon, 13 May 2024 18:02:22 +0200 Subject: [PATCH] Fix clippy lints (#150) --- ci.sh | 10 +++++++--- src/accumulator.rs | 6 ++++++ src/de/deserializer.rs | 6 ------ src/de/flavors.rs | 1 + src/de/mod.rs | 27 +++++++++++++++------------ src/lib.rs | 5 +---- src/max_size.rs | 1 + src/ser/mod.rs | 12 ++++++------ src/ser/serializer.rs | 4 ++-- tests/accumulator.rs | 6 ++++-- tests/crc.rs | 6 +++--- tests/loopback.rs | 10 +++++----- tests/schema.rs | 2 +- 13 files changed, 52 insertions(+), 44 deletions(-) diff --git a/ci.sh b/ci.sh index 8b62a9b..e000570 100755 --- a/ci.sh +++ b/ci.sh @@ -9,15 +9,14 @@ ensure_target() { } cargo_check() { cargo check "$@" - # TODO: Uncomment once clippy lints are fixed. - # cargo clippy "$@" -- --deny=warnings + cargo clippy "$@" -- --deny=warnings } cargo_test() { cargo_check --all-targets "$@" cargo test "$@" } -cargo_test --features=alloc,experimental-derive +cargo_test --features=alloc,experimental-derive,use-std,use-crc ensure_target thumbv7em-none-eabi cargo_check --target=thumbv7em-none-eabi --no-default-features @@ -27,3 +26,8 @@ cargo fmt -- --check # TODO: Uncomment once documentation lints are fixed. # env RUSTDOCFLAGS='--cfg=docsrs --deny=warnings' cargo doc --no-deps + +cd postcard-derive + +cargo_check +cargo fmt -- --check diff --git a/src/accumulator.rs b/src/accumulator.rs index 2de019b..e739abb 100644 --- a/src/accumulator.rs +++ b/src/accumulator.rs @@ -90,6 +90,12 @@ pub enum FeedResult<'a, T> { }, } +impl Default for CobsAccumulator { + fn default() -> Self { + Self::new() + } +} + impl CobsAccumulator { /// Create a new accumulator. pub const fn new() -> Self { diff --git a/src/de/deserializer.rs b/src/de/deserializer.rs index 9ceac81..ff00959 100644 --- a/src/de/deserializer.rs +++ b/src/de/deserializer.rs @@ -44,12 +44,6 @@ impl<'de> Deserializer<'de, Slice<'de>> { } impl<'de, F: Flavor<'de>> Deserializer<'de, F> { - #[cfg(target_pointer_width = "8")] - #[inline(always)] - fn try_take_varint_usize(&mut self) -> Result { - self.try_take_varint_u8().map(|u| u as usize) - } - #[cfg(target_pointer_width = "16")] #[inline(always)] fn try_take_varint_usize(&mut self) -> Result { diff --git a/src/de/flavors.rs b/src/de/flavors.rs index 95603d1..844fd00 100644 --- a/src/de/flavors.rs +++ b/src/de/flavors.rs @@ -308,6 +308,7 @@ pub mod io { } /// Support for [std::io] traits + #[allow(clippy::module_inception)] #[cfg(feature = "use-std")] pub mod io { use super::super::Flavor; diff --git a/src/de/mod.rs b/src/de/mod.rs index b7c3424..a519e19 100644 --- a/src/de/mod.rs +++ b/src/de/mod.rs @@ -139,7 +139,7 @@ mod test_heapless { #[test] fn de_u8() { let output: Vec = to_vec(&0x05u8).unwrap(); - assert!(&[5] == output.deref()); + assert_eq!(&[5], output.deref()); let out: u8 = from_bytes(output.deref()).unwrap(); assert_eq!(out, 0x05); @@ -148,7 +148,7 @@ mod test_heapless { #[test] fn de_u16() { let output: Vec() }> = to_vec(&0xA5C7u16).unwrap(); - assert!(&[0xC7, 0xCB, 0x02] == output.deref()); + assert_eq!(&[0xC7, 0xCB, 0x02], output.deref()); let out: u16 = from_bytes(output.deref()).unwrap(); assert_eq!(out, 0xA5C7); @@ -166,7 +166,10 @@ mod test_heapless { #[test] fn de_u64() { let output: Vec() }> = to_vec(&0x1234_5678_90AB_CDEFu64).unwrap(); - assert!(&[0xEF, 0x9B, 0xAF, 0x85, 0x89, 0xCF, 0x95, 0x9A, 0x12] == output.deref()); + assert_eq!( + &[0xEF, 0x9B, 0xAF, 0x85, 0x89, 0xCF, 0x95, 0x9A, 0x12], + output.deref() + ); let out: u64 = from_bytes(output.deref()).unwrap(); assert_eq!(out, 0x1234_5678_90AB_CDEFu64); @@ -176,11 +179,12 @@ mod test_heapless { fn de_u128() { let output: Vec() }> = to_vec(&0x1234_5678_90AB_CDEF_1234_5678_90AB_CDEFu128).unwrap(); - assert!( + assert_eq!( &[ 0xEF, 0x9B, 0xAF, 0x85, 0x89, 0xCF, 0x95, 0x9A, 0x92, 0xDE, 0xB7, 0xDE, 0x8A, 0x92, 0x9E, 0xAB, 0xB4, 0x24, - ] == output.deref() + ], + output.deref() ); let out: u128 = from_bytes(output.deref()).unwrap(); @@ -310,22 +314,22 @@ mod test_heapless { assert_eq!(out, BasicEnum::Bim); let output: Vec() }> = - to_vec(&DataEnum::Bim(u64::max_value())).unwrap(); + to_vec(&DataEnum::Bim(u64::MAX)).unwrap(); assert_eq!( &[0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01], output.deref() ); let output: Vec() }> = - to_vec(&DataEnum::Bib(u16::max_value())).unwrap(); + to_vec(&DataEnum::Bib(u16::MAX)).unwrap(); assert_eq!(&[0x00, 0xFF, 0xFF, 0x03], output.deref()); let out: DataEnum = from_bytes(output.deref()).unwrap(); - assert_eq!(out, DataEnum::Bib(u16::max_value())); + assert_eq!(out, DataEnum::Bib(u16::MAX)); - let output: Vec = to_vec(&DataEnum::Bap(u8::max_value())).unwrap(); + let output: Vec = to_vec(&DataEnum::Bap(u8::MAX)).unwrap(); assert_eq!(&[0x02, 0xFF], output.deref()); let out: DataEnum = from_bytes(output.deref()).unwrap(); - assert_eq!(out, DataEnum::Bap(u8::max_value())); + assert_eq!(out, DataEnum::Bap(u8::MAX)); let output: Vec = to_vec(&DataEnum::Kim(EnumStruct { eight: 0xF0, @@ -498,8 +502,7 @@ mod test_heapless { fn unit() { let output: Vec = to_vec(&()).unwrap(); assert_eq!(output.len(), 0); - let out: () = from_bytes(output.deref()).unwrap(); - assert_eq!(out, ()); + let _: () = from_bytes(output.deref()).unwrap(); } #[test] diff --git a/src/lib.rs b/src/lib.rs index 92afc1a..9fafa00 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,11 +17,8 @@ mod varint; pub(crate) mod max_size; /// The schema types and macros. -#[cfg(not(feature = "experimental-derive"))] -mod schema; - #[cfg(feature = "experimental-derive")] -pub mod schema; +pub(crate) mod schema; /// # Experimental Postcard Features /// diff --git a/src/max_size.rs b/src/max_size.rs index 69ceebd..2848f92 100644 --- a/src/max_size.rs +++ b/src/max_size.rs @@ -228,6 +228,7 @@ impl MaxSize for heapless::String { const POSTCARD_MAX_SIZE: usize = <[u8; N]>::POSTCARD_MAX_SIZE + varint_size(N); } +#[cfg(feature = "heapless")] const fn varint_size(max_n: usize) -> usize { const BITS_PER_BYTE: usize = 8; const BITS_PER_VARINT_BYTE: usize = 7; diff --git a/src/ser/mod.rs b/src/ser/mod.rs index 6955688..c2b2050 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -308,7 +308,7 @@ where /// assert_eq!(&buf[0..5], &[0x01, 0x03, b'H', b'i', b'!']); /// ``` #[cfg(feature = "use-std")] -pub fn to_io<'b, T, W>(value: &'b T, writer: W) -> Result +pub fn to_io(value: &T, writer: W) -> Result where T: Serialize + ?Sized, W: std::io::Write, @@ -508,7 +508,7 @@ mod test { #[test] fn ser_u8() { let output: Vec = to_vec(&0x05u8).unwrap(); - assert!(&[5] == output.deref()); + assert_eq!(&[5], output.deref()); assert!(output.len() == serialized_size(&0x05u8).unwrap()); assert!(output.len() <= Vec::::POSTCARD_MAX_SIZE); } @@ -654,7 +654,7 @@ mod test { let mut buf = [0; varint_max::()]; let res = varint_usize(1, &mut buf); - assert!(&[1] == res); + assert_eq!(&[1], res); let res = varint_usize(usize::MAX, &mut buf); @@ -702,7 +702,7 @@ mod test { assert_eq!(&[0x01], output.deref()); assert!(output.len() == serialized_size(&input).unwrap()); - let input = DataEnum::Bim(u64::max_value()); + let input = DataEnum::Bim(u64::MAX); let output: Vec() }> = to_vec(&input).unwrap(); assert_eq!( &[0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01], @@ -710,12 +710,12 @@ mod test { ); assert!(output.len() == serialized_size(&input).unwrap()); - let input = DataEnum::Bib(u16::max_value()); + let input = DataEnum::Bib(u16::MAX); let output: Vec() }> = to_vec(&input).unwrap(); assert_eq!(&[0x00, 0xFF, 0xFF, 0x03], output.deref()); assert!(output.len() == serialized_size(&input).unwrap()); - let input = DataEnum::Bap(u8::max_value()); + let input = DataEnum::Bap(u8::MAX); let output: Vec = to_vec(&input).unwrap(); assert_eq!(&[0x02, 0xFF], output.deref()); assert!(output.len() == serialized_size(&input).unwrap()); diff --git a/src/ser/serializer.rs b/src/ser/serializer.rs index c7eb5d2..cc61575 100644 --- a/src/ser/serializer.rs +++ b/src/ser/serializer.rs @@ -318,9 +318,9 @@ where } #[inline] - fn collect_str(self, value: &T) -> Result + fn collect_str(self, value: &T) -> Result where - T: core::fmt::Display, + T: core::fmt::Display + ?Sized, { use core::fmt::Write; diff --git a/tests/accumulator.rs b/tests/accumulator.rs index 04d85d0..611e510 100644 --- a/tests/accumulator.rs +++ b/tests/accumulator.rs @@ -23,6 +23,8 @@ fn reader() { }; let input = postcard::to_slice_cobs(&expected, &mut input_buf).unwrap(); + // TODO(https://github.com/rust-lang/rust-clippy/issues/12751): Remove once fixed. + #[allow(clippy::redundant_slicing)] let mut input = &input[..]; // Magic number from serializing struct and printing length @@ -37,10 +39,10 @@ fn reader() { } let buf = &raw_buf[..ct]; - let mut window = &buf[..]; + let mut window = buf; 'cobs: while !window.is_empty() { - window = match cobs_buf.feed::(&window) { + window = match cobs_buf.feed::(window) { FeedResult::Consumed => break 'cobs, FeedResult::OverFull(new_wind) => new_wind, FeedResult::DeserError(new_wind) => new_wind, diff --git a/tests/crc.rs b/tests/crc.rs index 6e7a2f9..efcd384 100644 --- a/tests/crc.rs +++ b/tests/crc.rs @@ -11,7 +11,7 @@ fn test_crc() { assert_eq!(res, &[0x04, 0x01, 0x00, 0x20, 0x30, 0x8E, 0xC8, 0x1A, 0x37]); let digest = crc.digest(); - let res = postcard::take_from_bytes_crc32::<[u8; 5]>(&res, digest).unwrap(); + let res = postcard::take_from_bytes_crc32::<[u8; 5]>(res, digest).unwrap(); let expected_bytes = [0x04, 0x01, 0x00, 0x20, 0x30]; let remaining_bytes = []; @@ -31,7 +31,7 @@ fn test_crc_8() { assert_eq!(res, &[0x04, 0x01, 0x00, 0x20, 0x30, 167]); let digest = crc.digest(); - let res = postcard::de_flavors::crc::take_from_bytes_u8::<[u8; 5]>(&res, digest).unwrap(); + let res = postcard::de_flavors::crc::take_from_bytes_u8::<[u8; 5]>(res, digest).unwrap(); let expected_bytes = [0x04, 0x01, 0x00, 0x20, 0x30]; let remaining_bytes = []; @@ -54,7 +54,7 @@ fn test_crc_error() { res[last] = 0; let digest = crc.digest(); - let res = postcard::take_from_bytes_crc32::<[u8; 5]>(&res, digest); + let res = postcard::take_from_bytes_crc32::<[u8; 5]>(res, digest); assert_eq!(res, Err(postcard::Error::DeserializeBadCrc)); } diff --git a/tests/loopback.rs b/tests/loopback.rs index e560bfc..d9fff5d 100644 --- a/tests/loopback.rs +++ b/tests/loopback.rs @@ -98,13 +98,13 @@ fn loopback() { // Enums! test_one(BasicEnum::Bim, &[0x01]); test_one( - DataEnum::Bim(u64::max_value()), + DataEnum::Bim(u64::MAX), &[ 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, ], ); - test_one(DataEnum::Bib(u16::max_value()), &[0x00, 0xFF, 0xFF, 0x03]); - test_one(DataEnum::Bap(u8::max_value()), &[0x02, 0xFF]); + test_one(DataEnum::Bib(u16::MAX), &[0x00, 0xFF, 0xFF, 0x03]); + test_one(DataEnum::Bap(u8::MAX), &[0x02, 0xFF]); test_one( DataEnum::Kim(EnumStruct { eight: 0xF0, @@ -156,7 +156,7 @@ fn loopback() { #[cfg(feature = "heapless")] #[track_caller] -fn test_one<'a, 'de, T>(data: T, ser_rep: &'a [u8]) +fn test_one(data: T, ser_rep: &[u8]) where T: Serialize + DeserializeOwned + Eq + PartialEq + Debug, { @@ -179,7 +179,7 @@ fn std_io_loopback() { use postcard::from_io; use postcard::to_io; - fn test_io<'a, 'de, T>(data: T, ser_rep: &'a [u8]) + fn test_io(data: T, ser_rep: &[u8]) where T: Serialize + DeserializeOwned + Eq + PartialEq + Debug, { diff --git a/tests/schema.rs b/tests/schema.rs index 8888929..244560a 100644 --- a/tests/schema.rs +++ b/tests/schema.rs @@ -100,7 +100,7 @@ fn test_enum_serialize() { #[test] fn test_struct_serialize() { - const TEN_BYTES_SCHEMA: &'static [&'static NamedType] = &[&U8_SCHEMA; 10]; + const TEN_BYTES_SCHEMA: &[&NamedType] = &[&U8_SCHEMA; 10]; assert_eq!( Outer::SCHEMA,