From 4b0ece6d2854af088fbc1bdb55c2cdd19ec9bb92 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Fri, 9 Jul 2021 18:23:55 +0200 Subject: [PATCH] chore: fix clippy lints (#707) --- examples/src/blocking/client.rs | 2 +- examples/src/reflection/server.rs | 2 +- examples/src/tower/client.rs | 1 + tonic-build/src/client.rs | 8 +++++++- tonic-build/src/lib.rs | 4 +--- tonic-build/src/server.rs | 8 +++++++- tonic-reflection/src/lib.rs | 2 +- tonic-reflection/src/server.rs | 6 +++--- tonic-reflection/tests/server.rs | 4 ++-- tonic-web/src/call.rs | 2 +- tonic-web/src/cors.rs | 2 +- tonic-web/tests/integration/tests/grpc.rs | 2 +- tonic/src/codec/prost.rs | 2 +- tonic/src/metadata/map.rs | 21 +++++++-------------- tonic/src/metadata/value.rs | 2 ++ tonic/src/status.rs | 8 ++++---- tonic/src/transport/channel/mod.rs | 4 ++-- tonic/src/transport/service/reconnect.rs | 2 +- tonic/src/transport/service/tls.rs | 6 +++--- 19 files changed, 47 insertions(+), 41 deletions(-) diff --git a/examples/src/blocking/client.rs b/examples/src/blocking/client.rs index 12788e0..fe83348 100644 --- a/examples/src/blocking/client.rs +++ b/examples/src/blocking/client.rs @@ -27,7 +27,7 @@ impl BlockingClient { let rt = Builder::new_multi_thread().enable_all().build().unwrap(); let client = rt.block_on(GreeterClient::connect(dst))?; - Ok(Self { rt, client }) + Ok(Self { client, rt }) } pub fn say_hello( diff --git a/examples/src/reflection/server.rs b/examples/src/reflection/server.rs index e0632fe..500141f 100644 --- a/examples/src/reflection/server.rs +++ b/examples/src/reflection/server.rs @@ -4,7 +4,7 @@ use tonic::{Request, Response, Status}; mod proto { tonic::include_proto!("helloworld"); - pub(crate) const FILE_DESCRIPTOR_SET: &'static [u8] = + pub(crate) const FILE_DESCRIPTOR_SET: &[u8] = tonic::include_file_descriptor_set!("helloworld_descriptor"); } diff --git a/examples/src/tower/client.rs b/examples/src/tower/client.rs index eab48ba..3f5d410 100644 --- a/examples/src/tower/client.rs +++ b/examples/src/tower/client.rs @@ -61,6 +61,7 @@ mod service { impl Service> for AuthSvc { type Response = Response; type Error = Box; + #[allow(clippy::type_complexity)] type Future = Pin> + Send>>; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { diff --git a/tonic-build/src/client.rs b/tonic-build/src/client.rs index 8125c5c..d3acad6 100644 --- a/tonic-build/src/client.rs +++ b/tonic-build/src/client.rs @@ -36,7 +36,13 @@ pub fn generate( /// Generated client implementations. #(#mod_attributes)* pub mod #client_mod { - #![allow(unused_variables, dead_code, missing_docs)] + #![allow( + unused_variables, + dead_code, + missing_docs, + // will trigger if compression is disabled + clippy::let_unit_value, + )] use tonic::codegen::*; #service_doc diff --git a/tonic-build/src/lib.rs b/tonic-build/src/lib.rs index f32c242..8eafdf2 100644 --- a/tonic-build/src/lib.rs +++ b/tonic-build/src/lib.rs @@ -284,9 +284,7 @@ fn generate_doc_comments>(comments: &[T]) -> TokenStream { pub(crate) fn match_name(pattern: &str, path: &str) -> bool { if pattern.is_empty() { false - } else if pattern == "." { - true - } else if pattern == path { + } else if pattern == "." || pattern == path { true } else { let pattern_segments = pattern.split('.').collect::>(); diff --git a/tonic-build/src/server.rs b/tonic-build/src/server.rs index be10b83..5feb816 100644 --- a/tonic-build/src/server.rs +++ b/tonic-build/src/server.rs @@ -69,7 +69,13 @@ pub fn generate( /// Generated server implementations. #(#mod_attributes)* pub mod #server_mod { - #![allow(unused_variables, dead_code, missing_docs)] + #![allow( + unused_variables, + dead_code, + missing_docs, + // will trigger if compression is disabled + clippy::let_unit_value, + )] use tonic::codegen::*; #generated_trait diff --git a/tonic-reflection/src/lib.rs b/tonic-reflection/src/lib.rs index 7d46dbf..68350f8 100644 --- a/tonic-reflection/src/lib.rs +++ b/tonic-reflection/src/lib.rs @@ -19,7 +19,7 @@ pub(crate) mod proto { #![allow(unreachable_pub)] tonic::include_proto!("grpc.reflection.v1alpha"); - pub(crate) const FILE_DESCRIPTOR_SET: &'static [u8] = + pub(crate) const FILE_DESCRIPTOR_SET: &[u8] = tonic::include_file_descriptor_set!("reflection_v1alpha1"); } diff --git a/tonic-reflection/src/server.rs b/tonic-reflection/src/server.rs index 72e82eb..d306ab3 100644 --- a/tonic-reflection/src/server.rs +++ b/tonic-reflection/src/server.rs @@ -221,7 +221,7 @@ impl<'b> Builder<'b> { field: &FieldDescriptorProto, ) -> Result<(), Error> { let field_name = extract_name(prefix, "field", field.name.as_ref())?; - self.symbols.insert(field_name, fd.clone()); + self.symbols.insert(field_name, fd); Ok(()) } } @@ -265,7 +265,7 @@ impl ReflectionServiceState { None => Err(Status::not_found(format!("symbol '{}' not found", symbol))), Some(fd) => { let mut encoded_fd = Vec::new(); - if let Err(_) = fd.clone().encode(&mut encoded_fd) { + if fd.clone().encode(&mut encoded_fd).is_err() { return Err(Status::internal("encoding error")); }; @@ -283,7 +283,7 @@ impl ReflectionServiceState { None => Err(Status::not_found(format!("file '{}' not found", filename))), Some(fd) => { let mut encoded_fd = Vec::new(); - if let Err(_) = fd.clone().encode(&mut encoded_fd) { + if fd.clone().encode(&mut encoded_fd).is_err() { return Err(Status::internal("encoding error")); } diff --git a/tonic-reflection/tests/server.rs b/tonic-reflection/tests/server.rs index 48182e8..30e78bc 100644 --- a/tonic-reflection/tests/server.rs +++ b/tonic-reflection/tests/server.rs @@ -20,12 +20,12 @@ mod pb { tonic::include_proto!("grpc.reflection.v1alpha"); - pub(crate) const REFLECTION_SERVICE_DESCRIPTOR: &'static [u8] = + pub(crate) const REFLECTION_SERVICE_DESCRIPTOR: &[u8] = tonic::include_file_descriptor_set!("reflection_v1alpha1"); pub(crate) fn get_encoded_reflection_service_fd() -> Vec { let mut expected = Vec::new(); - &prost_types::FileDescriptorSet::decode(REFLECTION_SERVICE_DESCRIPTOR) + prost_types::FileDescriptorSet::decode(REFLECTION_SERVICE_DESCRIPTOR) .expect("decode reflection service file descriptor set") .file[0] .encode(&mut expected) diff --git a/tonic-web/src/call.rs b/tonic-web/src/call.rs index 26a52d2..c2af867 100644 --- a/tonic-web/src/call.rs +++ b/tonic-web/src/call.rs @@ -235,7 +235,7 @@ impl Encoding { Self::from_header(headers.get(header::ACCEPT)) } - pub(crate) fn to_content_type(&self) -> &'static str { + pub(crate) fn to_content_type(self) -> &'static str { match self { Encoding::Base64 => GRPC_WEB_TEXT_PROTO, Encoding::None => GRPC_WEB_PROTO, diff --git a/tonic-web/src/cors.rs b/tonic-web/src/cors.rs index 20b9650..7b49d35 100644 --- a/tonic-web/src/cors.rs +++ b/tonic-web/src/cors.rs @@ -161,7 +161,7 @@ mod tests { ($header:expr, $expected:expr) => { fn sorted(value: &str) -> Vec<&str> { let mut vec = value.split(",").collect::>(); - vec.sort(); + vec.sort_unstable(); vec } diff --git a/tonic-web/tests/integration/tests/grpc.rs b/tonic-web/tests/integration/tests/grpc.rs index 0ab9ad4..5515ada 100644 --- a/tonic-web/tests/integration/tests/grpc.rs +++ b/tonic-web/tests/integration/tests/grpc.rs @@ -65,7 +65,7 @@ async fn smoke_server_stream() { let r3 = stream(r3).await; let r4 = stream(r4).await; - assert!(&r1 == &r2 && &r2 == &r3 && &r3 == &r4); + assert!(r1 == r2 && r2 == r3 && r3 == r4); } #[tokio::test] async fn smoke_error() { diff --git a/tonic/src/codec/prost.rs b/tonic/src/codec/prost.rs index 0db3ee0..678ab96 100644 --- a/tonic/src/codec/prost.rs +++ b/tonic/src/codec/prost.rs @@ -186,7 +186,7 @@ mod tests { impl MockBody { pub(super) fn new(b: &[u8], partial_len: usize, count: usize) -> Self { MockBody { - data: Bytes::copy_from_slice(&b[..]), + data: Bytes::copy_from_slice(b), partial_len, count, } diff --git a/tonic/src/metadata/map.rs b/tonic/src/metadata/map.rs index 197976d..017479d 100644 --- a/tonic/src/metadata/map.rs +++ b/tonic/src/metadata/map.rs @@ -2524,8 +2524,7 @@ mod tests { if key.as_str() == "x-word" { found_x_word = true; } else { - // Unexpected key - assert!(false); + panic!("Unexpected key"); } } } @@ -2545,8 +2544,7 @@ mod tests { if key.as_str() == "x-word-bin" { found_x_word_bin = true; } else { - // Unexpected key - assert!(false); + panic!("Unexpected key"); } } } @@ -2567,8 +2565,7 @@ mod tests { if key.as_str() == "x-word" { found_x_word = true; } else { - // Unexpected key - assert!(false); + panic!("Unexpected key"); } } } @@ -2588,8 +2585,7 @@ mod tests { if key.as_str() == "x-word-bin" { found_x_word_bin = true; } else { - // Unexpected key - assert!(false); + panic!("Unexpected key"); } } } @@ -2610,8 +2606,7 @@ mod tests { if key.as_str() == "x-word" { found_x_word = true; } else { - // Unexpected key - assert!(false); + panic!("Unexpected key"); } } } @@ -2631,8 +2626,7 @@ mod tests { if key.as_str() == "x-number-bin" { found_x_number_bin = true; } else { - // Unexpected key - assert!(false); + panic!("Unexpected key"); } } } @@ -2653,8 +2647,7 @@ mod tests { if *value == "hello" { found_x_word = true; } else { - // Unexpected key - assert!(false); + panic!("Unexpected key"); } } } diff --git a/tonic/src/metadata/value.rs b/tonic/src/metadata/value.rs index 73af6d9..b976440 100644 --- a/tonic/src/metadata/value.rs +++ b/tonic/src/metadata/value.rs @@ -1,3 +1,5 @@ +#![allow(clippy::upper_case_acronyms)] + use super::encoding::{ Ascii, Binary, InvalidMetadataValue, InvalidMetadataValueBytes, ValueEncoding, }; diff --git a/tonic/src/status.rs b/tonic/src/status.rs index 8d9af6b..c44f88a 100644 --- a/tonic/src/status.rs +++ b/tonic/src/status.rs @@ -738,7 +738,7 @@ impl Code { } } - fn to_header_value(&self) -> HeaderValue { + fn to_header_value(self) -> HeaderValue { match self { Code::Ok => HeaderValue::from_static("0"), Code::Cancelled => HeaderValue::from_static("1"), @@ -921,16 +921,16 @@ mod tests { let status = Status::with_details(Code::Unavailable, "some message", DETAILS.into()); - assert_eq!(&status.details()[..], DETAILS); + assert_eq!(status.details(), DETAILS); let header_map = status.to_header_map().unwrap(); - let b64_details = base64::encode_config(&DETAILS[..], base64::STANDARD_NO_PAD); + let b64_details = base64::encode_config(DETAILS, base64::STANDARD_NO_PAD); assert_eq!(header_map[super::GRPC_STATUS_DETAILS_HEADER], b64_details); let status = Status::from_header_map(&header_map).unwrap(); - assert_eq!(&status.details()[..], DETAILS); + assert_eq!(status.details(), DETAILS); } } diff --git a/tonic/src/transport/channel/mod.rs b/tonic/src/transport/channel/mod.rs index c2ecd8e..af8cfee 100644 --- a/tonic/src/transport/channel/mod.rs +++ b/tonic/src/transport/channel/mod.rs @@ -137,7 +137,7 @@ impl Channel { C::Future: Unpin + Send, C::Response: AsyncRead + AsyncWrite + HyperConnection + Unpin + Send + 'static, { - let buffer_size = endpoint.buffer_size.clone().unwrap_or(DEFAULT_BUFFER_SIZE); + let buffer_size = endpoint.buffer_size.unwrap_or(DEFAULT_BUFFER_SIZE); let svc = Connection::lazy(connector, endpoint); let svc = Buffer::new(Either::A(svc), buffer_size); @@ -152,7 +152,7 @@ impl Channel { C::Future: Unpin + Send, C::Response: AsyncRead + AsyncWrite + HyperConnection + Unpin + Send + 'static, { - let buffer_size = endpoint.buffer_size.clone().unwrap_or(DEFAULT_BUFFER_SIZE); + let buffer_size = endpoint.buffer_size.unwrap_or(DEFAULT_BUFFER_SIZE); let svc = Connection::connect(connector, endpoint) .await diff --git a/tonic/src/transport/service/reconnect.rs b/tonic/src/transport/service/reconnect.rs index 019a493..811544c 100644 --- a/tonic/src/transport/service/reconnect.rs +++ b/tonic/src/transport/service/reconnect.rs @@ -142,7 +142,7 @@ where tracing::trace!("Reconnect::call"); if let Some(error) = self.error.take() { tracing::debug!("error: {}", error); - return ResponseFuture::error(error.into()); + return ResponseFuture::error(error); } let service = match self.state { diff --git a/tonic/src/transport/service/tls.rs b/tonic/src/transport/service/tls.rs index 1d78df2..e9ba339 100644 --- a/tonic/src/transport/service/tls.rs +++ b/tonic/src/transport/service/tls.rs @@ -49,7 +49,7 @@ impl TlsConnector { domain: String, ) -> Result { let mut config = ClientConfig::new(); - config.set_protocols(&[Vec::from(&ALPN_H2[..])]); + config.set_protocols(&[Vec::from(ALPN_H2)]); if let Some(identity) = identity { let (client_cert, client_key) = rustls_keys::load_identity(identity)?; @@ -60,7 +60,7 @@ impl TlsConnector { { config.root_store = match rustls_native_certs::load_native_certs() { Ok(store) | Err((Some(store), _)) => store, - Err((None, error)) => Err(error)?, + Err((None, error)) => return Err(error.into()), }; } @@ -153,7 +153,7 @@ impl TlsAcceptor { } }; config.set_single_cert(cert, key)?; - config.set_protocols(&[Vec::from(&ALPN_H2[..])]); + config.set_protocols(&[Vec::from(ALPN_H2)]); Ok(Self { inner: Arc::new(config),