From 632713d9ae736d55ce1abdb9e7c9a3379efb44f8 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 28 Jan 2021 15:11:36 +0100 Subject: [PATCH] Fix or allow all clippy warnings --- firmware/defmt-rtt/src/lib.rs | 36 ++++++---------------------------- firmware/qemu/src/bin/panic.rs | 4 ++-- src/leb.rs | 1 + src/lib.rs | 9 +++++++++ tests/encode.rs | 24 +++++++++++------------ 5 files changed, 30 insertions(+), 44 deletions(-) diff --git a/firmware/defmt-rtt/src/lib.rs b/firmware/defmt-rtt/src/lib.rs index 147b5115..81c821ef 100644 --- a/firmware/defmt-rtt/src/lib.rs +++ b/firmware/defmt-rtt/src/lib.rs @@ -132,23 +132,11 @@ impl Channel { if cursor + len > SIZE { // split memcpy let pivot = SIZE - cursor; - ptr::copy_nonoverlapping( - bytes.as_ptr(), - self.buffer.add(cursor.into()), - pivot.into(), - ); - ptr::copy_nonoverlapping( - bytes.as_ptr().add(pivot.into()), - self.buffer, - (len - pivot).into(), - ); + ptr::copy_nonoverlapping(bytes.as_ptr(), self.buffer.add(cursor), pivot); + ptr::copy_nonoverlapping(bytes.as_ptr().add(pivot), self.buffer, len - pivot); } else { // single memcpy - ptr::copy_nonoverlapping( - bytes.as_ptr(), - self.buffer.add(cursor.into()), - len.into(), - ); + ptr::copy_nonoverlapping(bytes.as_ptr(), self.buffer.add(cursor), len); } } self.write @@ -167,23 +155,11 @@ impl Channel { if cursor + len > SIZE { // split memcpy let pivot = SIZE - cursor; - ptr::copy_nonoverlapping( - bytes.as_ptr(), - self.buffer.add(cursor.into()), - pivot.into(), - ); - ptr::copy_nonoverlapping( - bytes.as_ptr().add(pivot.into()), - self.buffer, - (len - pivot).into(), - ); + ptr::copy_nonoverlapping(bytes.as_ptr(), self.buffer.add(cursor), pivot); + ptr::copy_nonoverlapping(bytes.as_ptr().add(pivot), self.buffer, len - pivot); } else { // single memcpy - ptr::copy_nonoverlapping( - bytes.as_ptr(), - self.buffer.add(cursor.into()), - len.into(), - ); + ptr::copy_nonoverlapping(bytes.as_ptr(), self.buffer.add(cursor), len); } } self.write diff --git a/firmware/qemu/src/bin/panic.rs b/firmware/qemu/src/bin/panic.rs index 432be991..dba48c0a 100644 --- a/firmware/qemu/src/bin/panic.rs +++ b/firmware/qemu/src/bin/panic.rs @@ -8,12 +8,12 @@ use defmt_semihosting as _; // global logger #[entry] fn main() -> ! { let answer = 42; - let foo: u32 = match answer { + let value: u32 = match answer { 1 => 123, 2 => 456, _ => defmt::panic!("The answer is {=?}", answer), }; - defmt::panic!("should never get here {=?}", foo); + defmt::panic!("should never get here {=?}", value); } // like `panic-semihosting` but doesn't print to stdout (that would corrupt the defmt stream) diff --git a/src/leb.rs b/src/leb.rs index 3566a9ba..b8e1ef10 100644 --- a/src/leb.rs +++ b/src/leb.rs @@ -1,6 +1,7 @@ /// LEB128-encodes a `usize` value into `buf`. /// /// This handles 32-bit and 64-bit `usize`. +#[allow(clippy::needless_range_loop)] pub(crate) fn leb64(x: usize, buf: &mut [u8; 10]) -> usize { let mut low = x as u32; // Shift by 16 twice, to avoid a panic/error when shifting a 32-bit usize by 32 bits. diff --git a/src/lib.rs b/src/lib.rs index c1f53bff..9e665746 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -335,6 +335,7 @@ pub struct Formatter<'a> { impl InternalFormatter { /// Only for testing #[cfg(feature = "unstable-test")] + #[allow(clippy::new_without_default)] pub fn new() -> Self { Self { bytes: vec![], @@ -621,11 +622,19 @@ mod test_only { #[doc(hidden)] impl super::InternalFormatter { /// Implementation detail + /// + /// # Safety + /// + /// This is always safe to call and will panic. It only exists to match the non-test API. pub unsafe fn from_raw(_: NonNull) -> Self { unreachable!() } /// Implementation detail + /// + /// # Safety + /// + /// This is always safe to call and will panic. It only exists to match the non-test API. pub unsafe fn into_raw(self) -> NonNull { unreachable!() } diff --git a/tests/encode.rs b/tests/encode.rs index a41da741..d9b96eac 100644 --- a/tests/encode.rs +++ b/tests/encode.rs @@ -58,7 +58,7 @@ fn check_format_implementation(val: &(impl Format + ?Sized), expected_encoding: #[test] fn write() { let index = fetch_string_index(); - let ref mut f = InternalFormatter::new(); + let f = &mut InternalFormatter::new(); let g = Formatter { inner: f }; write!(g, "The answer is {=u8}", 42); @@ -70,7 +70,7 @@ fn write() { ] ); - let ref mut f2 = InternalFormatter::new(); + let f2 = &mut InternalFormatter::new(); let g2 = Formatter { inner: f2 }; write!(g2, "The answer is {=?}", 42u8); assert_eq!( @@ -86,7 +86,7 @@ fn write() { #[test] fn booleans_max_num_bool_flags() { let index = fetch_string_index(); - let ref mut f = InternalFormatter::new(); + let f = &mut InternalFormatter::new(); let g = Formatter { inner: f }; write!( @@ -106,7 +106,7 @@ fn booleans_max_num_bool_flags() { #[test] fn booleans_less_than_max_num_bool_flags() { let index = fetch_string_index(); - let ref mut f = InternalFormatter::new(); + let f = &mut InternalFormatter::new(); let g = Formatter { inner: f }; write!( @@ -126,7 +126,7 @@ fn booleans_less_than_max_num_bool_flags() { #[test] fn booleans_more_than_max_num_bool_flags() { let index = fetch_string_index(); - let ref mut f = InternalFormatter::new(); + let f = &mut InternalFormatter::new(); let g = Formatter { inner: f }; write!(g, "encode 9 bools {=bool} {=bool} {=bool} {=bool} {=bool} {=bool} {=bool} {=bool} {=bool} {=bool}", @@ -144,7 +144,7 @@ fn booleans_more_than_max_num_bool_flags() { #[test] fn booleans_mixed() { let index = fetch_string_index(); - let ref mut f = InternalFormatter::new(); + let f = &mut InternalFormatter::new(); let g = Formatter { inner: f }; write!( @@ -165,7 +165,7 @@ fn booleans_mixed() { #[test] fn booleans_mixed_no_trailing_bool() { let index = fetch_string_index(); - let ref mut f = InternalFormatter::new(); + let f = &mut InternalFormatter::new(); let g = Formatter { inner: f }; write!(g, "encode mixed bools {=bool} {=u8}", false, 42); @@ -181,7 +181,7 @@ fn booleans_mixed_no_trailing_bool() { #[test] fn bitfields_mixed() { let index = fetch_string_index(); - let ref mut f = InternalFormatter::new(); + let f = &mut InternalFormatter::new(); let g = Formatter { inner: f }; write!( @@ -203,7 +203,7 @@ fn bitfields_mixed() { #[test] fn bitfields_across_octets() { let index = fetch_string_index(); - let ref mut f = InternalFormatter::new(); + let f = &mut InternalFormatter::new(); let g = Formatter { inner: f }; write!(g, "bitfields {0=0..7} {0=9..14}", 0b0110_0011_1101_0010u16); @@ -220,7 +220,7 @@ fn bitfields_across_octets() { #[test] fn bitfields_truncate_lower() { let index = fetch_string_index(); - let ref mut f = InternalFormatter::new(); + let f = &mut InternalFormatter::new(); let g = Formatter { inner: f }; write!( @@ -240,7 +240,7 @@ fn bitfields_truncate_lower() { #[test] fn bitfields_assert_range_exclusive() { let index = fetch_string_index(); - let ref mut f = InternalFormatter::new(); + let f = &mut InternalFormatter::new(); let g = Formatter { inner: f }; write!(g, "bitfields {0=6..8}", 0b1010_0101u8,); @@ -280,7 +280,7 @@ fn boolean_struct_mixed() { } let index = fetch_string_index(); - let ref mut f = InternalFormatter::new(); + let f = &mut InternalFormatter::new(); let g = Formatter { inner: f }; write!(