From 3d80ed6a84ad2f2b03736575b667ab94c96b4de4 Mon Sep 17 00:00:00 2001 From: glendc Date: Sun, 10 Sep 2023 14:20:23 +0200 Subject: [PATCH] enable clippy for tower-http and fix current issues --- .github/workflows/CI.yml | 14 ++++ Cargo.toml | 1 + .../src/classify/grpc_errors_as_failures.rs | 2 +- tower-http/src/compression/future.rs | 1 + tower-http/src/compression/mod.rs | 2 +- tower-http/src/content_encoding.rs | 64 +++++++++---------- tower-http/src/cors/allow_private_network.rs | 6 ++ tower-http/src/lib.rs | 3 +- tower-http/src/timeout/body.rs | 2 + tower-http/src/trace/mod.rs | 2 +- tower-http/src/validate_request.rs | 2 +- 11 files changed, 61 insertions(+), 38 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index a4fdc6ab..11ea500e 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -93,6 +93,20 @@ jobs: tool: protoc@3.20.3 - run: cargo fmt --all --check + clippy: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@stable + with: + components: clippy + - name: Install protoc + uses: taiki-e/install-action@v2 + with: + tool: protoc@3.20.3 + - run: cargo clippy --workspace --all-features --all-targets + deny-check: name: cargo-deny check runs-on: ubuntu-latest diff --git a/Cargo.toml b/Cargo.toml index 1667aa59..7225df23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,3 +3,4 @@ members = [ "tower-http", "examples/*", ] +resolver = "2" \ No newline at end of file diff --git a/tower-http/src/classify/grpc_errors_as_failures.rs b/tower-http/src/classify/grpc_errors_as_failures.rs index 056cec93..ebdd98ba 100644 --- a/tower-http/src/classify/grpc_errors_as_failures.rs +++ b/tower-http/src/classify/grpc_errors_as_failures.rs @@ -262,7 +262,7 @@ impl fmt::Display for GrpcFailureClass { } } -#[allow(clippy::if_let_some_result)] +#[allow(clippy::match_result_ok)] pub(crate) fn classify_grpc_metadata( headers: &HeaderMap, success_codes: GrpcCodeBitmask, diff --git a/tower-http/src/compression/future.rs b/tower-http/src/compression/future.rs index 426bb161..bfccff52 100644 --- a/tower-http/src/compression/future.rs +++ b/tower-http/src/compression/future.rs @@ -73,6 +73,7 @@ where CompressionBody::new(BodyInner::zstd(WrapBody::new(body, self.quality))) } #[cfg(feature = "fs")] + #[allow(unreachable_patterns)] (true, _) => { // This should never happen because the `AcceptEncoding` struct which is used to determine // `self.encoding` will only enable the different compression algorithms if the diff --git a/tower-http/src/compression/mod.rs b/tower-http/src/compression/mod.rs index dbfd2364..f0587713 100644 --- a/tower-http/src/compression/mod.rs +++ b/tower-http/src/compression/mod.rs @@ -277,7 +277,7 @@ mod tests { let mut guard = self.0.write().unwrap(); let should_compress = *guard % 2 != 0; *guard += 1; - dbg!(should_compress) + should_compress } } diff --git a/tower-http/src/content_encoding.rs b/tower-http/src/content_encoding.rs index c962d0ee..a52a2ab0 100644 --- a/tower-http/src/content_encoding.rs +++ b/tower-http/src/content_encoding.rs @@ -148,7 +148,7 @@ impl QValue { let mut c = s.chars(); // Parse "q=" (case-insensitively). match c.next() { - Some('q') | Some('Q') => (), + Some('q' | 'Q') => (), _ => return None, }; match c.next() { @@ -272,7 +272,7 @@ mod tests { #[test] fn no_accept_encoding_header() { let encoding = - Encoding::from_headers(&http::HeaderMap::new(), SupportedEncodingsAll::default()); + Encoding::from_headers(&http::HeaderMap::new(), SupportedEncodingsAll); assert_eq!(Encoding::Identity, encoding); } @@ -283,7 +283,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Gzip, encoding); } @@ -294,7 +294,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip,br"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Brotli, encoding); } @@ -305,7 +305,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip,deflate,br"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Brotli, encoding); } @@ -316,7 +316,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=0.5,br"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Brotli, encoding); } @@ -327,7 +327,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=0.5,deflate,br"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Brotli, encoding); } @@ -342,7 +342,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("br"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Brotli, encoding); } @@ -357,7 +357,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("br"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Brotli, encoding); } @@ -376,7 +376,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("br"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Brotli, encoding); } @@ -387,7 +387,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=0.5,br;q=0.8"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Brotli, encoding); let mut headers = http::HeaderMap::new(); @@ -395,7 +395,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=0.8,br;q=0.5"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Gzip, encoding); let mut headers = http::HeaderMap::new(); @@ -403,7 +403,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=0.995,br;q=0.999"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Brotli, encoding); } @@ -414,7 +414,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=0.5,deflate;q=0.6,br;q=0.8"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Brotli, encoding); let mut headers = http::HeaderMap::new(); @@ -422,7 +422,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=0.8,deflate;q=0.6,br;q=0.5"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Gzip, encoding); let mut headers = http::HeaderMap::new(); @@ -430,7 +430,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=0.6,deflate;q=0.8,br;q=0.5"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Deflate, encoding); let mut headers = http::HeaderMap::new(); @@ -438,7 +438,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=0.995,deflate;q=0.997,br;q=0.999"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Brotli, encoding); } @@ -449,7 +449,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("invalid,gzip"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Gzip, encoding); } @@ -460,7 +460,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=0"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Identity, encoding); let mut headers = http::HeaderMap::new(); @@ -468,7 +468,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=0."), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Identity, encoding); let mut headers = http::HeaderMap::new(); @@ -476,7 +476,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=0,br;q=0.5"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Brotli, encoding); } @@ -487,7 +487,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gZiP"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Gzip, encoding); let mut headers = http::HeaderMap::new(); @@ -495,7 +495,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=0.5,br;Q=0.8"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Brotli, encoding); } @@ -506,7 +506,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static(" gzip\t; q=0.5 ,\tbr ;\tq=0.8\t"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Brotli, encoding); } @@ -517,7 +517,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q =0.5"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Identity, encoding); let mut headers = http::HeaderMap::new(); @@ -525,7 +525,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q= 0.5"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Identity, encoding); } @@ -536,7 +536,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=-0.1"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Identity, encoding); let mut headers = http::HeaderMap::new(); @@ -544,7 +544,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=00.5"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Identity, encoding); let mut headers = http::HeaderMap::new(); @@ -552,7 +552,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=0.5000"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Identity, encoding); let mut headers = http::HeaderMap::new(); @@ -560,7 +560,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=.5"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Identity, encoding); let mut headers = http::HeaderMap::new(); @@ -568,7 +568,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=1.01"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Identity, encoding); let mut headers = http::HeaderMap::new(); @@ -576,7 +576,7 @@ mod tests { http::header::ACCEPT_ENCODING, http::HeaderValue::from_static("gzip;q=1.001"), ); - let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll::default()); + let encoding = Encoding::from_headers(&headers, SupportedEncodingsAll); assert_eq!(Encoding::Identity, encoding); } } diff --git a/tower-http/src/cors/allow_private_network.rs b/tower-http/src/cors/allow_private_network.rs index 4163014d..75903a24 100644 --- a/tower-http/src/cors/allow_private_network.rs +++ b/tower-http/src/cors/allow_private_network.rs @@ -39,6 +39,7 @@ impl AllowPrivateNetwork { Self(AllowPrivateNetworkInner::Predicate(Arc::new(f))) } + #[allow(clippy::borrow_interior_mutable_const)] pub(super) fn to_header( &self, origin: Option<&HeaderValue>, @@ -52,6 +53,7 @@ impl AllowPrivateNetwork { const ALLOW_PRIVATE_NETWORK: HeaderName = HeaderName::from_static("access-control-allow-private-network"); + #[allow(clippy::declare_interior_mutable_const)] const TRUE: HeaderValue = HeaderValue::from_static("true"); // Cheapest fallback: allow_private_network hasn't been set @@ -110,6 +112,7 @@ impl Default for AllowPrivateNetworkInner { } #[cfg(test)] +#[allow(clippy::borrow_interior_mutable_const)] mod tests { use super::AllowPrivateNetwork; use crate::cors::CorsLayer; @@ -119,12 +122,15 @@ mod tests { use tower::{BoxError, ServiceBuilder, ServiceExt}; use tower_service::Service; + #[allow(clippy::declare_interior_mutable_const)] const REQUEST_PRIVATE_NETWORK: HeaderName = HeaderName::from_static("access-control-request-private-network"); + #[allow(clippy::declare_interior_mutable_const)] const ALLOW_PRIVATE_NETWORK: HeaderName = HeaderName::from_static("access-control-allow-private-network"); + #[allow(clippy::declare_interior_mutable_const)] const TRUE: HeaderValue = HeaderValue::from_static("true"); #[tokio::test] diff --git a/tower-http/src/lib.rs b/tower-http/src/lib.rs index 6719ddbd..4e009521 100644 --- a/tower-http/src/lib.rs +++ b/tower-http/src/lib.rs @@ -183,7 +183,6 @@ clippy::todo, clippy::empty_enum, clippy::enum_glob_use, - clippy::pub_enum_variant_names, clippy::mem_forget, clippy::unused_self, clippy::filter_map_next, @@ -211,7 +210,7 @@ nonstandard_style, missing_docs )] -#![deny(unreachable_pub, private_in_public)] +#![deny(unreachable_pub)] #![allow( elided_lifetimes_in_paths, // TODO: Remove this once the MSRV bumps to 1.42.0 or above. diff --git a/tower-http/src/timeout/body.rs b/tower-http/src/timeout/body.rs index 29a952da..0a780636 100644 --- a/tower-http/src/timeout/body.rs +++ b/tower-http/src/timeout/body.rs @@ -159,6 +159,7 @@ mod tests { impl Error for MockError {} impl Display for MockError { + #[allow(clippy::todo)] fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { todo!() } @@ -183,6 +184,7 @@ mod tests { this.sleep.poll(cx).map(|_| Some(Ok(vec![].into()))) } + #[allow(clippy::todo)] fn poll_trailers( self: Pin<&mut Self>, _cx: &mut Context<'_>, diff --git a/tower-http/src/trace/mod.rs b/tower-http/src/trace/mod.rs index e08da3e3..30b504e1 100644 --- a/tower-http/src/trace/mod.rs +++ b/tower-http/src/trace/mod.rs @@ -506,7 +506,7 @@ mod tests { tracing::info_span!("test-span", foo = tracing::field::Empty) }) .on_request(|_req: &Request, span: &Span| { - span.record("foo", &42); + span.record("foo", 42); ON_REQUEST_COUNT.fetch_add(1, Ordering::SeqCst); }) .on_response(|_res: &Response, _latency: Duration, _span: &Span| { diff --git a/tower-http/src/validate_request.rs b/tower-http/src/validate_request.rs index 4f1ea680..78549a75 100644 --- a/tower-http/src/validate_request.rs +++ b/tower-http/src/validate_request.rs @@ -381,7 +381,7 @@ where .into_iter() .filter_map(|header| header.to_str().ok()) .any(|h| { - MimeIter::new(&h) + MimeIter::new(h) .map(|mim| { if let Ok(mim) = mim { let typ = self.header_value.type_();