Skip to content

Commit

Permalink
Apply clippy fixes, except in #[test] code
Browse files Browse the repository at this point in the history
  • Loading branch information
bossmc committed Oct 24, 2022
1 parent 91e83b7 commit ec157cc
Show file tree
Hide file tree
Showing 31 changed files with 101 additions and 95 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ jobs:
command: fmt
args: --all -- --check

- name: cargo clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: --features full --lib --examples --benches

test:
name: Test ${{ matrix.rust }} on ${{ matrix.os }}
needs: [style]
Expand Down
1 change: 1 addition & 0 deletions benches/body.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "nightly")]
#![feature(test)]
#![deny(warnings)]

Expand Down
3 changes: 1 addition & 2 deletions benches/connect.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#![cfg(feature = "nightly")]
#![feature(test)]
#![deny(warnings)]

extern crate test;

// TODO: Reimplement http_connector bench using hyper::client::conn
// (instead of removed HttpConnector).

Expand Down
3 changes: 1 addition & 2 deletions benches/end_to_end.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#![cfg(feature = "nightly")]
#![feature(test)]
#![deny(warnings)]

extern crate test;

// TODO: Reimplement Opts::bench using hyper::server::conn and hyper::client::conn
// (instead of Server and HttpClient).

Expand Down
1 change: 1 addition & 0 deletions benches/pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "nightly")]
#![feature(test)]
#![deny(warnings)]

Expand Down
5 changes: 3 additions & 2 deletions benches/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "nightly")]
#![feature(test)]
#![deny(warnings)]

Expand Down Expand Up @@ -148,7 +149,7 @@ fn raw_tcp_throughput_small_payload(b: &mut test::Bencher) {

let mut buf = [0u8; 8192];
while rx.try_recv().is_err() {
sock.read(&mut buf).unwrap();
let _ = sock.read(&mut buf).unwrap();
sock.write_all(
b"\
HTTP/1.1 200 OK\r\n\
Expand Down Expand Up @@ -195,7 +196,7 @@ fn raw_tcp_throughput_large_payload(b: &mut test::Bencher) {
let mut buf = [0u8; 8192];
while rx.try_recv().is_err() {
let r = sock.read(&mut buf).unwrap();
extern crate test;

if r == 0 {
break;
}
Expand Down
6 changes: 3 additions & 3 deletions benches/support/tokiort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ impl Timer for TokioTimer {
fn sleep(&self, duration: Duration) -> Box<dyn Sleep + Unpin> {
let s = tokio::time::sleep(duration);
let hs = TokioSleep { inner: Box::pin(s) };
return Box::new(hs);
Box::new(hs)
}

fn sleep_until(&self, deadline: Instant) -> Box<dyn Sleep + Unpin> {
return Box::new(TokioSleep {
Box::new(TokioSleep {
inner: Box::pin(tokio::time::sleep_until(deadline.into())),
});
})
}
}

Expand Down
4 changes: 1 addition & 3 deletions examples/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let in_addr: SocketAddr = ([127, 0, 0, 1], 3001).into();
let out_addr: SocketAddr = ([127, 0, 0, 1], 3000).into();

let out_addr_clone = out_addr.clone();

let listener = TcpListener::bind(in_addr).await?;

println!("Listening on http://{}", in_addr);
Expand All @@ -27,7 +25,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let service = service_fn(move |mut req| {
let uri_string = format!(
"http://{}{}",
out_addr_clone,
out_addr,
req.uri()
.path_and_query()
.map(|x| x.as_str())
Expand Down
2 changes: 1 addition & 1 deletion examples/http_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async fn proxy(req: Request<Recv>) -> Result<Response<BoxBody<Bytes, hyper::Erro
}

fn host_addr(uri: &http::Uri) -> Option<String> {
uri.authority().and_then(|auth| Some(auth.to_string()))
uri.authority().map(|auth| auth.to_string())
}

fn empty() -> BoxBody<Bytes, hyper::Error> {
Expand Down
3 changes: 1 addition & 2 deletions examples/upgrades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ async fn main() {
res = &mut conn => {
if let Err(err) = res {
println!("Error serving connection: {:?}", err);
return;
}
}
// Continue polling the connection after enabling graceful shutdown.
Expand All @@ -178,7 +177,7 @@ async fn main() {
});

// Client requests a HTTP connection upgrade.
let request = client_upgrade_request(addr.clone());
let request = client_upgrade_request(addr);
if let Err(e) = request.await {
eprintln!("client error: {}", e);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/web_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async fn main() -> Result<()> {
let (stream, _) = listener.accept().await?;

tokio::task::spawn(async move {
let service = service_fn(move |req| response_examples(req));
let service = service_fn(response_examples);

if let Err(err) = http1::Builder::new()
.serve_connection(stream, service)
Expand Down
7 changes: 3 additions & 4 deletions src/body/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,11 @@ impl Recv {
if !content_length.is_exact() && recv.is_end_stream() {
content_length = DecodedLength::ZERO;
}
let body = Recv::new(Kind::H2 {
Recv::new(Kind::H2 {
ping,
content_length,
recv,
});

body
})
}

#[cfg(feature = "ffi")]
Expand Down Expand Up @@ -351,6 +349,7 @@ impl Sender {

/// Aborts the body in an abnormal fashion.
#[allow(unused)]
#[allow(clippy::redundant_clone)]
pub(crate) fn abort(self) {
let _ = self
.data_tx
Expand Down
9 changes: 7 additions & 2 deletions src/client/conn/http1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct SendRequest<B> {
/// This allows taking apart a `Connection` at a later time, in order to
/// reclaim the IO object, and additional related pieces.
#[derive(Debug)]
#[non_exhaustive]
pub struct Parts<T> {
/// The original IO object used in the handshake.
pub io: T,
Expand All @@ -45,7 +46,6 @@ pub struct Parts<T> {
/// You will want to check for any existing bytes if you plan to continue
/// communicating on the IO object.
pub read_buf: Bytes,
_inner: (),
}


Expand Down Expand Up @@ -76,7 +76,6 @@ where
Parts {
io,
read_buf,
_inner: (),
}
}

Expand Down Expand Up @@ -546,3 +545,9 @@ impl Builder {
}
}
}

impl std::default::Default for Builder {
fn default() -> Self {
Self::new()
}
}
6 changes: 6 additions & 0 deletions src/client/conn/http2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,9 @@ impl Builder {
}
}
}

impl std::default::Default for Builder {
fn default() -> Self {
Self::new()
}
}
4 changes: 3 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub(super) enum Parse {
Header(Header),
TooLarge,
Status,
#[cfg(feature = "http1")]
#[cfg_attr(debug_assertions, allow(unused))]
Internal,
}
Expand Down Expand Up @@ -193,7 +194,7 @@ impl Error {
pub(crate) fn find_source<E: StdError + 'static>(&self) -> Option<&E> {
let mut cause = self.source();
while let Some(err) = cause {
if let Some(ref typed) = err.downcast_ref() {
if let Some(typed) = err.downcast_ref() {
return Some(typed);
}
cause = err.source();
Expand Down Expand Up @@ -351,6 +352,7 @@ impl Error {
}
Kind::Parse(Parse::TooLarge) => "message head is too large",
Kind::Parse(Parse::Status) => "invalid HTTP status-code parsed",
#[cfg(feature = "http1")]
Kind::Parse(Parse::Internal) => {
"internal error inside Hyper and/or its dependencies, please report"
}
Expand Down
2 changes: 1 addition & 1 deletion src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl HeaderCaseMap {
&'a self,
name: &HeaderName,
) -> impl Iterator<Item = impl AsRef<[u8]> + 'a> + 'a {
self.get_all_internal(name).into_iter()
self.get_all_internal(name)
}

/// Returns a view of all spellings associated with that header name,
Expand Down
23 changes: 11 additions & 12 deletions src/ext/h1_reason_phrase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ impl ReasonPhrase {

/// Converts a `Bytes` directly into a `ReasonPhrase` without validating.
///
/// # Safety
///
/// Use with care; invalid bytes in a reason phrase can cause serious security problems if
/// emitted in a response.
pub unsafe fn from_bytes_unchecked(reason: Bytes) -> Self {
Expand Down Expand Up @@ -107,9 +109,9 @@ impl TryFrom<Bytes> for ReasonPhrase {
}
}

impl Into<Bytes> for ReasonPhrase {
fn into(self) -> Bytes {
self.0
impl From<ReasonPhrase> for Bytes {
fn from(rp: ReasonPhrase) -> Self {
rp.0
}
}

Expand Down Expand Up @@ -144,12 +146,9 @@ const fn is_valid_byte(b: u8) -> bool {
}

// See https://httpwg.org/http-core/draft-ietf-httpbis-semantics-latest.html#fields.values
//
// The 0xFF comparison is technically redundant, but it matches the text of the spec more
// clearly and will be optimized away.
#[allow(unused_comparisons)]
const fn is_obs_text(b: u8) -> bool {
0x80 <= b && b <= 0xFF
0x80 <= b /* && b <= 0xFF */
}

// See https://httpwg.org/http-core/draft-ietf-httpbis-messaging-latest.html#rfc.section.4.p.7
Expand All @@ -174,26 +173,26 @@ mod tests {

#[test]
fn basic_valid() {
const PHRASE: &'static [u8] = b"OK";
const PHRASE: &[u8] = b"OK";
assert_eq!(ReasonPhrase::from_static(PHRASE).as_bytes(), PHRASE);
assert_eq!(ReasonPhrase::try_from(PHRASE).unwrap().as_bytes(), PHRASE);
}

#[test]
fn empty_valid() {
const PHRASE: &'static [u8] = b"";
const PHRASE: &[u8] = b"";
assert_eq!(ReasonPhrase::from_static(PHRASE).as_bytes(), PHRASE);
assert_eq!(ReasonPhrase::try_from(PHRASE).unwrap().as_bytes(), PHRASE);
}

#[test]
fn obs_text_valid() {
const PHRASE: &'static [u8] = b"hyp\xe9r";
const PHRASE: &[u8] = b"hyp\xe9r";
assert_eq!(ReasonPhrase::from_static(PHRASE).as_bytes(), PHRASE);
assert_eq!(ReasonPhrase::try_from(PHRASE).unwrap().as_bytes(), PHRASE);
}

const NEWLINE_PHRASE: &'static [u8] = b"hyp\ner";
const NEWLINE_PHRASE: &[u8] = b"hyp\ner";

#[test]
#[should_panic]
Expand All @@ -206,7 +205,7 @@ mod tests {
assert!(ReasonPhrase::try_from(NEWLINE_PHRASE).is_err());
}

const CR_PHRASE: &'static [u8] = b"hyp\rer";
const CR_PHRASE: &[u8] = b"hyp\rer";

#[test]
#[should_panic]
Expand Down
7 changes: 2 additions & 5 deletions src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub(super) fn content_length_parse_all_values(values: ValueIter<'_, HeaderValue>
}
}

return content_length
content_length
}

fn from_digits(bytes: &[u8]) -> Option<u64> {
Expand Down Expand Up @@ -93,10 +93,7 @@ fn from_digits(bytes: &[u8]) -> Option<u64> {

#[cfg(all(feature = "http2", feature = "client"))]
pub(super) fn method_has_defined_payload_semantics(method: &Method) -> bool {
match *method {
Method::GET | Method::HEAD | Method::DELETE | Method::CONNECT => false,
_ => true,
}
!matches!(*method, Method::GET | Method::HEAD | Method::DELETE | Method::CONNECT)
}

#[cfg(feature = "http2")]
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![allow(clippy::module_inception)]
#![cfg_attr(test, deny(rust_2018_idioms))]
#![cfg_attr(all(test, feature = "full"), deny(unreachable_pub))]
#![cfg_attr(all(test, feature = "full"), deny(warnings))]
Expand Down
12 changes: 3 additions & 9 deletions src/proto/h1/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,7 @@ where
}

pub(crate) fn can_read_body(&self) -> bool {
match self.state.reading {
Reading::Body(..) | Reading::Continue(..) => true,
_ => false,
}
matches!(self.state.reading, Reading::Body(..) | Reading::Continue(..))
}

fn should_error_on_eof(&self) -> bool {
Expand All @@ -185,6 +182,7 @@ where
read_buf.len() >= 24 && read_buf[..24] == *H2_PREFACE
}

#[allow(clippy::type_complexity)] // The return type is complex due to task-semantics
pub(super) fn poll_read_head(
&mut self,
cx: &mut task::Context<'_>,
Expand Down Expand Up @@ -943,11 +941,7 @@ impl State {
}

fn wants_keep_alive(&self) -> bool {
if let KA::Disabled = self.keep_alive.status() {
false
} else {
true
}
!matches!(self.keep_alive.status(), KA::Disabled)
}

fn try_keep_alive<T: Http1Transaction>(&mut self) {
Expand Down
5 changes: 2 additions & 3 deletions src/proto/h1/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ mod tests {
let mut v = vec![0; len];
let mut buf = ReadBuf::new(&mut v);
ready!(Pin::new(self).poll_read(cx, &mut buf)?);
Poll::Ready(Ok(Bytes::copy_from_slice(&buf.filled())))
Poll::Ready(Ok(Bytes::copy_from_slice(buf.filled())))
}
}

Expand Down Expand Up @@ -487,8 +487,7 @@ mod tests {
let result =
futures_util::future::poll_fn(|cx| state.step(cx, rdr, &mut size, &mut None))
.await;
let desc = format!("read_size failed for {:?}", s);
state = result.expect(desc.as_str());
state = result.unwrap_or_else(|_| panic!("read_size failed for {:?}", s));
if state == ChunkedState::Body || state == ChunkedState::EndCr {
break;
}
Expand Down
Loading

0 comments on commit ec157cc

Please sign in to comment.