From 368504b6e77fb1d6d067930c4f36e863e0f982ed Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Tue, 10 Jul 2018 23:26:04 +0300 Subject: [PATCH 1/8] handshake: remove implied 'static warning: Constants have by default a `'static` lifetime --> src/handshake/mod.rs:113:21 | 113 | const WS_GUID: &'static [u8] = b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; | -^^^^^^^----- help: consider removing `'static`: `&[u8]` | = note: #[warn(const_static_lifetime)] on by default = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#const_static_lifetime --- src/handshake/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/handshake/mod.rs b/src/handshake/mod.rs index fcb3f78..f28445d 100644 --- a/src/handshake/mod.rs +++ b/src/handshake/mod.rs @@ -110,7 +110,7 @@ pub enum ProcessingResult { fn convert_key(input: &[u8]) -> Result { // ... field is constructed by concatenating /key/ ... // ... with the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" (RFC 6455) - const WS_GUID: &'static [u8] = b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; + const WS_GUID: &[u8] = b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; let mut sha1 = Sha1::default(); sha1.input(input); sha1.input(WS_GUID); From 2585feba44c4d86c5c65f144a0d2f0f1bc22af6f Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Tue, 10 Jul 2018 23:31:37 +0300 Subject: [PATCH 2/8] Use struct initialization shorthand when possible Clippy suggests it by default. --- src/handshake/client.rs | 2 +- src/handshake/headers.rs | 2 +- src/handshake/machine.rs | 4 ++-- src/protocol/frame/frame.rs | 4 ++-- src/protocol/frame/mod.rs | 4 ++-- src/protocol/mod.rs | 4 ++-- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/handshake/client.rs b/src/handshake/client.rs index 15494f9..fc2581d 100644 --- a/src/handshake/client.rs +++ b/src/handshake/client.rs @@ -113,7 +113,7 @@ impl ClientHandshake { }; trace!("Client handshake initiated."); - MidHandshake { role: client, machine: machine } + MidHandshake { role: client, machine } } } diff --git a/src/handshake/headers.rs b/src/handshake/headers.rs index 73419b6..23f0d77 100644 --- a/src/handshake/headers.rs +++ b/src/handshake/headers.rs @@ -28,7 +28,7 @@ impl Headers { /// Iterate over all headers with the given name. pub fn find<'headers, 'name>(&'headers self, name: &'name str) -> HeadersIter<'name, 'headers> { HeadersIter { - name: name, + name, iter: self.data.iter() } } diff --git a/src/handshake/machine.rs b/src/handshake/machine.rs index a945be1..39f37a5 100644 --- a/src/handshake/machine.rs +++ b/src/handshake/machine.rs @@ -16,14 +16,14 @@ impl HandshakeMachine { /// Start reading data from the peer. pub fn start_read(stream: Stream) -> Self { HandshakeMachine { - stream: stream, + stream, state: HandshakeState::Reading(InputBuffer::with_capacity(MIN_READ)), } } /// Start writing data to the peer. pub fn start_write>>(stream: Stream, data: D) -> Self { HandshakeMachine { - stream: stream, + stream, state: HandshakeState::Writing(Cursor::new(data.into())), } } diff --git a/src/protocol/frame/frame.rs b/src/protocol/frame/frame.rs index cee9b00..4c9cc83 100644 --- a/src/protocol/frame/frame.rs +++ b/src/protocol/frame/frame.rs @@ -296,7 +296,7 @@ impl Frame { let code = NetworkEndian::read_u16(&data[0..2]).into(); data.drain(0..2); let text = String::from_utf8(data)?; - Ok(Some(CloseFrame { code: code, reason: text.into() })) + Ok(Some(CloseFrame { code, reason: text.into() })) } } } @@ -357,7 +357,7 @@ impl Frame { Frame { header: FrameHeader::default(), - payload: payload, + payload, } } diff --git a/src/protocol/frame/mod.rs b/src/protocol/frame/mod.rs index 470a795..9d17338 100644 --- a/src/protocol/frame/mod.rs +++ b/src/protocol/frame/mod.rs @@ -30,7 +30,7 @@ impl FrameSocket { /// Create a new frame socket. pub fn new(stream: Stream) -> Self { FrameSocket { - stream: stream, + stream, in_buffer: InputBuffer::with_capacity(MIN_READ), out_buffer: Vec::new(), header: None, @@ -40,7 +40,7 @@ impl FrameSocket { /// Create a new frame socket from partially read data. pub fn from_partially_read(stream: Stream, part: Vec) -> Self { FrameSocket { - stream: stream, + stream, in_buffer: InputBuffer::from_partially_read(part), out_buffer: Vec::new(), header: None, diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index e35b9b7..7fb2259 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -116,8 +116,8 @@ impl WebSocket { config: Option ) -> Self { WebSocket { - role: role, - socket: socket, + role, + socket, state: WebSocketState::Active, incomplete: None, send_queue: VecDeque::new(), From c211d046cb58f2df271417d04aa0e5fd03199adc Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Tue, 10 Jul 2018 23:32:57 +0300 Subject: [PATCH 3/8] handshake: remove .clone() from Copy types warning: using `clone` on a `Copy` type --> src/handshake/server.rs:159:21 | 159 | self.config.clone(), | ^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.config` | = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#clone_on_copy --- src/handshake/client.rs | 2 +- src/handshake/server.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/handshake/client.rs b/src/handshake/client.rs index fc2581d..82a297a 100644 --- a/src/handshake/client.rs +++ b/src/handshake/client.rs @@ -135,7 +135,7 @@ impl HandshakeRole for ClientHandshake { stream, tail, Role::Client, - self.config.clone(), + self.config, ); ProcessingResult::Done((websocket, result)) } diff --git a/src/handshake/server.rs b/src/handshake/server.rs index 11a8ffb..aed7edf 100644 --- a/src/handshake/server.rs +++ b/src/handshake/server.rs @@ -156,7 +156,7 @@ impl HandshakeRole for ServerHandshake { let websocket = WebSocket::from_raw_socket( stream, Role::Server, - self.config.clone(), + self.config, ); ProcessingResult::Done(websocket) } From 556db990b5abb78360ead5854ba541bcc6c83274 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Tue, 10 Jul 2018 23:35:22 +0300 Subject: [PATCH 4/8] mask: take [u8; 4] by value warning: this argument is passed by reference, but would be more efficient if passed by value --> src/protocol/frame/mask.rs:21:46 | 21 | fn apply_mask_fallback(buf: &mut [u8], mask: &[u8; 4]) { | ^^^^^^^^ help: consider passing by value instead: `[u8; 4]` | = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#trivially_copy_pass_by_ref --- src/protocol/frame/frame.rs | 2 +- src/protocol/frame/mask.rs | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/protocol/frame/frame.rs b/src/protocol/frame/frame.rs index 4c9cc83..3016704 100644 --- a/src/protocol/frame/frame.rs +++ b/src/protocol/frame/frame.rs @@ -269,7 +269,7 @@ impl Frame { #[inline] pub(crate) fn apply_mask(&mut self) { if let Some(mask) = self.header.mask.take() { - apply_mask(&mut self.payload, &mask) + apply_mask(&mut self.payload, mask) } } diff --git a/src/protocol/frame/mask.rs b/src/protocol/frame/mask.rs index 055be08..88b1894 100644 --- a/src/protocol/frame/mask.rs +++ b/src/protocol/frame/mask.rs @@ -11,14 +11,14 @@ pub fn generate_mask() -> [u8; 4] { /// Mask/unmask a frame. #[inline] -pub fn apply_mask(buf: &mut [u8], mask: &[u8; 4]) { +pub fn apply_mask(buf: &mut [u8], mask: [u8; 4]) { apply_mask_fast32(buf, mask) } /// A safe unoptimized mask application. #[inline] #[allow(dead_code)] -fn apply_mask_fallback(buf: &mut [u8], mask: &[u8; 4]) { +fn apply_mask_fallback(buf: &mut [u8], mask: [u8; 4]) { for (i, byte) in buf.iter_mut().enumerate() { *byte ^= mask[i & 3]; } @@ -27,9 +27,9 @@ fn apply_mask_fallback(buf: &mut [u8], mask: &[u8; 4]) { /// Faster version of `apply_mask()` which operates on 4-byte blocks. #[inline] #[allow(dead_code)] -fn apply_mask_fast32(buf: &mut [u8], mask: &[u8; 4]) { +fn apply_mask_fast32(buf: &mut [u8], mask: [u8; 4]) { let mask_u32: u32 = unsafe { - read_unaligned(mask.as_ptr() as *const u32) + read_unaligned((&mask).as_ptr() as *const u32) }; let mut ptr = buf.as_mut_ptr(); @@ -101,10 +101,10 @@ mod tests { // Check masking with proper alignment. { let mut masked = unmasked.clone(); - apply_mask_fallback(&mut masked, &mask); + apply_mask_fallback(&mut masked, mask); let mut masked_fast = unmasked.clone(); - apply_mask_fast32(&mut masked_fast, &mask); + apply_mask_fast32(&mut masked_fast, mask); assert_eq!(masked, masked_fast); } @@ -112,10 +112,10 @@ mod tests { // Check masking without alignment. { let mut masked = unmasked.clone(); - apply_mask_fallback(&mut masked[1..], &mask); + apply_mask_fallback(&mut masked[1..], mask); let mut masked_fast = unmasked.clone(); - apply_mask_fast32(&mut masked_fast[1..], &mask); + apply_mask_fast32(&mut masked_fast[1..], mask); assert_eq!(masked, masked_fast); } From c7a71106acab415aebfbef85765ba156d1440ae8 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Tue, 10 Jul 2018 23:37:59 +0300 Subject: [PATCH 5/8] handshake: switch unwrap_or to a lazy unwrap_or_else warning: use of `unwrap_or` followed by a function call --> src/handshake/client.rs:55:53 | 55 | let mut headers = self.extra_headers.take().unwrap_or(vec![]); | ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| < [ _ ] > :: into_vec ( box [ $ ( $ x ) , * ] ))` | = note: #[warn(or_fun_call)] on by default = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#or_fun_call --- src/handshake/client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/handshake/client.rs b/src/handshake/client.rs index 82a297a..615b413 100644 --- a/src/handshake/client.rs +++ b/src/handshake/client.rs @@ -52,7 +52,7 @@ impl<'t> Request<'t> { /// Adds a custom header to the request. pub fn add_header(&mut self, name: Cow<'t, str>, value: Cow<'t, str>) { - let mut headers = self.extra_headers.take().unwrap_or(vec![]); + let mut headers = self.extra_headers.take().unwrap_or_else(|| vec![]); headers.push((name, value)); self.extra_headers = Some(headers); } From bdc42480b2b44cf15592aed190049e5289d5ce65 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Tue, 10 Jul 2018 23:39:09 +0300 Subject: [PATCH 6/8] protocol: remove redundant closure warning: redundant closure found --> src/protocol/mod.rs:114:43 | 114 | config: config.unwrap_or_else(|| WebSocketConfig::default()), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove closure as shown: `WebSocketConfig::default` | = note: #[warn(redundant_closure)] on by default = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#redundant_closure --- src/protocol/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index 7fb2259..d1eb958 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -122,7 +122,7 @@ impl WebSocket { incomplete: None, send_queue: VecDeque::new(), pong: None, - config: config.unwrap_or_else(|| WebSocketConfig::default()), + config: config.unwrap_or_else(WebSocketConfig::default), } } } From 3fd52147f39a52c981fae13317f18f61f808805b Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Tue, 10 Jul 2018 23:49:55 +0300 Subject: [PATCH 7/8] protocol: mark IncompleteMessageType as Copy warning: this argument is passed by value, but not consumed in the function body --> src/protocol/message.rs:98:30 | 98 | pub fn new(message_type: IncompleteMessageType) -> Self { | ^^^^^^^^^^^^^^^^^^^^^ | = note: #[warn(needless_pass_by_value)] on by default help: consider marking this type as Copy --> src/protocol/message.rs:135:1 | 135| / pub enum IncompleteMessageType { 136| | Text, 137| | Binary, 138| | } | |_^ = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#needless_pass_by_value --- src/protocol/message.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/protocol/message.rs b/src/protocol/message.rs index 8ee3eac..f7a4640 100644 --- a/src/protocol/message.rs +++ b/src/protocol/message.rs @@ -159,6 +159,7 @@ impl IncompleteMessage { } /// The type of incomplete message. +#[derive(Copy, Clone)] pub enum IncompleteMessageType { Text, Binary, From cf6fc1bb75022947677bc01f54f16715e5c88dba Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Wed, 11 Jul 2018 10:11:26 +0300 Subject: [PATCH 8/8] frame: change CloseCode::is_allowed to by-value It's a small copy type so copying is more conventional. warning: this argument is passed by reference, but would be more efficient if passed by value --> src/protocol/frame/coding.rs:186:23 | 186 | pub fn is_allowed(&self) -> bool { | ^^^^^ help: consider passing by value instead: `self` | = note: #[warn(trivially_copy_pass_by_ref)] on by default = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.212/index.html#trivially_copy_pass_by_ref This is a breaking change. --- src/protocol/frame/coding.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/protocol/frame/coding.rs b/src/protocol/frame/coding.rs index 5fde5fb..13d9045 100644 --- a/src/protocol/frame/coding.rs +++ b/src/protocol/frame/coding.rs @@ -183,8 +183,8 @@ pub enum CloseCode { impl CloseCode { /// Check if this CloseCode is allowed. - pub fn is_allowed(&self) -> bool { - match *self { + pub fn is_allowed(self) -> bool { + match self { Bad(_) => false, Reserved(_) => false, Status => false,