From 9c77ee02952a4336c00241f5966870f7928ebf23 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 9 Nov 2021 12:53:57 +0100 Subject: [PATCH] Use nightly clippy & rustfmt for more style checks (#487) --- .github/workflows/CI.yml | 4 ++-- .rustfmt.toml | 2 ++ axum-handle-error-extract/src/lib.rs | 1 + axum/src/{body.rs => body/mod.rs} | 3 +-- axum/src/error_handling/mod.rs | 4 ++-- axum/src/extract/connect_info.rs | 5 ++--- axum/src/extract/content_length_limit.rs | 1 + axum/src/extract/extractor_middleware.rs | 3 +-- axum/src/extract/mod.rs | 3 +-- axum/src/extract/path/mod.rs | 3 +-- axum/src/extract/rejection.rs | 3 +-- axum/src/extract/ws.rs | 3 ++- axum/src/handler/into_service.rs | 6 +++--- axum/src/handler/mod.rs | 3 +-- axum/src/json.rs | 6 +++--- axum/src/lib.rs | 1 + axum/src/response/headers.rs | 6 ++++-- axum/src/response/sse.rs | 3 +-- axum/src/routing/handler_method_routing.rs | 6 ++---- axum/src/routing/mod.rs | 3 +-- axum/src/routing/service_method_routing.rs | 5 ++--- axum/src/routing/tests/mod.rs | 12 +++++------- axum/src/routing/tests/nest.rs | 4 +--- axum/src/test_helpers.rs | 6 ++++-- examples/async-graphql/src/main.rs | 14 ++++++++++---- examples/async-graphql/src/starwars/model.rs | 6 ++++-- examples/customize-extractor-error/src/main.rs | 3 +-- examples/oauth/src/main.rs | 1 + .../query-params-with-empty-strings/src/main.rs | 1 + examples/testing/src/main.rs | 6 ++++-- examples/unix-domain-socket/src/main.rs | 4 ++-- examples/versioning/src/main.rs | 3 +-- 32 files changed, 69 insertions(+), 65 deletions(-) create mode 100644 .rustfmt.toml rename axum/src/{body.rs => body/mod.rs} (95%) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 6c454a04..a1923ea0 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -14,10 +14,10 @@ jobs: - uses: actions/checkout@master - uses: actions-rs/toolchain@v1 with: - toolchain: stable + toolchain: nightly override: true profile: minimal - components: clippy + components: clippy, rustfmt - uses: Swatinem/rust-cache@v1 - name: Check uses: actions-rs/cargo@v1 diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 00000000..946fb445 --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,2 @@ +# Merge multiple `use`s from the same crate +imports_granularity = "Crate" diff --git a/axum-handle-error-extract/src/lib.rs b/axum-handle-error-extract/src/lib.rs index 05db6165..a4550b5e 100644 --- a/axum-handle-error-extract/src/lib.rs +++ b/axum-handle-error-extract/src/lib.rs @@ -125,6 +125,7 @@ clippy::option_option, clippy::verbose_file_reads, clippy::unnested_or_patterns, + clippy::self_named_module_files, rust_2018_idioms, future_incompatible, nonstandard_style, diff --git a/axum/src/body.rs b/axum/src/body/mod.rs similarity index 95% rename from axum/src/body.rs rename to axum/src/body/mod.rs index 76e3019f..c96b6f36 100644 --- a/axum/src/body.rs +++ b/axum/src/body/mod.rs @@ -1,7 +1,6 @@ //! HTTP body utilities. -use crate::BoxError; -use crate::Error; +use crate::{BoxError, Error}; mod stream_body; diff --git a/axum/src/error_handling/mod.rs b/axum/src/error_handling/mod.rs index f97bcdc0..8eb20824 100644 --- a/axum/src/error_handling/mod.rs +++ b/axum/src/error_handling/mod.rs @@ -3,8 +3,8 @@ use crate::{body::BoxBody, response::IntoResponse, BoxError}; use bytes::Bytes; use http::{Request, Response}; -use std::convert::Infallible; use std::{ + convert::Infallible, fmt, marker::PhantomData, task::{Context, Poll}, @@ -159,8 +159,8 @@ pub mod future { use futures_util::ready; use http::Response; use pin_project_lite::pin_project; - use std::convert::Infallible; use std::{ + convert::Infallible, future::Future, pin::Pin, task::{Context, Poll}, diff --git a/axum/src/extract/connect_info.rs b/axum/src/extract/connect_info.rs index 11a8fb75..071b0690 100644 --- a/axum/src/extract/connect_info.rs +++ b/axum/src/extract/connect_info.rs @@ -8,10 +8,10 @@ use super::{Extension, FromRequest, RequestParts}; use crate::{AddExtension, AddExtensionLayer}; use async_trait::async_trait; use hyper::server::conn::AddrStream; -use std::future::ready; use std::{ convert::Infallible, fmt, + future::ready, marker::PhantomData, net::SocketAddr, task::{Context, Poll}, @@ -144,8 +144,7 @@ where #[cfg(test)] mod tests { use super::*; - use crate::Server; - use crate::{routing::get, Router}; + use crate::{routing::get, Router, Server}; use std::net::{SocketAddr, TcpListener}; #[tokio::test] diff --git a/axum/src/extract/content_length_limit.rs b/axum/src/extract/content_length_limit.rs index fe8961d4..81d2cf65 100644 --- a/axum/src/extract/content_length_limit.rs +++ b/axum/src/extract/content_length_limit.rs @@ -88,6 +88,7 @@ mod tests { use std::iter::repeat; #[derive(Debug, Deserialize)] + #[allow(dead_code)] struct Input { foo: String, } diff --git a/axum/src/extract/extractor_middleware.rs b/axum/src/extract/extractor_middleware.rs index 85015268..d4509c7f 100644 --- a/axum/src/extract/extractor_middleware.rs +++ b/axum/src/extract/extractor_middleware.rs @@ -3,8 +3,7 @@ //! See [`extractor_middleware`] for more details. use super::{FromRequest, RequestParts}; -use crate::BoxError; -use crate::{body::BoxBody, response::IntoResponse}; +use crate::{body::BoxBody, response::IntoResponse, BoxError}; use bytes::Bytes; use futures_util::{future::BoxFuture, ready}; use http::{Request, Response}; diff --git a/axum/src/extract/mod.rs b/axum/src/extract/mod.rs index 4c9c9ad7..25493adf 100644 --- a/axum/src/extract/mod.rs +++ b/axum/src/extract/mod.rs @@ -359,8 +359,7 @@ pub(crate) fn take_body(req: &mut RequestParts) -> Result) -> Poll> { - // `IntoService` can only be constructed from async functions which are always ready, or from - // `Layered` which bufferes in `::call` and is therefore also always - // ready. + // `IntoService` can only be constructed from async functions which are always ready, or + // from `Layered` which bufferes in `::call` and is therefore + // also always ready. Poll::Ready(Ok(())) } diff --git a/axum/src/handler/mod.rs b/axum/src/handler/mod.rs index 4982eab1..80b9fe55 100644 --- a/axum/src/handler/mod.rs +++ b/axum/src/handler/mod.rs @@ -407,8 +407,7 @@ mod tests { #[test] fn traits() { - use crate::routing::MethodRouter; - use crate::test_helpers::*; + use crate::{routing::MethodRouter, test_helpers::*}; assert_send::>(); assert_sync::>(); } diff --git a/axum/src/json.rs b/axum/src/json.rs index 71d65fad..75b9a589 100644 --- a/axum/src/json.rs +++ b/axum/src/json.rs @@ -1,7 +1,7 @@ -use crate::BoxError; use crate::{ extract::{rejection::*, take_body, FromRequest, RequestParts}, response::IntoResponse, + BoxError, }; use async_trait::async_trait; use bytes::Bytes; @@ -50,8 +50,8 @@ use std::{ /// # }; /// ``` /// -/// When used as a response, it can serialize any type that implements [`serde::Serialize`] to `JSON`, -/// and will automatically set `Content-Type: application/json` header. +/// When used as a response, it can serialize any type that implements [`serde::Serialize`] to +/// `JSON`, and will automatically set `Content-Type: application/json` header. /// /// # Response example /// diff --git a/axum/src/lib.rs b/axum/src/lib.rs index 74eaf0be..a424db6e 100644 --- a/axum/src/lib.rs +++ b/axum/src/lib.rs @@ -295,6 +295,7 @@ clippy::option_option, clippy::verbose_file_reads, clippy::unnested_or_patterns, + clippy::self_named_module_files, rust_2018_idioms, future_incompatible, nonstandard_style, diff --git a/axum/src/response/headers.rs b/axum/src/response/headers.rs index c4c9bcb6..01b9cdcb 100644 --- a/axum/src/response/headers.rs +++ b/axum/src/response/headers.rs @@ -4,8 +4,10 @@ use crate::{ BoxError, }; use bytes::Bytes; -use http::header::{HeaderMap, HeaderName, HeaderValue}; -use http::{Response, StatusCode}; +use http::{ + header::{HeaderMap, HeaderName, HeaderValue}, + Response, StatusCode, +}; use http_body::{Body, Full}; use std::{convert::TryInto, fmt}; use tower::util::Either; diff --git a/axum/src/response/sse.rs b/axum/src/response/sse.rs index b6108cf4..b9a19645 100644 --- a/axum/src/response/sse.rs +++ b/axum/src/response/sse.rs @@ -27,8 +27,7 @@ //! # }; //! ``` -use crate::response::IntoResponse; -use crate::BoxError; +use crate::{response::IntoResponse, BoxError}; use bytes::Bytes; use futures_util::{ ready, diff --git a/axum/src/routing/handler_method_routing.rs b/axum/src/routing/handler_method_routing.rs index fcae7a38..47c98d44 100644 --- a/axum/src/routing/handler_method_routing.rs +++ b/axum/src/routing/handler_method_routing.rs @@ -7,8 +7,7 @@ use crate::{ util::{Either, EitherProj}, }; use futures_util::{future::BoxFuture, ready}; -use http::Method; -use http::{Request, Response}; +use http::{Method, Request, Response}; use http_body::Empty; use pin_project_lite::pin_project; use std::{ @@ -19,8 +18,7 @@ use std::{ pin::Pin, task::{Context, Poll}, }; -use tower::util::Oneshot; -use tower::ServiceExt; +use tower::{util::Oneshot, ServiceExt}; use tower_service::Service; /// Route requests with any standard HTTP method to the given handler. diff --git a/axum/src/routing/mod.rs b/axum/src/routing/mod.rs index b9d8ba06..a4af3269 100644 --- a/axum/src/routing/mod.rs +++ b/axum/src/routing/mod.rs @@ -1,7 +1,6 @@ //! Routing between [`Service`]s and handlers. -use self::future::RouterFuture; -use self::not_found::NotFound; +use self::{future::RouterFuture, not_found::NotFound}; use crate::{ body::{box_body, Body, BoxBody}, extract::{ diff --git a/axum/src/routing/service_method_routing.rs b/axum/src/routing/service_method_routing.rs index f26dde5c..7588d560 100644 --- a/axum/src/routing/service_method_routing.rs +++ b/axum/src/routing/service_method_routing.rs @@ -107,15 +107,14 @@ use futures_util::ready; use http::{Method, Request, Response}; use http_body::Empty; use pin_project_lite::pin_project; -use std::marker::PhantomData; use std::{ fmt, future::Future, + marker::PhantomData, pin::Pin, task::{Context, Poll}, }; -use tower::util::Oneshot; -use tower::ServiceExt as _; +use tower::{util::Oneshot, ServiceExt as _}; use tower_service::Service; /// Route requests with any standard HTTP method to the given service. diff --git a/axum/src/routing/tests/mod.rs b/axum/src/routing/tests/mod.rs index 84bb4565..81b7bcb7 100644 --- a/axum/src/routing/tests/mod.rs +++ b/axum/src/routing/tests/mod.rs @@ -1,23 +1,21 @@ -use crate::error_handling::HandleErrorLayer; -use crate::test_helpers::*; -use crate::BoxError; use crate::{ + error_handling::HandleErrorLayer, extract::{self, Path}, handler::Handler, response::IntoResponse, routing::{any, delete, get, on, patch, post, service_method_routing as service, MethodFilter}, - Json, Router, + test_helpers::*, + BoxError, Json, Router, }; use bytes::Bytes; use http::{header::HeaderMap, Method, Request, Response, StatusCode, Uri}; use hyper::Body; use serde::Deserialize; use serde_json::{json, Value}; -use std::future::Ready; -use std::sync::atomic::{AtomicUsize, Ordering}; use std::{ convert::Infallible, - future::ready, + future::{ready, Ready}, + sync::atomic::{AtomicUsize, Ordering}, task::{Context, Poll}, time::Duration, }; diff --git a/axum/src/routing/tests/nest.rs b/axum/src/routing/tests/nest.rs index 96758444..39f74073 100644 --- a/axum/src/routing/tests/nest.rs +++ b/axum/src/routing/tests/nest.rs @@ -1,7 +1,5 @@ use super::*; -use crate::body::box_body; -use crate::error_handling::HandleErrorExt; -use crate::extract::Extension; +use crate::{body::box_body, error_handling::HandleErrorExt, extract::Extension}; use std::collections::HashMap; #[tokio::test] diff --git a/axum/src/test_helpers.rs b/axum/src/test_helpers.rs index 113f83dd..992305d7 100644 --- a/axum/src/test_helpers.rs +++ b/axum/src/test_helpers.rs @@ -6,8 +6,10 @@ use http::{ Request, StatusCode, }; use hyper::{Body, Server}; -use std::net::SocketAddr; -use std::{convert::TryFrom, net::TcpListener}; +use std::{ + convert::TryFrom, + net::{SocketAddr, TcpListener}, +}; use tower::make::Shared; use tower_service::Service; diff --git a/examples/async-graphql/src/main.rs b/examples/async-graphql/src/main.rs index d6d9aa8c..1e7271ef 100644 --- a/examples/async-graphql/src/main.rs +++ b/examples/async-graphql/src/main.rs @@ -1,9 +1,15 @@ mod starwars; -use async_graphql::http::{playground_source, GraphQLPlaygroundConfig}; -use async_graphql::{EmptyMutation, EmptySubscription, Request, Response, Schema}; -use axum::response::IntoResponse; -use axum::{extract::Extension, response::Html, routing::get, AddExtensionLayer, Json, Router}; +use async_graphql::{ + http::{playground_source, GraphQLPlaygroundConfig}, + EmptyMutation, EmptySubscription, Request, Response, Schema, +}; +use axum::{ + extract::Extension, + response::{Html, IntoResponse}, + routing::get, + AddExtensionLayer, Json, Router, +}; use starwars::{QueryRoot, StarWars, StarWarsSchema}; async fn graphql_handler(schema: Extension, req: Json) -> Json { diff --git a/examples/async-graphql/src/starwars/model.rs b/examples/async-graphql/src/starwars/model.rs index 9acb2de8..80f562f1 100644 --- a/examples/async-graphql/src/starwars/model.rs +++ b/examples/async-graphql/src/starwars/model.rs @@ -1,6 +1,8 @@ use super::StarWars; -use async_graphql::connection::{query, Connection, Edge, EmptyFields}; -use async_graphql::{Context, Enum, FieldResult, Interface, Object}; +use async_graphql::{ + connection::{query, Connection, Edge, EmptyFields}, + Context, Enum, FieldResult, Interface, Object, +}; /// One of the films in the Star Wars Trilogy #[derive(Enum, Copy, Clone, Eq, PartialEq)] diff --git a/examples/customize-extractor-error/src/main.rs b/examples/customize-extractor-error/src/main.rs index 4b682faf..e0dc411a 100644 --- a/examples/customize-extractor-error/src/main.rs +++ b/examples/customize-extractor-error/src/main.rs @@ -6,8 +6,7 @@ use axum::{ async_trait, - extract::rejection::JsonRejection, - extract::{FromRequest, RequestParts}, + extract::{rejection::JsonRejection, FromRequest, RequestParts}, http::StatusCode, routing::post, BoxError, Router, diff --git a/examples/oauth/src/main.rs b/examples/oauth/src/main.rs index 51da6053..37c81fcd 100644 --- a/examples/oauth/src/main.rs +++ b/examples/oauth/src/main.rs @@ -148,6 +148,7 @@ async fn logout( } #[derive(Debug, Deserialize)] +#[allow(dead_code)] struct AuthRequest { code: String, state: String, diff --git a/examples/query-params-with-empty-strings/src/main.rs b/examples/query-params-with-empty-strings/src/main.rs index 3f851683..0b22f29c 100644 --- a/examples/query-params-with-empty-strings/src/main.rs +++ b/examples/query-params-with-empty-strings/src/main.rs @@ -31,6 +31,7 @@ async fn handler(Query(params): Query) -> String { /// /// [`serde_with`]: https://docs.rs/serde_with/1.11.0/serde_with/rust/string_empty_as_none/index.html #[derive(Debug, Deserialize)] +#[allow(dead_code)] struct Params { #[serde(default, deserialize_with = "empty_string_as_none")] foo: Option, diff --git a/examples/testing/src/main.rs b/examples/testing/src/main.rs index 02c459ce..765c601e 100644 --- a/examples/testing/src/main.rs +++ b/examples/testing/src/main.rs @@ -47,8 +47,10 @@ fn app() -> Router { #[cfg(test)] mod tests { use super::*; - use axum::body::Body; - use axum::http::{self, Request, StatusCode}; + use axum::{ + body::Body, + http::{self, Request, StatusCode}, + }; use serde_json::{json, Value}; use std::net::{SocketAddr, TcpListener}; use tower::ServiceExt; // for `app.oneshot()` diff --git a/examples/unix-domain-socket/src/main.rs b/examples/unix-domain-socket/src/main.rs index ff224490..d5c1c3c2 100644 --- a/examples/unix-domain-socket/src/main.rs +++ b/examples/unix-domain-socket/src/main.rs @@ -23,10 +23,9 @@ use std::{ sync::Arc, task::{Context, Poll}, }; -use tokio::net::{unix::UCred, UnixListener}; use tokio::{ io::{AsyncRead, AsyncWrite}, - net::UnixStream, + net::{unix::UCred, UnixListener, UnixStream}, }; use tower::BoxError; @@ -150,6 +149,7 @@ impl Connection for ClientConnection { } #[derive(Clone, Debug)] +#[allow(dead_code)] struct UdsConnectInfo { peer_addr: Arc, peer_cred: UCred, diff --git a/examples/versioning/src/main.rs b/examples/versioning/src/main.rs index 399ba210..03f6133a 100644 --- a/examples/versioning/src/main.rs +++ b/examples/versioning/src/main.rs @@ -13,8 +13,7 @@ use axum::{ routing::get, Router, }; -use std::collections::HashMap; -use std::net::SocketAddr; +use std::{collections::HashMap, net::SocketAddr}; #[tokio::main] async fn main() {