From 03b82171f70815e43fbc64d120a20e7a1eebd0bc Mon Sep 17 00:00:00 2001 From: Richard Walters Date: Wed, 14 Oct 2020 23:09:54 -0700 Subject: Fix new clippy warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Suppress clippy::option_if_let_else in parse_host_port::State::next_percent_encoded_character because the warning recommends Option::map_or which we cannot use because it leads to using a moved value. * Use matches! instead in uri::Uri::is_path_absolute as recommended. * Use and_then instead of if let/else in uri::Uri::port as recommended; note we need to replace the & with as_ref for self.authority. * In uri::Uri::resolve, replace the large if let/else with map_or as recommended, although it's not clear if it makes it any easier to read. ¯\_(ツ)_/¯ --- src/parse_host_port.rs | 5 +++ src/uri.rs | 104 ++++++++++++++++++++++--------------------------- 2 files changed, 52 insertions(+), 57 deletions(-) diff --git a/src/parse_host_port.rs b/src/parse_host_port.rs index e7d65c0..ae4a278 100644 --- a/src/parse_host_port.rs +++ b/src/parse_host_port.rs @@ -121,6 +121,11 @@ impl State{ fn next_percent_encoded_character(state: Shared, c: char) -> Result { let mut state = state; + // We can't use `Option::map_or` (or `Option::map_or_else`, for similar + // reasons) in this case because the closure would take ownership of + // `state`, preventing it from being used to construct the default + // value. + #[allow(clippy::option_if_let_else)] if let Some(ci) = state.pec_decoder.next(c)? { state.host.push(ci); Ok(Self::NotIpLiteral(state)) diff --git a/src/uri.rs b/src/uri.rs index cbc4749..3cb235b 100644 --- a/src/uri.rs +++ b/src/uri.rs @@ -205,10 +205,7 @@ impl Uri { fn is_path_absolute(path: T) -> bool where T: AsRef<[Vec]> { - match path.as_ref() { - [segment, ..] if segment.is_empty() => true, - _ => false - } + matches!(path.as_ref(), [segment, ..] if segment.is_empty()) } /// Determines if the URI is a `relative-ref` (relative reference), as @@ -457,11 +454,7 @@ impl Uri { /// Return a copy of the port (if any) contained in the URI. #[must_use = "why did you get the port number and then throw it away?"] pub fn port(&self) -> Option { - if let Some(authority) = &self.authority { - authority.port() - } else { - None - } + self.authority.as_ref().and_then(Authority::port) } /// Borrow the query (if any) of the URI. @@ -514,58 +507,59 @@ impl Uri { relative_reference.query.clone() ) } else { - let scheme = self.scheme.clone(); - if let Some(authority) = &relative_reference.authority { - ( - scheme, - Some(authority.clone()), - Self::normalize_path(&relative_reference.path), - relative_reference.query.clone() - ) - } else { - let authority = self.authority.clone(); - if relative_reference.path.is_empty() { - let path = self.path.clone(); - let query = if relative_reference.query.is_none() { - self.query.clone() - } else { - relative_reference.query.clone() - }; - ( - scheme, - authority, - path, - query - ) - } else { - let query = relative_reference.query.clone(); - - // RFC describes this as: - // "if (R.path starts-with "/") then" - if Self::is_path_absolute(&relative_reference.path) { + relative_reference.authority.as_ref().map_or_else( + || { + let scheme = self.scheme.clone(); + let authority = self.authority.clone(); + if relative_reference.path.is_empty() { + let path = self.path.clone(); + let query = if relative_reference.query.is_none() { + self.query.clone() + } else { + relative_reference.query.clone() + }; ( scheme, authority, - relative_reference.path.clone(), + path, query ) } else { + let query = relative_reference.query.clone(); + // RFC describes this as: - // "T.path = merge(Base.path, R.path);" - let mut path = self.path.clone(); - if path.len() > 1 { - path.pop(); + // "if (R.path starts-with "/") then" + if Self::is_path_absolute(&relative_reference.path) { + ( + scheme, + authority, + relative_reference.path.clone(), + query + ) + } else { + // RFC describes this as: + // "T.path = merge(Base.path, R.path);" + let mut path = self.path.clone(); + if path.len() > 1 { + path.pop(); + } + path.extend(relative_reference.path.iter().cloned()); + ( + scheme, + authority, + Self::normalize_path(&path), + query + ) } - path.extend(relative_reference.path.iter().cloned()); - ( - scheme, - authority, - Self::normalize_path(&path), - query - ) } - } - } + }, + |authority| ( + self.scheme.clone(), + Some(authority.clone()), + Self::normalize_path(&relative_reference.path), + relative_reference.query.clone() + ) + ) }; Self{ scheme, @@ -697,11 +691,7 @@ impl Uri { /// or there is an Authority in the URI but it has no userinfo in it. #[must_use = "security breach... security breach... userinfo not used"] pub fn userinfo(&self) -> Option<&[u8]> { - if let Some(authority) = &self.authority { - authority.userinfo() - } else { - None - } + self.authority.as_ref().and_then(Authority::userinfo) } /// Convert the fragment (if any) into a string. -- cgit v1.2.3