From a9230b0760a5f3590b4f37dc7e13bd897bdaa853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Sk=C3=B3rkowski?= Date: Sat, 3 Dec 2022 18:49:31 +0100 Subject: [PATCH] make code pass clippy --- websockets/autobahn/src/utf8/mod.rs | 22 +++++++++------------- websockets/autobahn/src/websocket.rs | 28 +++++++++++----------------- 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/websockets/autobahn/src/utf8/mod.rs b/websockets/autobahn/src/utf8/mod.rs index 480f3ae1..ccc88216 100644 --- a/websockets/autobahn/src/utf8/mod.rs +++ b/websockets/autobahn/src/utf8/mod.rs @@ -38,9 +38,9 @@ const MAX_ASCII_VALUE: u8 = 0x7Fu8; const MIN_CONTINUATION: u8 = 0x80u8; const MAX_CONTINUATION: u8 = 0xBFu8; -const ERROR_INVALID_UTF8_SEQUENCE_MESSAGE: &'static str = "invalid utf-8 sequence"; +const ERROR_INVALID_UTF8_SEQUENCE_MESSAGE: &str = "invalid utf-8 sequence"; -#[derive(Debug, PartialEq)] +#[derive(Debug, Eq, PartialEq)] pub enum ByteResult { Continuation, First(usize), @@ -97,10 +97,7 @@ fn check_overflow(data: &[u8], expected_size: usize) -> bool { 0x80 }; - match (raw_1, raw_2) { - (0xE0, 0xA0..=0xBF) | (0xE1..=0xEC, 0x80..=0xBF) | (0xED, 0x80..=0x9F) => true, - _ => false, - } + matches!((raw_1, raw_2), (0xE0, 0xA0..=0xBF) | (0xE1..=0xEC, 0x80..=0xBF) | (0xED, 0x80..=0x9F)) } else { let raw_2: u8 = if len >= 2 { data[1] @@ -111,19 +108,18 @@ fn check_overflow(data: &[u8], expected_size: usize) -> bool { }; let raw_3: u8 = if len == 3 { data[2] } else { 0x80 }; - match (raw_1, raw_2, raw_3) { - (0xF0, 0x90..=0xBF, 0x80..=0xBF) - | (0xF1..=0xF3, 0x80..=0xBF, 0x80..=0xBF) - | (0xf4, 0x80..=0x8F, 0x80..=0xBF) => true, - _ => false, - } + matches!((raw_1, raw_2, raw_3), + (0xF0, 0x90..=0xBF, 0x80..=0xBF) | + (0xF1..=0xF3, 0x80..=0xBF, 0x80..=0xBF) | + (0xf4, 0x80..=0x8F, 0x80..=0xBF) + ) } } fn check_byte(byte: u8) -> ByteResult { if byte <= MAX_ASCII_VALUE { ByteResult::Ok - } else if byte >= MIN_CONTINUATION && byte <= MAX_CONTINUATION { + } else if (MIN_CONTINUATION..=MAX_CONTINUATION).contains(&byte) { ByteResult::Continuation } else if byte & UTF8_START_2_BYTE_SEQ_MASK == UTF8_2_BYTE_SEQ { ByteResult::First(2) diff --git a/websockets/autobahn/src/websocket.rs b/websockets/autobahn/src/websocket.rs index 7e86dc97..61c5992d 100644 --- a/websockets/autobahn/src/websocket.rs +++ b/websockets/autobahn/src/websocket.rs @@ -91,11 +91,8 @@ impl ContinuationBuffer { buffer.push(valid); - match message_overflow { - Some(message_overflow) => { - _ = overflow.insert(message_overflow); - } - None => {} + if let Some(message_overflow) = message_overflow { + _ = overflow.insert(message_overflow); } Ok(()) @@ -177,18 +174,15 @@ impl WebsocketActor { overflow: message_overflow, } = validate_utf8_bytes(data)?; - match message_overflow { - Some(bytes) => { - return Err(ProtocolError::Io(std::io::Error::new( - std::io::ErrorKind::Other, - format!( - "invalid utf-8 sequence of {} bytes from index {}", - bytes.len(), - valid.len() - ), - ))); - } - _ => {} + if let Some(bytes) = message_overflow { + return Err(ProtocolError::Io(std::io::Error::new( + std::io::ErrorKind::Other, + format!( + "invalid utf-8 sequence of {} bytes from index {}", + bytes.len(), + valid.len() + ), + ))); } ByteString::try_from(valid.clone()).map_err(|e| {