From 4746f4b38874e39eef2fafec3f21d378bf3ec289 Mon Sep 17 00:00:00 2001 From: Richard Bradfield Date: Mon, 10 Jun 2024 16:33:31 +0100 Subject: [PATCH] Rustup and Clippy fixes for Rust 1.78 --- examples/database.rs | 6 +++--- examples/git-http-backend.rs | 4 ++-- examples/login-session.rs | 12 ++++-------- examples/php.rs | 2 +- examples/reverse-proxy.rs | 2 +- examples/simple-form.rs | 4 ++-- examples/static-files.rs | 2 +- examples/websocket.rs | 2 +- src/assets.rs | 4 ++-- src/cgi.rs | 4 ++-- src/content_encoding.rs | 4 ++-- src/input/basic_http_auth.rs | 7 +++++-- src/input/cookies.rs | 2 +- src/input/post.rs | 6 +++--- src/lib.rs | 10 +++++----- src/response.rs | 6 +++--- src/session.rs | 4 ++-- src/websocket/mod.rs | 6 +++--- src/websocket/websocket.rs | 2 +- 19 files changed, 44 insertions(+), 45 deletions(-) diff --git a/examples/database.rs b/examples/database.rs index 087239165..bf88a5ed9 100644 --- a/examples/database.rs +++ b/examples/database.rs @@ -85,7 +85,7 @@ fn main() { let mut db = db.transaction().unwrap(); // For better readability, we handle the request in a separate function. - let response = note_routes(&request, &mut db); + let response = note_routes(request, &mut db); // If the response is a success, we commit the transaction before returning. It's only at // this point that data are actually written in the database. @@ -148,7 +148,7 @@ fn note_routes(request: &Request, db: &mut Transaction) -> Response { // This route modifies the content of an existing note. // We start by reading the body of the HTTP request into a `String`. - let body = try_or_400!(rouille::input::plain_text_body(&request)); + let body = try_or_400!(rouille::input::plain_text_body(request)); // And write the content with a query. This line can only panic if the // SQL is malformed. @@ -168,7 +168,7 @@ fn note_routes(request: &Request, db: &mut Transaction) -> Response { // This route creates a new note whose initial content is the body. // We start by reading the body of the HTTP request into a `String`. - let body = try_or_400!(rouille::input::plain_text_body(&request)); + let body = try_or_400!(rouille::input::plain_text_body(request)); // To do so, we first create a variable that will receive the content. let mut id: Option = None; diff --git a/examples/git-http-backend.rs b/examples/git-http-backend.rs index 4dc00da3b..e901a06af 100644 --- a/examples/git-http-backend.rs +++ b/examples/git-http-backend.rs @@ -22,7 +22,7 @@ fn main() { println!("Now listening on localhost:8000"); rouille::start_server("localhost:8000", move |request| { - rouille::log(&request, io::stdout(), || { + rouille::log(request, io::stdout(), || { // When a request is received, we invoke the `git http-backend` command through CGI. let mut cmd = Command::new("git"); cmd.arg("http-backend"); @@ -42,7 +42,7 @@ fn main() { // Note that an error is returned only if `git http-backend` fails to execute, and not // if the client sends bad data for example. In other words, an error can only occur // if the server was misconfigured. Therefore it's okay-ish to call `unwrap()` here. - cmd.start_cgi(&request).unwrap() + cmd.start_cgi(request).unwrap() }) }); } diff --git a/examples/login-session.rs b/examples/login-session.rs index 9c11d3675..87bb61cfa 100644 --- a/examples/login-session.rs +++ b/examples/login-session.rs @@ -34,7 +34,7 @@ fn main() { let sessions_storage: Mutex> = Mutex::new(HashMap::new()); rouille::start_server("localhost:8000", move |request| { - rouille::log(&request, io::stdout(), || { + rouille::log(request, io::stdout(), || { // We call `session::session` in order to assign a unique identifier to each client. // This identifier is tracked through a cookie that is automatically appended to the // response. @@ -48,11 +48,7 @@ fn main() { // // We thus obtain a `Option`. let mut session_data = if session.client_has_sid() { - if let Some(data) = sessions_storage.lock().unwrap().get(session.id()) { - Some(data.clone()) - } else { - None - } + sessions_storage.lock().unwrap().get(session.id()).map(|data| data.clone()) } else { None }; @@ -60,7 +56,7 @@ fn main() { // Use a separate function to actually handle the request, for readability. // We pass a mutable reference to the `Option` so that the function // is free to modify it. - let response = handle_route(&request, &mut session_data); + let response = handle_route(request, &mut session_data); // Since the function call to `handle_route` can modify the session data, we have // to store it back in the `sessions_storage` when necessary. @@ -117,7 +113,7 @@ fn handle_route(request: &Request, session_data: &mut Option) -> Re // In this example all login attempts are successful in the password starts with the // letter 'b'. Of course in a real website you should check the credentials in a proper // way. - if data.password.starts_with("b") { + if data.password.starts_with('b') { // Logging the user in is done by writing the content of `session_data`. // // A minor warning here: in this demo we store in memory directly the data that diff --git a/examples/php.rs b/examples/php.rs index 8a12e9ca3..99a497f3b 100644 --- a/examples/php.rs +++ b/examples/php.rs @@ -19,6 +19,6 @@ fn main() { cmd.arg("-n"); // Don't use a php.ini. cmd.env("SCRIPT_FILENAME", "examples/php-test.php"); // The PHP script to use. cmd.env("REDIRECT_STATUS", "1"); // Necessary for security. - cmd.start_cgi(&request).unwrap() + cmd.start_cgi(request).unwrap() }); } diff --git a/examples/reverse-proxy.rs b/examples/reverse-proxy.rs index faccc5833..80b897800 100644 --- a/examples/reverse-proxy.rs +++ b/examples/reverse-proxy.rs @@ -16,7 +16,7 @@ fn main() { rouille::start_server("localhost:8000", move |request| { rouille::proxy::full_proxy( - &request, + request, rouille::proxy::ProxyConfig { addr: "example.com:80", replace_host: Some("example.com".into()), diff --git a/examples/simple-form.rs b/examples/simple-form.rs index 322c98a2c..627948ef7 100644 --- a/examples/simple-form.rs +++ b/examples/simple-form.rs @@ -11,7 +11,7 @@ fn main() { println!("Now listening on localhost:8000"); rouille::start_server("localhost:8000", move |request| { - rouille::log(&request, io::stdout(), || { + rouille::log(request, io::stdout(), || { router!(request, (GET) (/) => { // When viewing the home page, we return an HTML document described below. @@ -46,7 +46,7 @@ fn main() { } // The HTML document of the home page. -static FORM: &'static str = r#" +static FORM: &str = r#" Form diff --git a/examples/static-files.rs b/examples/static-files.rs index 364d14800..e2e7a3369 100644 --- a/examples/static-files.rs +++ b/examples/static-files.rs @@ -25,7 +25,7 @@ fn main() { // located. // In order to avoid potential security threats, `match_assets` will never return any // file outside of this directory even if the URL is for example `/../../foo.txt`. - let response = rouille::match_assets(&request, "."); + let response = rouille::match_assets(request, "."); // If a file is found, the `match_assets` function will return a response with a 200 // status code and the content of the file. If no file is found, it will instead return diff --git a/examples/websocket.rs b/examples/websocket.rs index b75d34e3c..b9f99454b 100644 --- a/examples/websocket.rs +++ b/examples/websocket.rs @@ -58,7 +58,7 @@ fn main() { // function, and a `websocket` variable of type `Receiver`. // Once the response has been sent back to the client, the `Receiver` will be // filled by rouille with a `Websocket` object representing the websocket. - let (response, websocket) = try_or_400!(websocket::start(&request, Some("echo"))); + let (response, websocket) = try_or_400!(websocket::start(request, Some("echo"))); // Because of the nature of I/O in Rust, we need to spawn a separate thread for // each websocket. diff --git a/src/assets.rs b/src/assets.rs index 1bda00d40..b6340ca19 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -77,9 +77,9 @@ use Response; /// In this example, a request made to `/static/test.txt` will return the file /// `public/test.txt` if it exists. /// -pub fn match_assets(request: &Request, path: &P) -> Response +pub fn match_assets

(request: &Request, path: &P) -> Response where - P: AsRef, + P: AsRef + ?Sized, { let path = path.as_ref(); let path = match path.canonicalize() { diff --git a/src/cgi.rs b/src/cgi.rs index d625670d2..59d2f4937 100644 --- a/src/cgi.rs +++ b/src/cgi.rs @@ -120,11 +120,11 @@ impl CgiRun for Command { .env("REMOTE_USER", "") // FIXME: .env( "CONTENT_TYPE", - &request.header("Content-Type").unwrap_or(""), + request.header("Content-Type").unwrap_or(""), ) .env( "CONTENT_LENGTH", - &request.header("Content-Length").unwrap_or(""), + request.header("Content-Length").unwrap_or(""), ) .stdout(Stdio::piped()) .stderr(Stdio::inherit()) diff --git a/src/content_encoding.rs b/src/content_encoding.rs index 6b0f6e08b..332f6c30f 100644 --- a/src/content_encoding.rs +++ b/src/content_encoding.rs @@ -64,7 +64,7 @@ pub fn apply(request: &Request, mut response: Response) -> Response { if response .headers .iter() - .any(|&(ref key, _)| key.eq_ignore_ascii_case("Content-Encoding")) + .any(|(key, _)| key.eq_ignore_ascii_case("Content-Encoding")) { return response; } @@ -91,7 +91,7 @@ pub fn apply(request: &Request, mut response: Response) -> Response { // Since encoding is purely an optimization, it's not a problem if the function sometimes has // false positives or false negatives. fn response_is_text(response: &Response) -> bool { - response.headers.iter().any(|&(ref key, ref value)| { + response.headers.iter().any(|(key, value)| { if !key.eq_ignore_ascii_case("Content-Type") { return false; } diff --git a/src/input/basic_http_auth.rs b/src/input/basic_http_auth.rs index 461b35663..4d3116394 100644 --- a/src/input/basic_http_auth.rs +++ b/src/input/basic_http_auth.rs @@ -17,7 +17,7 @@ //! - In order to read a plain text body, see //! [the `plain_text_body` function](fn.plain_text_body.html). -use base64::{Engine as _, prelude::BASE64_STANDARD}; +use base64::{prelude::BASE64_STANDARD, Engine as _}; use Request; /// Credentials returned by `basic_http_auth`. @@ -73,7 +73,10 @@ pub fn basic_http_auth(request: &Request) -> Option { return None; } - let authvalue = match split.next().and_then(|val| BASE64_STANDARD.decode(val).ok()) { + let authvalue = match split + .next() + .and_then(|val| BASE64_STANDARD.decode(val).ok()) + { Some(v) => v, None => return None, }; diff --git a/src/input/cookies.rs b/src/input/cookies.rs index 7752ea336..cf17a24c9 100644 --- a/src/input/cookies.rs +++ b/src/input/cookies.rs @@ -109,7 +109,7 @@ mod test { assert_eq!( cookies(&request).collect::>(), - vec![("a".into(), "b".into()), ("hello".into(), "world".into())] + vec![("a", "b"), ("hello", "world")] ); } } diff --git a/src/input/post.rs b/src/input/post.rs index 1cef19ddb..725dabe81 100644 --- a/src/input/post.rs +++ b/src/input/post.rs @@ -802,7 +802,7 @@ mod tests { let input = post_input!(&request, { field: bool }).unwrap(); - assert_eq!(input.field, true); + assert!(input.field); } #[test] @@ -908,7 +908,7 @@ mod tests { let input = post_input!(&request, { field: bool }).unwrap(); - assert_eq!(input.field, true); + assert!(input.field); } #[test] @@ -1051,7 +1051,7 @@ mod tests { let input = post_input!(&request, { field: bool }).unwrap(); - assert_eq!(input.field, false); + assert!(!input.field); } #[test] diff --git a/src/lib.rs b/src/lib.rs index 335538e73..37cf6cca6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -939,8 +939,8 @@ impl Request { pub fn header(&self, key: &str) -> Option<&str> { self.headers .iter() - .find(|&&(ref k, _)| k.eq_ignore_ascii_case(key)) - .map(|&(_, ref v)| &v[..]) + .find(|&(k, _)| k.eq_ignore_ascii_case(key)) + .map(|(_, v)| &v[..]) } /// Returns a list of all the headers of the request. @@ -973,8 +973,8 @@ impl Request { /// ``` pub fn do_not_track(&self) -> Option { match self.header("DNT") { - Some(h) if h == "1" => Some(true), - Some(h) if h == "0" => Some(false), + Some("1") => Some(true), + Some("0") => Some(false), _ => None, } } @@ -1044,7 +1044,7 @@ impl<'a> Iterator for HeadersIter<'a> { #[inline] fn next(&mut self) -> Option { - self.iter.next().map(|&(ref k, ref v)| (&k[..], &v[..])) + self.iter.next().map(|(k, v)| (&k[..], &v[..])) } #[inline] diff --git a/src/response.rs b/src/response.rs index 4b4338875..8f8f2ee4d 100644 --- a/src/response.rs +++ b/src/response.rs @@ -503,7 +503,7 @@ impl Response { /// Removes all headers from the response that match `header`. pub fn without_header(mut self, header: &str) -> Response { self.headers - .retain(|&(ref h, _)| !h.eq_ignore_ascii_case(header)); + .retain(|(h, _)| !h.eq_ignore_ascii_case(header)); self } @@ -534,7 +534,7 @@ impl Response { let header = header.into(); let mut found_one = false; - self.headers.retain(|&(ref h, _)| { + self.headers.retain(|(h, _)| { if h.eq_ignore_ascii_case(&header) { if !found_one { found_one = true; @@ -597,7 +597,7 @@ impl Response { } let mut not_modified = false; - for &(ref key, ref etag) in &self.headers { + for (key, etag) in &self.headers { if !key.eq_ignore_ascii_case("ETag") { continue; } diff --git a/src/session.rs b/src/session.rs index 847b92005..9fe02fca0 100644 --- a/src/session.rs +++ b/src/session.rs @@ -49,7 +49,7 @@ where F: FnOnce(&Session<'r>) -> Response, { let mut cookie = input::cookies(request); - let cookie = cookie.find(|&(ref k, _)| k == &cookie_name); + let cookie = cookie.find(|(k, _)| k == &cookie_name); let cookie = cookie.map(|(_, v)| v); let session = if let Some(cookie) = cookie { @@ -125,7 +125,7 @@ pub fn generate_session_id() -> String { .sample_iter(&Alphanumeric) .map(char::from) .filter(|&c| { - ('a'..='z').contains(&c) || ('A'..='Z').contains(&c) || ('0'..='9').contains(&c) + c.is_ascii_lowercase() || c.is_ascii_uppercase() || c.is_ascii_digit() }) .take(64) .collect::() diff --git a/src/websocket/mod.rs b/src/websocket/mod.rs index 32d7e6da1..ede2370de 100644 --- a/src/websocket/mod.rs +++ b/src/websocket/mod.rs @@ -69,7 +69,7 @@ pub use self::websocket::Message; pub use self::websocket::SendError; pub use self::websocket::Websocket; -use base64::{Engine as _, prelude::BASE64_STANDARD}; +use base64::{prelude::BASE64_STANDARD, Engine as _}; use sha1_smol::Sha1; use std::borrow::Cow; use std::error; @@ -152,7 +152,7 @@ where // TODO: there are some version shenanigans to handle // see https://tools.ietf.org/html/rfc6455#section-4.4 match request.header("Sec-WebSocket-Version") { - Some(h) if h == "13" => (), + Some("13") => (), _ => return Err(WebsocketError::InvalidWebsocketRequest), } @@ -246,5 +246,5 @@ fn convert_key(input: &str) -> String { sha1.update(input.as_bytes()); sha1.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"); - BASE64_STANDARD.encode(&sha1.digest().bytes()) + BASE64_STANDARD.encode(sha1.digest().bytes()) } diff --git a/src/websocket/websocket.rs b/src/websocket/websocket.rs index 51248134c..57d420577 100644 --- a/src/websocket/websocket.rs +++ b/src/websocket/websocket.rs @@ -142,7 +142,7 @@ impl Iterator for Websocket { // Read `n` bytes in `buf`. let mut buf = [0; 256]; let n = match self.socket.as_mut().unwrap().read(&mut buf) { - Ok(n) if n == 0 => { + Ok(0) => { // Read returning zero means EOF self.socket = None; return None;