From 9aed552c491aaabc84e3141bc70e4d26c03efa85 Mon Sep 17 00:00:00 2001 From: diogo464 Date: Tue, 9 Dec 2025 22:43:40 +0000 Subject: fixed warnings/lints --- src/entity_sensor.rs | 7 ++----- src/lib.rs | 38 +++++++++++++++++++++++++------------- src/log.rs | 11 ++++++----- src/mqtt/mod.rs | 50 +++++++++++++++++++++++--------------------------- src/mqtt/qos.rs | 3 ++- src/mqtt/varint.rs | 4 ++-- 6 files changed, 60 insertions(+), 53 deletions(-) diff --git a/src/entity_sensor.rs b/src/entity_sensor.rs index 1168c37..e221141 100644 --- a/src/entity_sensor.rs +++ b/src/entity_sensor.rs @@ -19,7 +19,9 @@ impl StateClass { } #[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Default)] pub enum SensorClass { + #[default] Generic, ApparentPower, Aqi, @@ -75,11 +77,6 @@ pub enum SensorClass { Other(&'static str), } -impl Default for SensorClass { - fn default() -> Self { - SensorClass::Generic - } -} impl SensorClass { pub fn as_str(&self) -> Option<&'static str> { diff --git a/src/lib.rs b/src/lib.rs index 714e186..50c6308 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -485,8 +485,10 @@ pub fn create_sensor<'a>( id: &'static str, config: SensorConfig, ) -> Sensor<'a> { - let mut entity_config = EntityConfig::default(); - entity_config.id = id; + let mut entity_config = EntityConfig { + id, + ..Default::default() + }; config.populate(&mut entity_config); let entity = create_entity( @@ -502,8 +504,10 @@ pub fn create_button<'a>( id: &'static str, config: ButtonConfig, ) -> Button<'a> { - let mut entity_config = EntityConfig::default(); - entity_config.id = id; + let mut entity_config = EntityConfig { + id, + ..Default::default() + }; config.populate(&mut entity_config); let entity = create_entity( @@ -519,8 +523,10 @@ pub fn create_number<'a>( id: &'static str, config: NumberConfig, ) -> Number<'a> { - let mut entity_config = EntityConfig::default(); - entity_config.id = id; + let mut entity_config = EntityConfig { + id, + ..Default::default() + }; config.populate(&mut entity_config); let entity = create_entity( @@ -539,8 +545,10 @@ pub fn create_switch<'a>( id: &'static str, config: SwitchConfig, ) -> Switch<'a> { - let mut entity_config = EntityConfig::default(); - entity_config.id = id; + let mut entity_config = EntityConfig { + id, + ..Default::default() + }; config.populate(&mut entity_config); let entity = create_entity( @@ -559,8 +567,10 @@ pub fn create_binary_sensor<'a>( id: &'static str, config: BinarySensorConfig, ) -> BinarySensor<'a> { - let mut entity_config = EntityConfig::default(); - entity_config.id = id; + let mut entity_config = EntityConfig { + id, + ..Default::default() + }; config.populate(&mut entity_config); let entity = create_entity( @@ -693,7 +703,7 @@ pub async fn run(device: &mut Device<'_>, transport: &mut T) -> Re .discovery_buffer .resize(device.discovery_buffer.capacity(), 0) .unwrap(); - let n = serde_json_core::to_slice(&discovery, &mut device.discovery_buffer) + let n = serde_json_core::to_slice(&discovery, device.discovery_buffer) .expect("discovery buffer too small"); device.discovery_buffer.truncate(n); } @@ -702,7 +712,7 @@ pub async fn run(device: &mut Device<'_>, transport: &mut T) -> Re crate::log::debug!("sending discovery to topic '{}'", discovery_topic); match embassy_time::with_timeout( MQTT_TIMEOUT, - client.publish(discovery_topic, &device.discovery_buffer), + client.publish(discovery_topic, device.discovery_buffer), ) .await { @@ -1077,10 +1087,12 @@ pub async fn connect_and_run( } }; + #[allow(unreachable_patterns)] let ipv4_addr = match addrs .iter() .filter_map(|addr| match addr { - embassy_net::IpAddress::Ipv4(ipv4) => Some((*ipv4).into()), + embassy_net::IpAddress::Ipv4(ipv4) => Some(*ipv4), + _ => None, }) .next() { diff --git a/src/log.rs b/src/log.rs index d25d210..5b67001 100644 --- a/src/log.rs +++ b/src/log.rs @@ -32,6 +32,7 @@ pub trait Format {} pub use defmt::Debug2Format; // For tracing or no logging, Debug2Format is a passthrough +#[allow(non_snake_case)] #[cfg(not(feature = "defmt"))] #[inline] pub fn Debug2Format(value: &T) -> &T { @@ -51,7 +52,7 @@ macro_rules! trace { tracing::trace!($($arg)*); #[cfg(not(any(feature = "defmt", feature = "tracing")))] - { let _ = (); } // no-op + { let _ = format_args!($($arg)*); } // no-op, format_args! borrows without moving }; } @@ -65,7 +66,7 @@ macro_rules! debug { tracing::debug!($($arg)*); #[cfg(not(any(feature = "defmt", feature = "tracing")))] - { let _ = (); } // no-op + { let _ = format_args!($($arg)*); } // no-op, format_args! borrows without moving }; } @@ -79,7 +80,7 @@ macro_rules! info { tracing::info!($($arg)*); #[cfg(not(any(feature = "defmt", feature = "tracing")))] - { let _ = (); } // no-op + { let _ = format_args!($($arg)*); } // no-op, format_args! borrows without moving }; } @@ -93,7 +94,7 @@ macro_rules! warn { tracing::warn!($($arg)*); #[cfg(not(any(feature = "defmt", feature = "tracing")))] - { let _ = (); } // no-op + { let _ = format_args!($($arg)*); } // no-op, format_args! borrows without moving }; } @@ -107,7 +108,7 @@ macro_rules! error { tracing::error!($($arg)*); #[cfg(not(any(feature = "defmt", feature = "tracing")))] - { let _ = (); } // no-op + { let _ = format_args!($($arg)*); } // no-op, format_args! borrows without moving }; } diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 30e3a33..04e63b6 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -1,3 +1,5 @@ +#![allow(unused)] + mod connect_code; mod field; mod packet_id; @@ -23,7 +25,7 @@ pub enum Error { Transport(T::Error), TransportEOF, InsufficientBufferSpace, - ProtocolError(&'static str), + Protocol(&'static str), ConnectFailed(ConnectCode), } @@ -33,7 +35,7 @@ impl core::fmt::Debug for Error { Error::Transport(err) => f.debug_tuple("Transport").field(err).finish(), Error::TransportEOF => f.write_str("TransportEOF"), Error::InsufficientBufferSpace => f.write_str("InsufficientBufferSpace"), - Error::ProtocolError(msg) => f.debug_tuple("ProtocolError").field(msg).finish(), + Error::Protocol(msg) => f.debug_tuple("ProtocolError").field(msg).finish(), Error::ConnectFailed(code) => f.debug_tuple("ConnectFailed").field(code).finish(), } } @@ -47,7 +49,7 @@ impl core::fmt::Display for Error { Error::InsufficientBufferSpace => { write!(f, "insufficient buffer space to receive packet") } - Error::ProtocolError(msg) => write!(f, "MQTT protocol error: {}", msg), + Error::Protocol(msg) => write!(f, "MQTT protocol error: {}", msg), Error::ConnectFailed(code) => write!(f, "connection failed: {}", code), } } @@ -209,7 +211,7 @@ where // Wait for CONNACK response match self.receive_inner().await? { rx::Packet::ConnAck { - session_present, + session_present: _, code, } => { if code == ConnectCode::ConnectionAccepted { @@ -218,7 +220,7 @@ where Err(Error::ConnectFailed(code)) } } - _ => Err(Error::ProtocolError( + _ => Err(Error::Protocol( "expected CONNACK packet after CONNECT", )), } @@ -351,12 +353,12 @@ where return Err(Error::InsufficientBufferSpace); } } - rx::Error::InvalidPacket(msg) => return Err(Error::ProtocolError(msg)), - rx::Error::UnsupportedPacket { packet_type, .. } => { - return Err(Error::ProtocolError("unsupported packet type")); + rx::Error::InvalidPacket(msg) => return Err(Error::Protocol(msg)), + rx::Error::UnsupportedPacket { packet_type: _, .. } => { + return Err(Error::Protocol("unsupported packet type")); } - rx::Error::UnknownPacket { packet_type, .. } => { - return Err(Error::ProtocolError("unknown packet type")); + rx::Error::UnknownPacket { packet_type: _, .. } => { + return Err(Error::Protocol("unknown packet type")); } }, } @@ -367,9 +369,7 @@ where pub async fn receive<'s>(&'s mut self) -> Result, Error> { match self.receive_inner().await? { - rx::Packet::ConnAck { .. } => { - return Err(Error::ProtocolError("unexpected CONNACK packet")); - } + rx::Packet::ConnAck { .. } => Err(Error::Protocol("unexpected CONNACK packet")), rx::Packet::Publish { topic, packet_id, @@ -377,23 +377,19 @@ where retain, dup: _dup, data_len, - } => { - return Ok(Packet::Publish(Publish { - topic, - packet_id, - qos, - retain, - data_len, - })); - } - rx::Packet::PubAck { packet_id } => { - return Ok(Packet::PublishAck(PublishAck { packet_id })); - } + } => Ok(Packet::Publish(Publish { + topic, + packet_id, + qos, + retain, + data_len, + })), + rx::Packet::PubAck { packet_id } => Ok(Packet::PublishAck(PublishAck { packet_id })), rx::Packet::SubscribeAck { packet_id, success } => { - return Ok(Packet::SubscribeAck(SubscribeAck { packet_id, success })); + Ok(Packet::SubscribeAck(SubscribeAck { packet_id, success })) } rx::Packet::UnsubscribeAck { packet_id } => { - return Ok(Packet::UnsubscribeAck(UnsubscribeAck { packet_id })); + Ok(Packet::UnsubscribeAck(UnsubscribeAck { packet_id })) } } } diff --git a/src/mqtt/qos.rs b/src/mqtt/qos.rs index 0d464b4..34fa7c1 100644 --- a/src/mqtt/qos.rs +++ b/src/mqtt/qos.rs @@ -9,6 +9,7 @@ impl core::fmt::Display for InvalidQos { impl core::error::Error for InvalidQos {} +#[allow(clippy::enum_variant_names)] #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] pub enum Qos { #[default] @@ -28,7 +29,7 @@ impl core::fmt::Display for Qos { } impl Qos { - pub fn to_u8(&self) -> u8 { + pub fn to_u8(self) -> u8 { match self { Qos::AtMostOnce => 0, Qos::AtLeastOnce => 1, diff --git a/src/mqtt/varint.rs b/src/mqtt/varint.rs index 63bdd06..d5416ca 100644 --- a/src/mqtt/varint.rs +++ b/src/mqtt/varint.rs @@ -41,8 +41,8 @@ pub fn encode(mut v: u32) -> ([u8; 4], usize) { pub fn decode(buf: &[u8]) -> Result<(u32, usize), Error> { let mut value = 0u32; - let v = buf.get(0).ok_or(Error::NeedMoreData)?; - value |= ((v & 0x7F) as u32) << 0; + let v = buf.first().ok_or(Error::NeedMoreData)?; + value |= (v & 0x7F) as u32; if v & 0x80 == 0 { return Ok((value, 1)); } -- cgit