Skip to content

Commit

Permalink
Fix clippy lints (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
ia0 committed May 13, 2024
1 parent 4e1104d commit b7f67b0
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 44 deletions.
10 changes: 7 additions & 3 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
6 changes: 6 additions & 0 deletions src/accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ pub enum FeedResult<'a, T> {
},
}

impl<const N: usize> Default for CobsAccumulator<N> {
fn default() -> Self {
Self::new()
}
}

impl<const N: usize> CobsAccumulator<N> {
/// Create a new accumulator.
pub const fn new() -> Self {
Expand Down
6 changes: 0 additions & 6 deletions src/de/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> {
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<usize> {
Expand Down
1 change: 1 addition & 0 deletions src/de/flavors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 15 additions & 12 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ mod test_heapless {
#[test]
fn de_u8() {
let output: Vec<u8, 1> = 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);
Expand All @@ -148,7 +148,7 @@ mod test_heapless {
#[test]
fn de_u16() {
let output: Vec<u8, { varint_max::<u16>() }> = 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);
Expand All @@ -166,7 +166,10 @@ mod test_heapless {
#[test]
fn de_u64() {
let output: Vec<u8, { varint_max::<u64>() }> = 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);
Expand All @@ -176,11 +179,12 @@ mod test_heapless {
fn de_u128() {
let output: Vec<u8, { varint_max::<u128>() }> =
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();
Expand Down Expand Up @@ -310,22 +314,22 @@ mod test_heapless {
assert_eq!(out, BasicEnum::Bim);

let output: Vec<u8, { 1 + varint_max::<u64>() }> =
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<u8, { 1 + varint_max::<u16>() }> =
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<u8, 2> = to_vec(&DataEnum::Bap(u8::max_value())).unwrap();
let output: Vec<u8, 2> = 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<u8, 8> = to_vec(&DataEnum::Kim(EnumStruct {
eight: 0xF0,
Expand Down Expand Up @@ -498,8 +502,7 @@ mod test_heapless {
fn unit() {
let output: Vec<u8, 1> = 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]
Expand Down
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down
1 change: 1 addition & 0 deletions src/max_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ impl<const N: usize> MaxSize for heapless::String<N> {
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;
Expand Down
12 changes: 6 additions & 6 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W>
pub fn to_io<T, W>(value: &T, writer: W) -> Result<W>
where
T: Serialize + ?Sized,
W: std::io::Write,
Expand Down Expand Up @@ -508,7 +508,7 @@ mod test {
#[test]
fn ser_u8() {
let output: Vec<u8, 1> = to_vec(&0x05u8).unwrap();
assert!(&[5] == output.deref());
assert_eq!(&[5], output.deref());
assert!(output.len() == serialized_size(&0x05u8).unwrap());
assert!(output.len() <= Vec::<u8, 1>::POSTCARD_MAX_SIZE);
}
Expand Down Expand Up @@ -654,7 +654,7 @@ mod test {
let mut buf = [0; varint_max::<usize>()];
let res = varint_usize(1, &mut buf);

assert!(&[1] == res);
assert_eq!(&[1], res);

let res = varint_usize(usize::MAX, &mut buf);

Expand Down Expand Up @@ -702,20 +702,20 @@ 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<u8, { 1 + varint_max::<u64>() }> = to_vec(&input).unwrap();
assert_eq!(
&[0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01],
output.deref()
);
assert!(output.len() == serialized_size(&input).unwrap());

let input = DataEnum::Bib(u16::max_value());
let input = DataEnum::Bib(u16::MAX);
let output: Vec<u8, { 1 + varint_max::<u16>() }> = 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<u8, 2> = to_vec(&input).unwrap();
assert_eq!(&[0x02, 0xFF], output.deref());
assert!(output.len() == serialized_size(&input).unwrap());
Expand Down
4 changes: 2 additions & 2 deletions src/ser/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ where
}

#[inline]
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok>
fn collect_str<T>(self, value: &T) -> Result<Self::Ok>
where
T: core::fmt::Display,
T: core::fmt::Display + ?Sized,
{
use core::fmt::Write;

Expand Down
6 changes: 4 additions & 2 deletions tests/accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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::<Huge>(&window) {
window = match cobs_buf.feed::<Huge>(window) {
FeedResult::Consumed => break 'cobs,
FeedResult::OverFull(new_wind) => new_wind,
FeedResult::DeserError(new_wind) => new_wind,
Expand Down
6 changes: 3 additions & 3 deletions tests/crc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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 = [];
Expand All @@ -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));
}
Expand Down
10 changes: 5 additions & 5 deletions tests/loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<T>(data: T, ser_rep: &[u8])
where
T: Serialize + DeserializeOwned + Eq + PartialEq + Debug,
{
Expand All @@ -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<T>(data: T, ser_rep: &[u8])
where
T: Serialize + DeserializeOwned + Eq + PartialEq + Debug,
{
Expand Down
2 changes: 1 addition & 1 deletion tests/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b7f67b0

Please sign in to comment.