Skip to content

Commit

Permalink
refactor(clippy): reduce clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
linyihai committed Dec 3, 2024
1 parent 12717d1 commit 9dc5525
Show file tree
Hide file tree
Showing 24 changed files with 113 additions and 124 deletions.
4 changes: 2 additions & 2 deletions benches/end_to_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn http1_parallel_x10_req_10kb_100_chunks(b: &mut test::Bencher) {
#[bench]
#[ignore]
fn http1_parallel_x10_res_1mb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 1024 * 1];
let body = &[b'x'; 1024 * 1024];
opts().parallel(10).response_body(body).bench(b)
}

Expand Down Expand Up @@ -177,7 +177,7 @@ fn http2_parallel_x10_req_10kb_100_chunks_max_window(b: &mut test::Bencher) {

#[bench]
fn http2_parallel_x10_res_1mb(b: &mut test::Bencher) {
let body = &[b'x'; 1024 * 1024 * 1];
let body = &[b'x'; 1024 * 1024];
opts()
.http2()
.parallel(10)
Expand Down
2 changes: 1 addition & 1 deletion benches/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,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
2 changes: 1 addition & 1 deletion benches/support/tokiort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Timer for TokioTimer {

fn reset(&self, sleep: &mut Pin<Box<dyn Sleep>>, new_deadline: Instant) {
if let Some(sleep) = sleep.as_mut().downcast_mut_pin::<TokioSleep>() {
sleep.reset(new_deadline.into())
sleep.reset(new_deadline)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async fn fetch_url(url: hyper::Uri) -> Result<()> {
while let Some(next) = res.frame().await {
let frame = next?;
if let Some(chunk) = frame.data_ref() {
io::stdout().write_all(&chunk).await?;
io::stdout().write_all(chunk).await?;
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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 out_addr_clone = out_addr;

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

Expand Down
2 changes: 1 addition & 1 deletion examples/http_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async fn proxy(
}

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
4 changes: 2 additions & 2 deletions examples/single_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ async fn http1_client(url: hyper::Uri) -> Result<(), Box<dyn std::error::Error>>
while let Some(next) = res.frame().await {
let frame = next?;
if let Some(chunk) = frame.data_ref() {
stdout.write_all(&chunk).await.unwrap();
stdout.write_all(chunk).await.unwrap();
}
}
stdout.write_all(b"\n-----------------\n").await.unwrap();
Expand Down Expand Up @@ -308,7 +308,7 @@ async fn http2_client(url: hyper::Uri) -> Result<(), Box<dyn std::error::Error>>
while let Some(next) = res.frame().await {
let frame = next?;
if let Some(chunk) = frame.data_ref() {
stdout.write_all(&chunk).await.unwrap();
stdout.write_all(chunk).await.unwrap();
}
}
stdout.write_all(b"\n-----------------\n").await.unwrap();
Expand Down
3 changes: 1 addition & 2 deletions examples/upgrades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,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 @@ -187,7 +186,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 @@ -117,7 +117,7 @@ async fn main() -> Result<()> {
let io = TokioIo::new(stream);

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(io, service).await {
println!("Failed to serve connection: {:?}", err);
Expand Down
6 changes: 6 additions & 0 deletions src/client/conn/http1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ where

// ===== impl Builder

impl Default for Builder {
fn default() -> Builder {
Builder::new()
}
}

impl Builder {
/// Creates a new connection builder.
#[inline]
Expand Down
10 changes: 5 additions & 5 deletions src/ext/h1_reason_phrase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,26 +174,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 +206,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
8 changes: 4 additions & 4 deletions src/proto/h1/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ mod tests {
use std::pin::Pin;
use std::time::Duration;

impl<'a> MemRead for &'a [u8] {
impl MemRead for &[u8] {
fn read_mem(&mut self, _: &mut Context<'_>, len: usize) -> Poll<io::Result<Bytes>> {
let n = std::cmp::min(len, self.len());
if n > 0 {
Expand All @@ -707,12 +707,12 @@ mod tests {
}
}

impl<'a> MemRead for &'a mut (dyn Read + Unpin) {
impl MemRead for &mut (dyn Read + Unpin) {
fn read_mem(&mut self, cx: &mut Context<'_>, len: usize) -> Poll<io::Result<Bytes>> {
let mut v = vec![0; len];
let mut buf = ReadBuf::new(&mut v);
ready!(Pin::new(self).poll_read(cx, buf.unfilled())?);
Poll::Ready(Ok(Bytes::copy_from_slice(&buf.filled())))
Poll::Ready(Ok(Bytes::copy_from_slice(buf.filled())))
}
}

Expand Down Expand Up @@ -761,7 +761,7 @@ mod tests {
})
.await;
let desc = format!("read_size failed for {:?}", s);
state = result.expect(desc.as_str());
state = result.expect(&desc);
if state == ChunkedState::Body || state == ChunkedState::EndCr {
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/proto/h1/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ impl<'a, T> OptGuard<'a, T> {
}
}

impl<'a, T> Drop for OptGuard<'a, T> {
impl<T> Drop for OptGuard<'_, T> {
fn drop(&mut self) {
if self.1 {
self.0.set(None);
Expand Down
68 changes: 28 additions & 40 deletions src/proto/h1/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,19 +535,16 @@ mod tests {
let trailers = vec![HeaderValue::from_static("chunky-trailer")];
let encoder = encoder.into_chunked_with_trailing_fields(trailers);

let headers = HeaderMap::from_iter(
vec![
(
HeaderName::from_static("chunky-trailer"),
HeaderValue::from_static("header data"),
),
(
HeaderName::from_static("should-not-be-included"),
HeaderValue::from_static("oops"),
),
]
.into_iter(),
);
let headers = HeaderMap::from_iter(vec![
(
HeaderName::from_static("chunky-trailer"),
HeaderValue::from_static("header data"),
),
(
HeaderName::from_static("should-not-be-included"),
HeaderValue::from_static("oops"),
),
]);

let buf1 = encoder.encode_trailers::<&[u8]>(headers, false).unwrap();

Expand All @@ -565,19 +562,16 @@ mod tests {
];
let encoder = encoder.into_chunked_with_trailing_fields(trailers);

let headers = HeaderMap::from_iter(
vec![
(
HeaderName::from_static("chunky-trailer"),
HeaderValue::from_static("header data"),
),
(
HeaderName::from_static("chunky-trailer-2"),
HeaderValue::from_static("more header data"),
),
]
.into_iter(),
);
let headers = HeaderMap::from_iter(vec![
(
HeaderName::from_static("chunky-trailer"),
HeaderValue::from_static("header data"),
),
(
HeaderName::from_static("chunky-trailer-2"),
HeaderValue::from_static("more header data"),
),
]);

let buf1 = encoder.encode_trailers::<&[u8]>(headers, false).unwrap();

Expand All @@ -593,13 +587,10 @@ mod tests {
fn chunked_with_no_trailer_header() {
let encoder = Encoder::chunked();

let headers = HeaderMap::from_iter(
vec![(
HeaderName::from_static("chunky-trailer"),
HeaderValue::from_static("header data"),
)]
.into_iter(),
);
let headers = HeaderMap::from_iter(vec![(
HeaderName::from_static("chunky-trailer"),
HeaderValue::from_static("header data"),
)]);

assert!(encoder
.encode_trailers::<&[u8]>(headers.clone(), false)
Expand Down Expand Up @@ -656,13 +647,10 @@ mod tests {
let trailers = vec![HeaderValue::from_static("chunky-trailer")];
let encoder = encoder.into_chunked_with_trailing_fields(trailers);

let headers = HeaderMap::from_iter(
vec![(
HeaderName::from_static("chunky-trailer"),
HeaderValue::from_static("header data"),
)]
.into_iter(),
);
let headers = HeaderMap::from_iter(vec![(
HeaderName::from_static("chunky-trailer"),
HeaderValue::from_static("header data"),
)]);
let buf1 = encoder.encode_trailers::<&[u8]>(headers, true).unwrap();

let mut dst = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion src/proto/h1/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ where
}

#[cfg(test)]
fn flush<'a>(&'a mut self) -> impl std::future::Future<Output = io::Result<()>> + 'a {
fn flush(&mut self) -> impl std::future::Future<Output = io::Result<()>> + '_ {
futures_util::future::poll_fn(move |cx| self.poll_flush(cx))
}
}
Expand Down
18 changes: 7 additions & 11 deletions src/proto/h1/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ where
return Ok(None);
}

let _entered = trace_span!("parse_headers");
trace_span!("parse_headers");

if let Some(prev_len) = prev_len {
if !is_complete_fast(bytes, prev_len) {
Expand All @@ -100,10 +100,8 @@ fn is_complete_fast(bytes: &[u8], prev_len: usize) -> bool {
if bytes[i + 1..].chunks(3).next() == Some(&b"\n\r\n"[..]) {
return true;
}
} else if b == b'\n' {
if bytes.get(i + 1) == Some(&b'\n') {
return true;
}
} else if b == b'\n' && bytes.get(i + 1) == Some(&b'\n') {
return true;
}
}

Expand All @@ -117,7 +115,7 @@ pub(super) fn encode_headers<T>(
where
T: Http1Transaction,
{
let _entered = trace_span!("encode_headers");
trace_span!("encode_headers");
T::encode(enc, dst)
}

Expand Down Expand Up @@ -1622,7 +1620,7 @@ fn write_headers_original_case(
struct FastWrite<'a>(&'a mut Vec<u8>);

#[cfg(feature = "client")]
impl<'a> fmt::Write for FastWrite<'a> {
impl fmt::Write for FastWrite<'_> {
#[inline]
fn write_str(&mut self, s: &str) -> fmt::Result {
extend(self.0, s.as_bytes());
Expand Down Expand Up @@ -1721,7 +1719,7 @@ mod tests {
Server::parse(&mut raw, ctx).unwrap_err();
}

const H09_RESPONSE: &'static str = "Baguettes are super delicious, don't you agree?";
const H09_RESPONSE: &str = "Baguettes are super delicious, don't you agree?";

#[test]
fn test_parse_response_h09_allowed() {
Expand Down Expand Up @@ -1766,7 +1764,7 @@ mod tests {
assert_eq!(raw, H09_RESPONSE);
}

const RESPONSE_WITH_WHITESPACE_BETWEEN_HEADER_NAME_AND_COLON: &'static str =
const RESPONSE_WITH_WHITESPACE_BETWEEN_HEADER_NAME_AND_COLON: &str =
"HTTP/1.1 200 OK\r\nAccess-Control-Allow-Credentials : true\r\n\r\n";

#[test]
Expand Down Expand Up @@ -1842,14 +1840,12 @@ mod tests {
assert_eq!(
orig_headers
.get_all_internal(&HeaderName::from_static("host"))
.into_iter()
.collect::<Vec<_>>(),
vec![&Bytes::from("Host")]
);
assert_eq!(
orig_headers
.get_all_internal(&HeaderName::from_static("x-bread"))
.into_iter()
.collect::<Vec<_>>(),
vec![&Bytes::from("X-BREAD")]
);
Expand Down
Loading

0 comments on commit 9dc5525

Please sign in to comment.