From 1059eb349a560a4b9b83181acd9db19d1ef42073 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Mon, 12 Jun 2017 23:16:20 -0400 Subject: [PATCH] style(lib): address clippy code style warnings * Remove unnecessary return statements. * Combine identical `match` arms. * Collapse nested conditional. * Use `assert_ne` where applicable. * Lifetime elision. --- src/client/connect.rs | 1 - src/client/mod.rs | 2 +- src/error.rs | 8 ++--- src/header/common/cookie.rs | 6 ++-- src/header/common/link.rs | 30 +++++++------------ .../common/strict_transport_security.rs | 4 +-- src/header/internals/cell.rs | 2 +- src/header/mod.rs | 6 ++-- src/header/raw.rs | 2 +- src/http/conn.rs | 12 ++++---- src/http/h1/decode.rs | 6 ++-- src/server/mod.rs | 2 +- src/status.rs | 4 +-- src/uri.rs | 2 +- 14 files changed, 38 insertions(+), 49 deletions(-) diff --git a/src/client/connect.rs b/src/client/connect.rs index 745a281d..716e2f94 100644 --- a/src/client/connect.rs +++ b/src/client/connect.rs @@ -103,7 +103,6 @@ impl Service for HttpConnector { let port = match uri.port() { Some(port) => port, None => match uri.scheme() { - Some("http") => 80, Some("https") => 443, _ => 80, }, diff --git a/src/client/mod.rs b/src/client/mod.rs index cad107ed..ee25249b 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -76,7 +76,7 @@ impl Client { impl Client { /// Return a reference to a handle to the event loop this Client is associated with. #[inline] - pub fn handle<'a>(&'a self) -> &'a Handle { + pub fn handle(&self) -> &Handle { &self.handle } diff --git a/src/error.rs b/src/error.rs index ecff6015..32f6291f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -128,11 +128,11 @@ impl From for Error { impl From for Error { fn from(err: httparse::Error) -> Error { match err { - httparse::Error::HeaderName => Header, - httparse::Error::HeaderValue => Header, - httparse::Error::NewLine => Header, - httparse::Error::Status => Status, + httparse::Error::HeaderName | + httparse::Error::HeaderValue | + httparse::Error::NewLine | httparse::Error::Token => Header, + httparse::Error::Status => Status, httparse::Error::TooManyHeaders => TooLarge, httparse::Error::Version => Version, } diff --git a/src/header/common/cookie.rs b/src/header/common/cookie.rs index 9e86bd28..dbe2d8e2 100644 --- a/src/header/common/cookie.rs +++ b/src/header/common/cookie.rs @@ -222,16 +222,16 @@ mod tests { // left has more params cookie.append("foo", "bar"); - assert!(cookie != cookie2); + assert_ne!(cookie, cookie2); // same len, different params cookie2.append("bar", "foo"); - assert!(cookie != cookie2); + assert_ne!(cookie, cookie2); // right has more params, and matching KV cookie2.append("foo", "bar"); - assert!(cookie != cookie2); + assert_ne!(cookie, cookie2); // same params, different order cookie.append("bar", "foo"); diff --git a/src/header/common/link.rs b/src/header/common/link.rs index e374cf7a..04783ba4 100644 --- a/src/header/common/link.rs +++ b/src/header/common/link.rs @@ -505,14 +505,13 @@ impl FromStr for Link { // https://tools.ietf.org/html/rfc5988#section-5.3 if link_header.rel.is_none() { link_header.rel = match link_param_split.next() { - None => return Err(::Error::Header), - Some("") => return Err(::Error::Header), + None | Some("") => return Err(::Error::Header), Some(s) => { s.trim_matches(|c: char| c == '"' || c.is_whitespace()) .split(' ') .map(|t| t.trim().parse()) .collect::, _>>() - .or_else(|_| return Err(::Error::Header)) + .or_else(|_| Err(::Error::Header)) .ok() }, }; @@ -521,8 +520,7 @@ impl FromStr for Link { // Parse the `Context IRI`. // https://tools.ietf.org/html/rfc5988#section-5.2 link_header.anchor = match link_param_split.next() { - None => return Err(::Error::Header), - Some("") => return Err(::Error::Header), + None | Some("") => return Err(::Error::Header), Some(s) => match verify_and_trim(s.trim(), (b'"', b'"')) { Err(_) => return Err(::Error::Header), Ok(a) => Some(String::from(a)), @@ -533,14 +531,13 @@ impl FromStr for Link { // https://tools.ietf.org/html/rfc5988#section-5.3 if link_header.rev.is_none() { link_header.rev = match link_param_split.next() { - None => return Err(::Error::Header), - Some("") => return Err(::Error::Header), + None | Some("") => return Err(::Error::Header), Some(s) => { s.trim_matches(|c: char| c == '"' || c.is_whitespace()) .split(' ') .map(|t| t.trim().parse()) .collect::, _>>() - .or_else(|_| return Err(::Error::Header)) + .or_else(|_| Err(::Error::Header)) .ok() }, } @@ -552,8 +549,7 @@ impl FromStr for Link { v.push( match link_param_split.next() { - None => return Err(::Error::Header), - Some("") => return Err(::Error::Header), + None | Some("") => return Err(::Error::Header), Some(s) => match s.trim().parse() { Err(_) => return Err(::Error::Header), Ok(t) => t, @@ -567,14 +563,13 @@ impl FromStr for Link { // https://tools.ietf.org/html/rfc5988#section-5.4 if link_header.media_desc.is_none() { link_header.media_desc = match link_param_split.next() { - None => return Err(::Error::Header), - Some("") => return Err(::Error::Header), + None | Some("") => return Err(::Error::Header), Some(s) => { s.trim_matches(|c: char| c == '"' || c.is_whitespace()) .split(',') .map(|t| t.trim().parse()) .collect::, _>>() - .or_else(|_| return Err(::Error::Header)) + .or_else(|_| Err(::Error::Header)) .ok() }, }; @@ -584,8 +579,7 @@ impl FromStr for Link { // https://tools.ietf.org/html/rfc5988#section-5.4 if link_header.title.is_none() { link_header.title = match link_param_split.next() { - None => return Err(::Error::Header), - Some("") => return Err(::Error::Header), + None | Some("") => return Err(::Error::Header), Some(s) => match verify_and_trim(s.trim(), (b'"', b'"')) { Err(_) => return Err(::Error::Header), Ok(t) => Some(String::from(t)), @@ -600,8 +594,7 @@ impl FromStr for Link { // https://tools.ietf.org/html/rfc5987#section-3.2.1 if link_header.title_star.is_none() { link_header.title_star = match link_param_split.next() { - None => return Err(::Error::Header), - Some("") => return Err(::Error::Header), + None | Some("") => return Err(::Error::Header), Some(s) => Some(String::from(s.trim())), }; } @@ -610,8 +603,7 @@ impl FromStr for Link { // https://tools.ietf.org/html/rfc5988#section-5.4 if link_header.media_type.is_none() { link_header.media_type = match link_param_split.next() { - None => return Err(::Error::Header), - Some("") => return Err(::Error::Header), + None | Some("") => return Err(::Error::Header), Some(s) => match verify_and_trim(s.trim(), (b'"', b'"')) { Err(_) => return Err(::Error::Header), Ok(t) => match t.parse() { diff --git a/src/header/common/strict_transport_security.rs b/src/header/common/strict_transport_security.rs index a4c06d98..6e018555 100644 --- a/src/header/common/strict_transport_security.rs +++ b/src/header/common/strict_transport_security.rs @@ -104,8 +104,8 @@ impl FromStr for StrictTransportSecurity { .fold(Ok((None, None)), |res, dir| match (res, dir) { (Ok((None, sub)), Ok(Directive::MaxAge(age))) => Ok((Some(age), sub)), (Ok((age, None)), Ok(Directive::IncludeSubdomains)) => Ok((age, Some(()))), - (Ok((Some(_), _)), Ok(Directive::MaxAge(_))) => Err(::Error::Header), - (Ok((_, Some(_))), Ok(Directive::IncludeSubdomains)) => Err(::Error::Header), + (Ok((Some(_), _)), Ok(Directive::MaxAge(_))) | + (Ok((_, Some(_))), Ok(Directive::IncludeSubdomains)) | (_, Err(_)) => Err(::Error::Header), (res, _) => res }) diff --git a/src/header/internals/cell.rs b/src/header/internals/cell.rs index 2e87b0bd..59597d49 100644 --- a/src/header/internals/cell.rs +++ b/src/header/internals/cell.rs @@ -109,7 +109,7 @@ impl PtrMapCell { let one = mem::replace(map, PtrMap::Empty); match one { PtrMap::One(id, one) => { - debug_assert!(id != key); + debug_assert_ne!(id, key); let mut hm = HashMap::with_capacity(2); hm.insert(id, one); hm.insert(key, val); diff --git a/src/header/mod.rs b/src/header/mod.rs index d662ad82..9bc3fea3 100644 --- a/src/header/mod.rs +++ b/src/header/mod.rs @@ -918,7 +918,7 @@ mod tests { headers1.set(ContentLength(11)); headers2.set(Host::new("foo.bar", None)); - assert!(headers1 != headers2); + assert_ne!(headers1, headers2); headers1 = Headers::new(); headers2 = Headers::new(); @@ -928,7 +928,7 @@ mod tests { assert_eq!(headers1, headers2); headers1.set(ContentLength(10)); - assert!(headers1 != headers2); + assert_ne!(headers1, headers2); headers1 = Headers::new(); headers2 = Headers::new(); @@ -936,7 +936,7 @@ mod tests { headers1.set(Host::new("foo.bar", None)); headers1.set(ContentLength(11)); headers2.set(ContentLength(11)); - assert!(headers1 != headers2); + assert_ne!(headers1, headers2); } #[cfg(feature = "nightly")] diff --git a/src/header/raw.rs b/src/header/raw.rs index 9401dd2b..f0f72f57 100644 --- a/src/header/raw.rs +++ b/src/header/raw.rs @@ -231,7 +231,7 @@ impl ::std::ops::Index for Raw { macro_rules! literals { ($($len:expr => $($value:expr),+;)+) => ( - fn maybe_literal<'a>(s: Cow<'a, [u8]>) -> Bytes { + fn maybe_literal(s: Cow<[u8]>) -> Bytes { match s.len() { $($len => { $( diff --git a/src/http/conn.rs b/src/http/conn.rs index bc9585b8..fc817f72 100644 --- a/src/http/conn.rs +++ b/src/http/conn.rs @@ -119,12 +119,12 @@ where I: AsyncRead + AsyncWrite, (true, Reading::Body(decoder)) }; self.state.reading = reading; - return Ok(Async::Ready(Some(Frame::Message { message: head, body: body }))); + Ok(Async::Ready(Some(Frame::Message { message: head, body: body }))) }, _ => { error!("unimplemented HTTP Version = {:?}", version); self.state.close_read(); - return Ok(Async::Ready(Some(Frame::Error { error: ::Error::Version }))); + Ok(Async::Ready(Some(Frame::Error { error: ::Error::Version }))) } } } @@ -139,12 +139,10 @@ where I: AsyncRead + AsyncWrite, let slice = try_nb!(decoder.decode(&mut self.io)); if !slice.is_empty() { return Ok(Async::Ready(Some(http::Chunk::from(slice)))); + } else if decoder.is_eof() { + (Reading::KeepAlive, Ok(Async::Ready(None))) } else { - if decoder.is_eof() { - (Reading::KeepAlive, Ok(Async::Ready(None))) - } else { - (Reading::Closed, Ok(Async::Ready(None))) - } + (Reading::Closed, Ok(Async::Ready(None))) } }, diff --git a/src/http/h1/decode.rs b/src/http/h1/decode.rs index 293b2102..ad284358 100644 --- a/src/http/h1/decode.rs +++ b/src/http/h1/decode.rs @@ -198,7 +198,7 @@ impl ChunkedState { // LWS can follow the chunk size, but no more digits can come b'\t' | b' ' => Ok(ChunkedState::SizeLws), b';' => Ok(ChunkedState::Extension), - b'\r' => return Ok(ChunkedState::SizeLf), + b'\r' => Ok(ChunkedState::SizeLf), _ => { Err(io::Error::new(io::ErrorKind::InvalidInput, "Invalid chunk size linear white space")) @@ -208,8 +208,8 @@ impl ChunkedState { fn read_extension(rdr: &mut R) -> io::Result { trace!("read_extension"); match byte!(rdr) { - b'\r' => return Ok(ChunkedState::SizeLf), - _ => return Ok(ChunkedState::Extension), // no supported extensions + b'\r' => Ok(ChunkedState::SizeLf), + _ => Ok(ChunkedState::Extension), // no supported extensions } } fn read_size_lf(rdr: &mut R, size: &mut u64) -> io::Result { diff --git a/src/server/mod.rs b/src/server/mod.rs index f147761c..2d213669 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -426,7 +426,7 @@ impl Server let wait = WaitUntilZero { info: info.clone() }; match core.run(wait.select(timeout)) { Ok(_) => Ok(()), - Err((e, _)) => return Err(e.into()) + Err((e, _)) => Err(e.into()) } } } diff --git a/src/status.rs b/src/status.rs index fbd6b12b..e12e7216 100644 --- a/src/status.rs +++ b/src/status.rs @@ -648,11 +648,11 @@ impl StatusClass { pub fn default_code(&self) -> StatusCode { match *self { StatusClass::Informational => StatusCode::Continue, - StatusClass::Success => StatusCode::Ok, + StatusClass::Success | + StatusClass::NoClass => StatusCode::Ok, StatusClass::Redirection => StatusCode::MultipleChoices, StatusClass::ClientError => StatusCode::BadRequest, StatusClass::ServerError => StatusCode::InternalServerError, - StatusClass::NoClass => StatusCode::Ok, } } } diff --git a/src/uri.rs b/src/uri.rs index 7e13f587..89b3811c 100644 --- a/src/uri.rs +++ b/src/uri.rs @@ -80,7 +80,7 @@ impl Uri { }) } else if (s.contains("/") || s.contains("?")) && !s.contains("://") { // last possibility is authority-form, above are illegal characters - return Err(UriError(ErrorKind::Malformed)) + Err(UriError(ErrorKind::Malformed)) } else { // authority-form let len = s.len();