From a07060878e8b8304da003dc3f79ce4f32d36d863 Mon Sep 17 00:00:00 2001 From: Ethan Brierley Date: Thu, 21 May 2020 14:52:33 +0100 Subject: [PATCH 01/12] clippy::single_char_pattern fix --- src/cookies/middleware.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cookies/middleware.rs b/src/cookies/middleware.rs index 11131ad8a..dba8e9d36 100644 --- a/src/cookies/middleware.rs +++ b/src/cookies/middleware.rs @@ -82,7 +82,7 @@ impl CookieData { if let Some(cookie_headers) = req.header(&headers::COOKIE) { for cookie_header in cookie_headers { // spec says there should be only one, so this is permissive - for pair in cookie_header.as_str().split(";") { + for pair in cookie_header.as_str().split(';') { if let Ok(cookie) = Cookie::parse_encoded(String::from(pair)) { jar.add_original(cookie); } From a47be8264fb0ccfa50f25f0ba1f6edbebfe3ac55 Mon Sep 17 00:00:00 2001 From: Ethan Brierley Date: Thu, 21 May 2020 15:04:45 +0100 Subject: [PATCH 02/12] clippy::inefficient_to_string fix `&str` implements `ToString` through a slower blanket impl, but `str` has a fast specialization of `ToString` --- src/security/cors.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/security/cors.rs b/src/security/cors.rs index 0d67114ad..9a501c6ce 100644 --- a/src/security/cors.rs +++ b/src/security/cors.rs @@ -236,7 +236,11 @@ impl From> for Origin { impl From> for Origin { fn from(list: Vec<&str>) -> Self { - Origin::from(list.iter().map(|s| s.to_string()).collect::>()) + Origin::from( + list.iter() + .map(|s| (*s).to_string()) + .collect::>(), + ) } } From 92f62baefb66fd34820e76d60194b9dab31cac3e Mon Sep 17 00:00:00 2001 From: Ethan Brierley Date: Thu, 21 May 2020 15:07:52 +0100 Subject: [PATCH 03/12] clippy::clone_double_ref fix --- src/security/cors.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/security/cors.rs b/src/security/cors.rs index 9a501c6ce..c6cd84272 100644 --- a/src/security/cors.rs +++ b/src/security/cors.rs @@ -88,7 +88,7 @@ impl CorsMiddleware { fn build_preflight_response(&self, origin: &[HeaderValue]) -> http_types::Response { let mut response = http_types::Response::new(StatusCode::Ok); response - .insert_header(headers::ACCESS_CONTROL_ALLOW_ORIGIN, origin.clone()) + .insert_header(headers::ACCESS_CONTROL_ALLOW_ORIGIN, origin) .unwrap(); response .insert_header( From 2b50af09f161c86a4aaeee3e7b673ddc3b5b45dc Mon Sep 17 00:00:00 2001 From: Ethan Brierley Date: Thu, 21 May 2020 19:09:11 +0100 Subject: [PATCH 04/12] clippy::must_use_candidate --- src/lib.rs | 1 + src/log/middleware.rs | 1 + src/middleware.rs | 1 + src/request.rs | 8 ++++++++ src/response.rs | 8 ++++++++ src/route.rs | 1 + src/security/cors.rs | 4 +++- src/server.rs | 1 + 8 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index ca40a13d5..a2302e4e5 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -227,6 +227,7 @@ pub use http_types::{self as http, Body, Error, Status, StatusCode}; /// # /// # Ok(()) }) } /// ``` +#[must_use] pub fn new() -> server::Server<()> { Server::new() } diff --git a/src/log/middleware.rs b/src/log/middleware.rs index d985dda43..d9f9a614b 100644 --- a/src/log/middleware.rs +++ b/src/log/middleware.rs @@ -19,6 +19,7 @@ pub struct LogMiddleware { impl LogMiddleware { /// Create a new instance of `LogMiddleware`. + #[must_use] pub fn new() -> Self { Self { _priv: () } } diff --git a/src/middleware.rs b/src/middleware.rs index 4885b7a72..19424f01f 100644 --- a/src/middleware.rs +++ b/src/middleware.rs @@ -47,6 +47,7 @@ pub struct Next<'a, State> { impl<'a, State: 'static> Next<'a, State> { /// Asynchronously execute the remaining middleware chain. + #[must_use] pub fn run(mut self, req: Request) -> BoxFuture<'a, crate::Result> { if let Some((current, next)) = self.next_middleware.split_first() { self.next_middleware = next; diff --git a/src/request.rs b/src/request.rs index 79b07f411..29eba9bbb 100644 --- a/src/request.rs +++ b/src/request.rs @@ -59,6 +59,7 @@ impl Request { /// # /// # Ok(()) })} /// ``` + #[must_use] pub fn method(&self) -> Method { self.request.method() } @@ -82,6 +83,7 @@ impl Request { /// # /// # Ok(()) })} /// ``` + #[must_use] pub fn uri(&self) -> &Url { self.request.url() } @@ -105,6 +107,7 @@ impl Request { /// # /// # Ok(()) })} /// ``` + #[must_use] pub fn version(&self) -> Option { self.request.version() } @@ -128,6 +131,7 @@ impl Request { /// # /// # Ok(()) })} /// ``` + #[must_use] pub fn header( &self, key: &http_types::headers::HeaderName, @@ -136,6 +140,7 @@ impl Request { } /// Get a local value. + #[must_use] pub fn local(&self) -> Option<&T> { self.request.local().get() } @@ -146,6 +151,7 @@ impl Request { self } + #[must_use] /// Access app-global state. pub fn state(&self) -> &State { &self.state @@ -293,6 +299,7 @@ impl Request { } /// returns a `Cookie` by name of the cookie. + #[must_use] pub fn cookie(&self, name: &str) -> Option> { let cookie_data = self .local::() @@ -303,6 +310,7 @@ impl Request { } /// Get the length of the body. + #[must_use] pub fn len(&self) -> Option { self.request.len() } diff --git a/src/response.rs b/src/response.rs index 9cc6e864c..26316666b 100644 --- a/src/response.rs +++ b/src/response.rs @@ -25,6 +25,7 @@ pub struct Response { impl Response { /// Create a new instance. + #[must_use] pub fn new(status: StatusCode) -> Self { let res = http_types::Response::new(status); Self { @@ -72,22 +73,26 @@ impl Response { } /// Returns the statuscode. + #[must_use] pub fn status(&self) -> crate::StatusCode { self.res.status() } /// Set the statuscode. + #[must_use] pub fn set_status(mut self, status: crate::StatusCode) -> Self { self.res.set_status(status); self } /// Get the length of the body. + #[must_use] pub fn len(&self) -> Option { self.res.len() } /// Get an HTTP header. + #[must_use] pub fn header(&self, name: &HeaderName) -> Option<&Vec> { self.res.header(name) } @@ -126,6 +131,7 @@ impl Response { /// Set the request MIME. /// /// [Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) + #[must_use] pub fn set_mime(self, mime: Mime) -> Self { self.set_header(http_types::headers::CONTENT_TYPE, format!("{}", mime)) } @@ -135,6 +141,7 @@ impl Response { /// # Mime /// /// The encoding is set to `text/plain; charset=utf-8`. + #[must_use] pub fn body_string(mut self, string: String) -> Self { self.res.set_body(string); self.set_mime(mime::TEXT_PLAIN_UTF_8) @@ -221,6 +228,7 @@ impl Response { } /// Get a local value. + #[must_use] pub fn local(&self) -> Option<&T> { self.res.local().get() } diff --git a/src/route.rs b/src/route.rs index 39d8ccc70..7865c6187 100644 --- a/src/route.rs +++ b/src/route.rs @@ -60,6 +60,7 @@ impl<'a, State: 'static> Route<'a, State> { } /// Get the current path. + #[must_use] pub fn path(&self) -> &str { &self.path } diff --git a/src/security/cors.rs b/src/security/cors.rs index c6cd84272..cb482b620 100644 --- a/src/security/cors.rs +++ b/src/security/cors.rs @@ -14,7 +14,7 @@ use crate::{Request, Result}; /// use http_types::headers::HeaderValue; /// use tide::security::{CorsMiddleware, Origin}; /// -/// CorsMiddleware::new() +/// let cors = CorsMiddleware::new() /// .allow_methods("GET, POST, OPTIONS".parse::().unwrap()) /// .allow_origin(Origin::from("*")) /// .allow_credentials(false); @@ -35,6 +35,7 @@ pub const WILDCARD: &str = "*"; impl CorsMiddleware { /// Creates a new Cors middleware. + #[must_use] pub fn new() -> Self { Self { allow_credentials: None, @@ -47,6 +48,7 @@ impl CorsMiddleware { } /// Set allow_credentials and return new Cors + #[must_use] pub fn allow_credentials(mut self, allow_credentials: bool) -> Self { self.allow_credentials = match allow_credentials.to_string().parse() { Ok(header) => Some(header), diff --git a/src/server.rs b/src/server.rs index 4185ec164..df19908a3 100644 --- a/src/server.rs +++ b/src/server.rs @@ -150,6 +150,7 @@ impl Server<()> { /// # /// # Ok(()) }) } /// ``` + #[must_use] pub fn new() -> Server<()> { Self::with_state(()) } From 9cfbea4946daa3d9c81a7763ae81f2720578bbb5 Mon Sep 17 00:00:00 2001 From: Ethan Brierley Date: Thu, 21 May 2020 19:28:16 +0100 Subject: [PATCH 05/12] clippy::single-match-else --- src/fs/serve_dir.rs | 11 +++++------ src/security/cors.rs | 11 +++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/fs/serve_dir.rs b/src/fs/serve_dir.rs index ab9f269af..e5822a1f7 100644 --- a/src/fs/serve_dir.rs +++ b/src/fs/serve_dir.rs @@ -57,12 +57,11 @@ impl Endpoint for ServeDir { Ok(file) => file, }; - let len = match file.metadata().await { - Ok(metadata) => metadata.len() as usize, - Err(_) => { - log::warn!("Could not retrieve metadata"); - return Ok(Response::new(StatusCode::InternalServerError)); - } + let len = if let Ok(metadata) = file.metadata().await { + metadata.len() as usize + } else { + log::warn!("Could not retrieve metadata"); + return Ok(Response::new(StatusCode::InternalServerError)); }; let body = Body::from_reader(BufReader::new(file), Some(len)); diff --git a/src/security/cors.rs b/src/security/cors.rs index cb482b620..c99106fdd 100644 --- a/src/security/cors.rs +++ b/src/security/cors.rs @@ -153,12 +153,11 @@ impl Middleware for CorsMiddleware { let origins = req.header(&headers::ORIGIN).cloned().unwrap_or_default(); // TODO: how should multiple origin values be handled? - let origin = match origins.first() { - Some(origin) => origin, - None => { - // This is not a CORS request if there is no Origin header - return next.run(req).await; - } + let origin = if let Some(origin) = origins.first() { + origin + } else { + // This is not a CORS request if there is no Origin header + return next.run(req).await; }; if !self.is_valid_origin(origin) { From 955a4031943a440986b55c7a0d5aeda862b1c7ac Mon Sep 17 00:00:00 2001 From: Ethan Brierley Date: Thu, 21 May 2020 19:31:50 +0100 Subject: [PATCH 06/12] clippy::filter_map_next --- src/request.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/request.rs b/src/request.rs index 29eba9bbb..82a00e01d 100644 --- a/src/request.rs +++ b/src/request.rs @@ -177,8 +177,7 @@ impl Request { self.route_params .iter() .rev() - .filter_map(|params| params.find(key)) - .next() + .find_map(|params| params.find(key)) .unwrap() .parse() } From b9a216ce9673e250f9c03d20692d6573fd84af80 Mon Sep 17 00:00:00 2001 From: Ethan Brierley Date: Thu, 21 May 2020 19:38:17 +0100 Subject: [PATCH 07/12] clippy::doc-markdown --- src/cookies/middleware.rs | 2 +- src/security/cors.rs | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/cookies/middleware.rs b/src/cookies/middleware.rs index dba8e9d36..96cffc362 100644 --- a/src/cookies/middleware.rs +++ b/src/cookies/middleware.rs @@ -25,7 +25,7 @@ use std::sync::{Arc, RwLock}; pub(crate) struct CookiesMiddleware; impl CookiesMiddleware { - /// Creates a new CookiesMiddleware. + /// Creates a new `CookiesMiddleware`. pub fn new() -> Self { Self::default() } diff --git a/src/security/cors.rs b/src/security/cors.rs index c99106fdd..48b80f686 100644 --- a/src/security/cors.rs +++ b/src/security/cors.rs @@ -47,7 +47,7 @@ impl CorsMiddleware { } } - /// Set allow_credentials and return new Cors + /// Set `allow_credentials` and return new Cors #[must_use] pub fn allow_credentials(mut self, allow_credentials: bool) -> Self { self.allow_credentials = match allow_credentials.to_string().parse() { @@ -57,31 +57,31 @@ impl CorsMiddleware { self } - /// Set allow_headers and return new Cors + /// Set `allow_headers` and return new Cors pub fn allow_headers>(mut self, headers: T) -> Self { self.allow_headers = headers.into(); self } - /// Set max_age and return new Cors + /// Set `max_age` and return new Cors pub fn max_age>(mut self, max_age: T) -> Self { self.max_age = max_age.into(); self } - /// Set allow_methods and return new Cors + /// Set `allow_methods` and return new Cors pub fn allow_methods>(mut self, methods: T) -> Self { self.allow_methods = methods.into(); self } - /// Set allow_origin and return new Cors + /// Set `allow_origin` and return new Cors pub fn allow_origin>(mut self, origin: T) -> Self { self.allow_origin = origin.into(); self } - /// Set expose_headers and return new Cors + /// Set `expose_headers` and return new Cors pub fn expose_headers>(mut self, headers: T) -> Self { self.expose_headers = Some(headers.into()); self @@ -123,7 +123,7 @@ impl CorsMiddleware { response } - /// Look at origin of request and determine allow_origin + /// Look at origin of request and determine `allow_origin` fn response_origin(&self, origin: &HeaderValue) -> Option { if !self.is_valid_origin(origin) { return None; @@ -199,7 +199,7 @@ impl Default for CorsMiddleware { } } -/// allow_origin enum +/// `allow_origin` enum #[derive(Clone, Debug, Hash, PartialEq)] pub enum Origin { /// Wildcard. Accept all origin requests From ff309f4459e922bfd8fa549997b4e632db3ad966 Mon Sep 17 00:00:00 2001 From: Ethan Brierley Date: Thu, 21 May 2020 19:40:44 +0100 Subject: [PATCH 08/12] clippy::new_without_default --- src/log/middleware.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/log/middleware.rs b/src/log/middleware.rs index d9f9a614b..5f18e4530 100644 --- a/src/log/middleware.rs +++ b/src/log/middleware.rs @@ -12,7 +12,7 @@ use crate::{Middleware, Next, Request}; /// let mut app = tide::Server::new(); /// app.middleware(tide::log::LogMiddleware::new()); /// ``` -#[derive(Debug, Clone)] +#[derive(Debug, Default, Clone)] pub struct LogMiddleware { _priv: (), } From a732a1f99066cb05d90fd3287a285a416275a3a0 Mon Sep 17 00:00:00 2001 From: Ethan Brierley Date: Thu, 21 May 2020 20:01:00 +0100 Subject: [PATCH 09/12] clippy::use-self --- src/cookies/middleware.rs | 2 +- src/request.rs | 4 ++-- src/response.rs | 2 +- src/router.rs | 4 ++-- src/security/cors.rs | 10 +++++----- src/server.rs | 8 ++++---- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/cookies/middleware.rs b/src/cookies/middleware.rs index 96cffc362..24f640f89 100644 --- a/src/cookies/middleware.rs +++ b/src/cookies/middleware.rs @@ -90,7 +90,7 @@ impl CookieData { } } - CookieData { + Self { content: Arc::new(RwLock::new(jar)), } } diff --git a/src/request.rs b/src/request.rs index 82a00e01d..6e801f5cb 100644 --- a/src/request.rs +++ b/src/request.rs @@ -32,8 +32,8 @@ impl Request { state: Arc, request: http_types::Request, route_params: Vec, - ) -> Request { - Request { + ) -> Self { + Self { state, request, route_params, diff --git a/src/response.rs b/src/response.rs index 26316666b..e06242af3 100644 --- a/src/response.rs +++ b/src/response.rs @@ -174,7 +174,7 @@ impl Response { pub async fn body_form( mut self, form: T, - ) -> Result { + ) -> Result { // TODO: think about how to handle errors self.res.set_body(serde_qs::to_string(&form)?.into_bytes()); Ok(self diff --git a/src/router.rs b/src/router.rs index 63e1e6aa8..ba1ce26b3 100644 --- a/src/router.rs +++ b/src/router.rs @@ -22,8 +22,8 @@ pub(crate) struct Selection<'a, State> { } impl Router { - pub(crate) fn new() -> Router { - Router { + pub(crate) fn new() -> Self { + Self { method_map: HashMap::default(), all_method_router: MethodRouter::new(), } diff --git a/src/security/cors.rs b/src/security/cors.rs index 48b80f686..350942299 100644 --- a/src/security/cors.rs +++ b/src/security/cors.rs @@ -213,15 +213,15 @@ pub enum Origin { impl From for Origin { fn from(s: String) -> Self { if s == "*" { - return Origin::Any; + return Self::Any; } - Origin::Exact(s) + Self::Exact(s) } } impl From<&str> for Origin { fn from(s: &str) -> Self { - Origin::from(s.to_string()) + Self::from(s.to_string()) } } @@ -231,13 +231,13 @@ impl From> for Origin { return Self::from(list[0].clone()); } - Origin::List(list) + Self::List(list) } } impl From> for Origin { fn from(list: Vec<&str>) -> Self { - Origin::from( + Self::from( list.iter() .map(|s| (*s).to_string()) .collect::>(), diff --git a/src/server.rs b/src/server.rs index df19908a3..ded42700b 100644 --- a/src/server.rs +++ b/src/server.rs @@ -151,13 +151,13 @@ impl Server<()> { /// # Ok(()) }) } /// ``` #[must_use] - pub fn new() -> Server<()> { + pub fn new() -> Self { Self::with_state(()) } } impl Default for Server<()> { - fn default() -> Server<()> { + fn default() -> Self { Self::new() } } @@ -194,8 +194,8 @@ impl Server { /// # /// # Ok(()) }) } /// ``` - pub fn with_state(state: State) -> Server { - let mut server = Server { + pub fn with_state(state: State) -> Self { + let mut server = Self { router: Arc::new(Router::new()), middleware: Arc::new(vec![]), state: Arc::new(state), From a66706d1cb2dc37544d38831cc56e971ceb41c49 Mon Sep 17 00:00:00 2001 From: Ethan Brierley Date: Thu, 21 May 2020 20:08:04 +0100 Subject: [PATCH 10/12] clippy::needless-borrow --- src/security/cors.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/security/cors.rs b/src/security/cors.rs index 350942299..7edf74344 100644 --- a/src/security/cors.rs +++ b/src/security/cors.rs @@ -173,7 +173,7 @@ impl Middleware for CorsMiddleware { response .insert_header( headers::ACCESS_CONTROL_ALLOW_ORIGIN, - self.response_origin(&origin).unwrap(), + self.response_origin(origin).unwrap(), ) .unwrap(); From 719cc3b09f803a1633e317e79e714c394c9801e8 Mon Sep 17 00:00:00 2001 From: Ethan Brierley Date: Thu, 21 May 2020 20:13:13 +0100 Subject: [PATCH 11/12] clippy::needless_doctest_main --- src/endpoint.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/endpoint.rs b/src/endpoint.rs index 65ee503e9..b955362ed 100644 --- a/src/endpoint.rs +++ b/src/endpoint.rs @@ -27,10 +27,8 @@ use crate::{Middleware, Request, Response}; /// Ok(String::from("hello")) /// } /// -/// fn main() { -/// let mut app = tide::Server::new(); -/// app.at("/hello").get(hello); -/// } +/// let mut app = tide::Server::new(); +/// app.at("/hello").get(hello); /// ``` /// /// An endpoint with similar functionality that does not make use of the `async` keyword would look something like this: @@ -41,10 +39,8 @@ use crate::{Middleware, Request, Response}; /// async_std::future::ready(Ok(String::from("hello"))) /// } /// -/// fn main() { -/// let mut app = tide::Server::new(); -/// app.at("/hello").get(hello); -/// } +/// let mut app = tide::Server::new(); +/// app.at("/hello").get(hello); /// ``` /// /// Tide routes will also accept endpoints with `Fn` signatures of this form, but using the `async` keyword has better ergonomics. From 40a353ad130dbbf8afeab4d734dfd828aac2edd8 Mon Sep 17 00:00:00 2001 From: Ethan Brierley Date: Thu, 21 May 2020 20:33:31 +0100 Subject: [PATCH 12/12] clippy::len_without_is_empty --- src/request.rs | 5 +++++ src/response.rs | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/src/request.rs b/src/request.rs index 6e801f5cb..e47a744ed 100644 --- a/src/request.rs +++ b/src/request.rs @@ -313,6 +313,11 @@ impl Request { pub fn len(&self) -> Option { self.request.len() } + /// Checks if the body is empty. + #[must_use] + pub fn is_empty(&self) -> Option { + Some(self.request.len()? == 0) + } } impl AsMut for Request { diff --git a/src/response.rs b/src/response.rs index e06242af3..aeb5185ae 100644 --- a/src/response.rs +++ b/src/response.rs @@ -91,6 +91,12 @@ impl Response { self.res.len() } + /// Checks if the body is empty. + #[must_use] + pub fn is_empty(&self) -> Option { + Some(self.res.len()? == 0) + } + /// Get an HTTP header. #[must_use] pub fn header(&self, name: &HeaderName) -> Option<&Vec> {