From 038d1c4ef193373ba09855a17af15e959d9b9e43 Mon Sep 17 00:00:00 2001 From: Richard Brodie Date: Mon, 20 Feb 2023 14:50:53 +0100 Subject: [PATCH] chore: fix all clippy warnings fixes #47 --- src/mac/beacon.rs | 44 +++++++++++++------ src/mac/command.rs | 16 +++---- src/mac/frame/header.rs | 21 ++++----- .../security/auxiliary_security_header.rs | 22 ++++------ src/mac/frame/security/mod.rs | 30 ++++++------- src/mac/frame/security/security_control.rs | 4 +- src/utils.rs | 6 --- 7 files changed, 73 insertions(+), 70 deletions(-) diff --git a/src/mac/beacon.rs b/src/mac/beacon.rs index fadf991..6e5b67b 100644 --- a/src/mac/beacon.rs +++ b/src/mac/beacon.rs @@ -102,7 +102,7 @@ const ASSOCIATION_PERMIT: u8 = 0b1000_0000; impl TryRead<'_> for SuperframeSpecification { fn try_read(bytes: &[u8], _ctx: ()) -> byte::Result<(Self, usize)> { let offset = &mut 0; - check_len(&bytes, 2)?; + check_len(bytes, 2)?; let byte: u8 = bytes.read(offset)?; let beacon_order = BeaconOrder::from(byte & 0x0f); let superframe_order = SuperframeOrder::from((byte >> 4) & 0x0f); @@ -130,8 +130,8 @@ impl TryRead<'_> for SuperframeSpecification { impl TryWrite for SuperframeSpecification { fn try_write(self, bytes: &mut [u8], _ctx: ()) -> byte::Result { let offset = &mut 0; - let bo = u8::from(self.beacon_order.clone()); - let so = u8::from(self.superframe_order.clone()); + let bo = u8::from(self.beacon_order); + let so = u8::from(self.superframe_order); bytes.write(offset, (bo & 0x0f) | (so << 4))?; let ble = if self.battery_life_extension { BATTERY_LIFE_EXTENSION @@ -189,10 +189,16 @@ impl GuaranteedTimeSlotDescriptor { } } +impl Default for GuaranteedTimeSlotDescriptor { + fn default() -> Self { + Self::new() + } +} + impl TryRead<'_> for GuaranteedTimeSlotDescriptor { fn try_read(bytes: &[u8], _ctx: ()) -> byte::Result<(Self, usize)> { let offset = &mut 0; - check_len(&bytes, 3)?; + check_len(bytes, 3)?; let short_address = bytes.read(offset)?; let byte: u8 = bytes.read(offset)?; let starting_slot = byte & 0x0f; @@ -259,6 +265,12 @@ impl GuaranteedTimeSlotInformation { } } +impl Default for GuaranteedTimeSlotInformation { + fn default() -> Self { + Self::new() + } +} + impl TryWrite for GuaranteedTimeSlotInformation { fn try_write(self, bytes: &mut [u8], _ctx: ()) -> byte::Result { let offset = &mut 0; @@ -275,9 +287,9 @@ impl TryWrite for GuaranteedTimeSlotInformation { for n in 0..self.slot_count { let slot = self.slots[n]; if slot.direction_transmit() { - direction_mask = direction_mask | dir; + direction_mask |= dir; } - dir = dir << 1; + dir <<= 1; } direction_mask }; @@ -307,7 +319,7 @@ impl TryRead<'_> for GuaranteedTimeSlotInformation { if slot_count > 0 { check_len(&bytes[*offset..], 2 + (3 * slot_count))?; let mut direction_mask: u8 = bytes.read(offset)?; - for n in 0..slot_count { + for item in slots.iter_mut().take(slot_count) { let mut slot: GuaranteedTimeSlotDescriptor = bytes.read(offset)?; let direction = if direction_mask & 0b1 == 0b1 { @@ -316,8 +328,8 @@ impl TryRead<'_> for GuaranteedTimeSlotInformation { Direction::Receive }; slot.set_direction(direction); - direction_mask = direction_mask >> 1; - slots[n] = slot; + direction_mask >>= 1; + *item = slot; } } Ok(( @@ -383,6 +395,12 @@ impl PendingAddress { } } +impl Default for PendingAddress { + fn default() -> Self { + Self::new() + } +} + impl TryRead<'_> for PendingAddress { fn try_read(bytes: &[u8], _ctx: ()) -> byte::Result<(Self, usize)> { let offset = &mut 0; @@ -393,12 +411,12 @@ impl TryRead<'_> for PendingAddress { let el = ((byte & EXTENDED_MASK) >> 4) as usize; check_len(&bytes[*offset..], (sl * ss) + (el * es))?; let mut short_addresses = [ShortAddress::broadcast(); 7]; - for n in 0..sl { - short_addresses[n] = bytes.read(offset)?; + for item in short_addresses.iter_mut().take(sl) { + *item = bytes.read(offset)?; } let mut extended_addresses = [ExtendedAddress::broadcast(); 7]; - for n in 0..el { - extended_addresses[n] = bytes.read(offset)?; + for item in extended_addresses.iter_mut().take(el) { + *item = bytes.read(offset)?; } Ok(( Self { diff --git a/src/mac/command.rs b/src/mac/command.rs index cc1c76c..1cf7c4f 100644 --- a/src/mac/command.rs +++ b/src/mac/command.rs @@ -80,19 +80,19 @@ impl From for u8 { fn from(ar: CapabilityInformation) -> Self { let mut byte = 0u8; if ar.full_function_device { - byte = byte | CAP_FFD; + byte |= CAP_FFD; } if ar.mains_power { - byte = byte | CAP_MAINS_POWER; + byte |= CAP_MAINS_POWER; } if ar.idle_receive { - byte = byte | CAP_IDLE_RECEIVE; + byte |= CAP_IDLE_RECEIVE; } if ar.frame_protection { - byte = byte | CAP_FRAME_PROTECTION; + byte |= CAP_FRAME_PROTECTION; } if ar.allocate_address { - byte = byte | CAP_ALLOCATE_ADDRESS; + byte |= CAP_ALLOCATE_ADDRESS; } byte } @@ -157,7 +157,7 @@ impl TryWrite for CoordinatorRealignmentData { impl TryRead<'_> for CoordinatorRealignmentData { fn try_read(bytes: &[u8], _ctx: ()) -> byte::Result<(Self, usize)> { let offset = &mut 0; - check_len(&bytes, 7)?; + check_len(bytes, 7)?; let pan_id = bytes.read(offset)?; let coordinator_address = bytes.read(offset)?; let channel = bytes.read(offset)?; @@ -213,10 +213,10 @@ impl From for u8 { fn from(gtsc: GuaranteedTimeSlotCharacteristics) -> Self { let mut byte = gtsc.count & 0x0f; if gtsc.receive_only { - byte = byte | GTSC_RECEIVE_ONLY; + byte |= GTSC_RECEIVE_ONLY; } if gtsc.allocation { - byte = byte | GTSC_ALLOCATION; + byte |= GTSC_ALLOCATION; } byte } diff --git a/src/mac/frame/header.rs b/src/mac/frame/header.rs index a8bf7f0..9f96559 100644 --- a/src/mac/frame/header.rs +++ b/src/mac/frame/header.rs @@ -88,18 +88,13 @@ impl Header { // Frame control + sequence number let mut len = 3; - for i in [self.destination, self.source].iter() { - match i { - Some(addr) => { - // pan ID - len += 2; - // Address length - match addr { - Address::Short(..) => len += 2, - Address::Extended(..) => len += 8, - } - } - _ => {} + for addr in [self.destination, self.source].iter().flatten() { + // pan ID + len += 2; + // Address length + match addr { + Address::Short(..) => len += 2, + Address::Extended(..) => len += 8, } } len @@ -115,7 +110,7 @@ impl TryRead<'_> for Header { fn try_read(bytes: &[u8], _ctx: ()) -> byte::Result<(Self, usize)> { let offset = &mut 0; // Make sure we have enough buffer for the Frame Control field - check_len(&bytes, 3)?; + check_len(bytes, 3)?; /* Decode Frame Control Field */ let bits: u16 = bytes.read_with(offset, LE)?; diff --git a/src/mac/frame/security/auxiliary_security_header.rs b/src/mac/frame/security/auxiliary_security_header.rs index 1e771f2..65bd9a4 100644 --- a/src/mac/frame/security/auxiliary_security_header.rs +++ b/src/mac/frame/security/auxiliary_security_header.rs @@ -25,8 +25,7 @@ impl AuxiliarySecurityHeader { /// Get the size of this security header, in octets pub fn get_octet_size(&self) -> usize { // SecurityControl length + FrameCounter length - let length = 1 - + 4 + 1 + 4 + match self.key_identifier { Some(key_id) => match key_id.key_source { Some(source) => match source { @@ -36,8 +35,7 @@ impl AuxiliarySecurityHeader { None => 1, }, None => 0, - }; - length + } } /// Create a new Auxiliary Security Header with the specified control and key identifier @@ -54,6 +52,8 @@ impl AuxiliarySecurityHeader { /// Create a new Auxiliary Security Header with the specified control, key identifier, and frame counter. /// + /// # Safety + /// /// This function is unsafe because the frame_counter is almost always set when parsing a frame from a buffer, /// or by the security context at the time of actually writing a secured frame. pub unsafe fn new_unsafe( @@ -143,11 +143,8 @@ where bytes.write(offset, self.control)?; bytes.write(offset, sec_ctx.frame_counter)?; - match self.key_identifier { - Some(key_identifier) => { - bytes.write(offset, key_identifier)?; - } - _ => {} + if let Some(key_identifier) = self.key_identifier { + bytes.write(offset, key_identifier)?; } Ok(*offset) } @@ -166,12 +163,11 @@ pub struct KeyIdentifier { impl TryWrite for KeyIdentifier { fn try_write(self, bytes: &mut [u8], _ctx: ()) -> byte::Result { let offset = &mut 0; - match self.key_source { - Some(source) => match source { + if let Some(source) = self.key_source { + match source { KeySource::Short(src) => bytes.write(offset, src)?, KeySource::Long(src) => bytes.write(offset, src)?, - }, - _ => {} + } } bytes.write(offset, self.key_index)?; diff --git a/src/mac/frame/security/mod.rs b/src/mac/frame/security/mod.rs index 52d878f..0ec7995 100644 --- a/src/mac/frame/security/mod.rs +++ b/src/mac/frame/security/mod.rs @@ -303,8 +303,8 @@ fn calculate_nonce( sec_level: SecurityLevel, ) -> [u8; 13] { let mut output = [0u8; 13]; - for i in 0..8 { - output[i] = (source_addr >> (8 * i) & 0xFF) as u8; + for (i, item) in output.iter_mut().enumerate().take(8) { + *item = (source_addr >> (8 * i) & 0xFF) as u8; } for i in 0..4 { @@ -326,7 +326,7 @@ fn calculate_nonce( /// /// # Panics /// if footer_mode is not None due to currently absent implementation of explicit footers -pub(crate) fn secure_frame<'a, AEADBLKCIPH, KEYDESCLO>( +pub(crate) fn secure_frame( frame: Frame<'_>, context: &mut SecurityContext, footer_mode: FooterMode, @@ -347,7 +347,7 @@ where } } - let mut offset = 0 as usize; + let mut offset = 0; let header = frame.header; if header.has_security() { @@ -368,12 +368,12 @@ where // If frame size plus AuthLen plus AuxLen plus FCS is bigger than aMaxPHYPacketSize // 7.2.1b4 - if !(frame.payload.len() + if frame.payload.len() + frame.header.get_octet_size() + aux_len + auth_len + 2 - <= 127) + > 127 { return Err(SecurityError::FrameTooLong); } @@ -432,7 +432,7 @@ where ), $encmic => aead.encrypt_in_place_detached( &GenericArray::from_slice(&nonce), - &mut [], + &[], auth_enc_part, ), _ => { @@ -482,15 +482,15 @@ where #[allow(unreachable_patterns)] _ => {} }; - return Ok(offset); + Ok(offset) } else { - return Err(SecurityError::UnavailableKey); + Err(SecurityError::UnavailableKey) } } else { - return Err(SecurityError::AuxSecHeaderAbsent); + Err(SecurityError::AuxSecHeaderAbsent) } } else { - return Err(SecurityError::SecurityNotEnabled); + Err(SecurityError::SecurityNotEnabled) } } @@ -510,7 +510,7 @@ where /// /// # Panics /// if footer_mode is not None due to currently absent implementation of explicit footers -pub(crate) fn unsecure_frame<'a, AEADBLKCIPH, KEYDESCLO, DEVDESCLO>( +pub(crate) fn unsecure_frame( header: &Header, buffer: &mut [u8], context: &mut SecurityContext, @@ -618,7 +618,7 @@ where ), $encmic => aead.decrypt_in_place_detached( &GenericArray::from_slice(&nonce), - &mut [], + &[], auth_enc_part, &tag, ), @@ -670,9 +670,9 @@ where } else { return Err(SecurityError::UnavailableKey); } - return Ok(taglen); + Ok(taglen) } else { - return Err(SecurityError::SecurityNotEnabled); + Err(SecurityError::SecurityNotEnabled) } } diff --git a/src/mac/frame/security/security_control.rs b/src/mac/frame/security/security_control.rs index dbca01b..802f3c2 100644 --- a/src/mac/frame/security/security_control.rs +++ b/src/mac/frame/security/security_control.rs @@ -101,7 +101,7 @@ impl SecurityLevel { } } - pub(crate) fn to_bits(&self) -> u8 { + pub(crate) fn to_bits(self) -> u8 { match self { SecurityLevel::None => 0b000, SecurityLevel::MIC32 => 0b001, @@ -150,7 +150,7 @@ impl KeyIdentifierMode { _ => None, } } - fn to_bits(&self) -> u8 { + fn to_bits(self) -> u8 { match self { KeyIdentifierMode::None => 0b00, KeyIdentifierMode::KeyIndex => 0b01, diff --git a/src/utils.rs b/src/utils.rs index 1225f73..ec1659c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -55,12 +55,6 @@ macro_rules! extended_enum { $( $name::$var => *self == $val, )* } } - - fn ne(&self, other: &$name) -> bool { - match *other { - $( $name::$var => *self != $val, )* - } - } } ); }