Skip to content

Commit 15ae029

Browse files
committed
Fix more warnings for different feature combinations
This patch helps to clean up warnings by: * adding some more `#[cfg(feature = "...")]` annotations. * Moving `#[test]` and `#[tokio::test]` to be first in order to work better with rust-analyzer's proc macro server. As best as I can tell I didn't accidentally remove APIs, but I wouldn't mind if someone reviewed with that in mind.
1 parent 4595a08 commit 15ae029

6 files changed

Lines changed: 57 additions & 28 deletions

File tree

src/client/legacy/client.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,8 @@ impl Builder {
15121512
{
15131513
#[cfg(feature = "http2")]
15141514
self.h2_builder.timer(timer);
1515+
#[cfg(not(feature = "http2"))]
1516+
let _ = timer;
15151517
self
15161518
}
15171519

@@ -1568,7 +1570,7 @@ impl Builder {
15681570
}
15691571

15701572
/// Build a client with this configuration and the default `HttpConnector`.
1571-
#[cfg(feature = "tokio")]
1573+
#[cfg(all(feature = "tokio"))]
15721574
pub fn build_http<B>(&self) -> Client<HttpConnector, B>
15731575
where
15741576
B: Body + Send,

src/client/legacy/connect/http.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,9 +1074,9 @@ mod tests {
10741074
}
10751075

10761076
// NOTE: pnet crate that we use in this test doesn't compile on Windows
1077+
#[tokio::test]
10771078
#[cfg(any(target_os = "linux", target_os = "macos"))]
10781079
#[cfg_attr(miri, ignore)]
1079-
#[tokio::test]
10801080
async fn local_address() {
10811081
use std::net::{IpAddr, TcpListener};
10821082

@@ -1112,8 +1112,8 @@ mod tests {
11121112
}
11131113

11141114
// NOTE: pnet crate that we use in this test doesn't compile on Windows
1115-
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
11161115
#[tokio::test]
1116+
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
11171117
#[ignore = "setting `SO_BINDTODEVICE` requires the `CAP_NET_RAW` capability (works when running as root)"]
11181118
async fn interface() {
11191119
use socket2::{Domain, Protocol, Socket, Type};

src/common/rewind.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(any(feature = "http1", feature = "http2"))]
2+
13
use std::{cmp, io};
24

35
use bytes::{Buf, Bytes};

src/server/conn/auto/mod.rs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,16 @@ impl<E> Builder<E> {
8080
/// # Example
8181
///
8282
/// ```
83+
/// # #[cfg(all(feature = "tokio", feature = "http2"))]
84+
/// # fn run() {
8385
/// use hyper_util::{
8486
/// rt::TokioExecutor,
8587
/// server::conn::auto,
8688
/// };
8789
///
8890
/// auto::Builder::new(TokioExecutor::new());
91+
/// # }
92+
/// # fn main() {}
8993
/// ```
9094
pub fn new(executor: E) -> Self {
9195
Self {
@@ -179,7 +183,7 @@ impl<E> Builder<E> {
179183
/// auto::Builder::new(TokioExecutor::new())
180184
/// .title_case_headers(true);
181185
/// ```
182-
#[cfg(feature = "http1")]
186+
#[cfg(all(feature = "tokio", feature = "http1"))]
183187
pub fn title_case_headers(mut self, enabled: bool) -> Self {
184188
self.http1.title_case_headers(enabled);
185189
self
@@ -203,7 +207,7 @@ impl<E> Builder<E> {
203207
/// auto::Builder::new(TokioExecutor::new())
204208
/// .preserve_header_case(true);
205209
/// ```
206-
#[cfg(feature = "http1")]
210+
#[cfg(all(feature = "tokio", feature = "http1"))]
207211
pub fn preserve_header_case(mut self, enabled: bool) -> Self {
208212
self.http1.preserve_header_case(enabled);
209213
self
@@ -1124,17 +1128,24 @@ impl<E> Http2Builder<'_, E> {
11241128
}
11251129
}
11261130

1127-
#[cfg(test)]
1131+
#[cfg(all(test, feature = "tokio"))]
11281132
mod tests {
1129-
use crate::{
1130-
rt::{TokioExecutor, TokioIo},
1131-
server::conn::auto,
1132-
};
1133+
use crate::rt::{TokioExecutor, TokioIo};
1134+
use crate::server::conn::auto;
11331135
use http::{Request, Response};
1136+
#[cfg(all(feature = "http1", feature = "http2", not(miri)))]
11341137
use http_body::Body;
1135-
use http_body_util::{BodyExt, Empty, Full};
1136-
use hyper::{body, body::Bytes, client, service::service_fn};
1137-
use std::{convert::Infallible, error::Error as StdError, net::SocketAddr, time::Duration};
1138+
use http_body_util::Full;
1139+
#[cfg(all(feature = "http1", feature = "http2", not(miri)))]
1140+
use http_body_util::{BodyExt, Empty};
1141+
#[cfg(all(feature = "http1", feature = "http2", not(miri)))]
1142+
use hyper::client;
1143+
use hyper::service::service_fn;
1144+
use hyper::{body, body::Bytes};
1145+
use std::convert::Infallible;
1146+
#[cfg(all(feature = "http1", feature = "http2", not(miri)))]
1147+
use std::error::Error as StdError;
1148+
use std::{net::SocketAddr, time::Duration};
11381149
use tokio::{
11391150
net::{TcpListener, TcpStream},
11401151
pin,
@@ -1143,6 +1154,7 @@ mod tests {
11431154
const BODY: &[u8] = b"Hello, world!";
11441155

11451156
#[test]
1157+
#[cfg(all(feature = "http1", feature = "http2"))]
11461158
fn configuration() {
11471159
// One liner.
11481160
auto::Builder::new(TokioExecutor::new())
@@ -1184,8 +1196,8 @@ mod tests {
11841196
.http1_only();
11851197
}
11861198

1187-
#[cfg(not(miri))]
11881199
#[tokio::test]
1200+
#[cfg(all(feature = "http1", feature = "http2", not(miri)))]
11891201
async fn http1() {
11901202
let addr = start_server(false, false).await;
11911203
let mut sender = connect_h1(addr).await;
@@ -1200,8 +1212,8 @@ mod tests {
12001212
assert_eq!(body, BODY);
12011213
}
12021214

1203-
#[cfg(not(miri))]
12041215
#[tokio::test]
1216+
#[cfg(all(feature = "http1", feature = "http2", not(miri)))]
12051217
async fn http2() {
12061218
let addr = start_server(false, false).await;
12071219
let mut sender = connect_h2(addr).await;
@@ -1216,8 +1228,8 @@ mod tests {
12161228
assert_eq!(body, BODY);
12171229
}
12181230

1219-
#[cfg(not(miri))]
12201231
#[tokio::test]
1232+
#[cfg(all(feature = "http1", feature = "http2", not(miri)))]
12211233
async fn http2_only() {
12221234
let addr = start_server(false, true).await;
12231235
let mut sender = connect_h2(addr).await;
@@ -1232,8 +1244,8 @@ mod tests {
12321244
assert_eq!(body, BODY);
12331245
}
12341246

1235-
#[cfg(not(miri))]
12361247
#[tokio::test]
1248+
#[cfg(all(feature = "http1", feature = "http2", not(miri)))]
12371249
async fn http2_only_fail_if_client_is_http1() {
12381250
let addr = start_server(false, true).await;
12391251
let mut sender = connect_h1(addr).await;
@@ -1244,8 +1256,8 @@ mod tests {
12441256
.expect_err("should fail");
12451257
}
12461258

1247-
#[cfg(not(miri))]
12481259
#[tokio::test]
1260+
#[cfg(all(feature = "http1", feature = "http2", not(miri)))]
12491261
async fn http1_only() {
12501262
let addr = start_server(true, false).await;
12511263
let mut sender = connect_h1(addr).await;
@@ -1260,8 +1272,8 @@ mod tests {
12601272
assert_eq!(body, BODY);
12611273
}
12621274

1263-
#[cfg(not(miri))]
12641275
#[tokio::test]
1276+
#[cfg(all(feature = "http1", feature = "http2", not(miri)))]
12651277
async fn http1_only_fail_if_client_is_http2() {
12661278
let addr = start_server(true, false).await;
12671279
let mut sender = connect_h2(addr).await;
@@ -1306,6 +1318,7 @@ mod tests {
13061318
assert_eq!(connection_error.kind(), std::io::ErrorKind::Interrupted);
13071319
}
13081320

1321+
#[cfg(all(feature = "http1", feature = "http2", not(miri)))]
13091322
async fn connect_h1<B>(addr: SocketAddr) -> client::conn::http1::SendRequest<B>
13101323
where
13111324
B: Body + Send + 'static,
@@ -1320,6 +1333,7 @@ mod tests {
13201333
sender
13211334
}
13221335

1336+
#[cfg(all(feature = "http1", feature = "http2", not(miri)))]
13231337
async fn connect_h2<B>(addr: SocketAddr) -> client::conn::http2::SendRequest<B>
13241338
where
13251339
B: Body + Unpin + Send + 'static,
@@ -1337,6 +1351,7 @@ mod tests {
13371351
sender
13381352
}
13391353

1354+
#[cfg(all(feature = "http1", feature = "http2", not(miri)))]
13401355
async fn start_server(h1_only: bool, h2_only: bool) -> SocketAddr {
13411356
let addr: SocketAddr = ([127, 0, 0, 1], 0).into();
13421357
let listener = TcpListener::bind(addr).await.unwrap();

tests/legacy_client.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
#![cfg(all(feature = "client-legacy", any(feature = "http1", feature = "http2")))]
1+
#![cfg(all(
2+
feature = "client-legacy",
3+
feature = "tokio",
4+
any(feature = "http1", feature = "http2")
5+
))]
26

37
mod test_utils;
48

59
use std::io::{Read, Write};
6-
use std::net::{SocketAddr, TcpListener};
10+
#[cfg(feature = "http2")]
11+
use std::net::SocketAddr;
12+
use std::net::TcpListener;
713
use std::pin::Pin;
814
use std::sync::atomic::Ordering;
915
use std::sync::Arc;
@@ -16,15 +22,19 @@ use futures_util::future::{self, FutureExt, TryFutureExt};
1622
use futures_util::stream::StreamExt;
1723
use futures_util::{self, Stream};
1824
use http_body_util::BodyExt;
19-
use http_body_util::{Empty, Full, StreamBody};
25+
#[cfg(feature = "http2")]
26+
use http_body_util::Full;
27+
use http_body_util::{Empty, StreamBody};
2028
use tokio::io::{AsyncReadExt, AsyncWriteExt};
2129

2230
use hyper::body::Bytes;
2331
use hyper::body::Frame;
2432
use hyper::Request;
2533
use hyper_util::client::legacy::connect::{capture_connection, HttpConnector};
2634
use hyper_util::client::legacy::Client;
27-
use hyper_util::rt::{TokioExecutor, TokioIo};
35+
use hyper_util::rt::TokioExecutor;
36+
#[cfg(feature = "http2")]
37+
use hyper_util::rt::TokioIo;
2838

2939
use test_utils::{DebugConnector, DebugStream};
3040

@@ -815,7 +825,7 @@ fn client_upgrade() {
815825
}
816826

817827
#[cfg(not(miri))]
818-
#[cfg(feature = "server")]
828+
#[cfg(all(feature = "server", feature = "http1", feature = "http2"))]
819829
#[test]
820830
fn client_http2_upgrade() {
821831
use http::{Method, Response, Version};
@@ -1346,8 +1356,8 @@ impl tower_service::Service<hyper::Uri> for MockConnector {
13461356
// Test for connection error propagation with PR #184.
13471357
// Simulates a connection failure by setting failed=true and returning a custom io::Error.
13481358
// Verifies the error propagates through hyper’s client as a hyper::Error(Io, ...).
1349-
#[cfg(feature = "http1")]
13501359
#[tokio::test]
1360+
#[cfg(feature = "http1")]
13511361
async fn test_connection_error_propagation_pr184() {
13521362
// Define the error message for the simulated connection failure.
13531363
// Reused for creating the error and verifying the result.
@@ -1404,8 +1414,8 @@ async fn test_connection_error_propagation_pr184() {
14041414
// Simulates a connection that returns EOF immediately, causing hyper’s HTTP/1.1 parser
14051415
// to fail with IncompleteMessage due to no response data.
14061416
// Uses MockConnector with conn_error=None to keep failed=false, ensuring EOF behavior.
1407-
#[cfg(feature = "http1")]
14081417
#[tokio::test]
1418+
#[cfg(feature = "http1")]
14091419
async fn test_incomplete_message_error_pr184() {
14101420
// Create an empty IoBuilder to simulate a connection with no data.
14111421
// No write or read expectations, so poll_read returns EOF (Poll::Ready(Ok(0))).
@@ -1464,8 +1474,8 @@ async fn test_incomplete_message_error_pr184() {
14641474
// Test for a successful HTTP/1.1 connection using a mock connector.
14651475
// Simulates a server that accepts a request and responds with a 200 OK.
14661476
// Verifies the client correctly sends the request and receives the response.
1467-
#[cfg(feature = "http1")]
14681477
#[tokio::test]
1478+
#[cfg(feature = "http1")]
14691479
async fn test_successful_connection() {
14701480
// Define the expected server response: a valid HTTP/1.1 200 OK with no body.
14711481
let response = b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n";

tests/proxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![cfg(all(feature = "client-legacy", feature = "client-proxy"))]
1+
#![cfg(all(feature = "client-legacy", feature = "client-proxy", feature = "tokio"))]
22

33
use tokio::io::{AsyncReadExt, AsyncWriteExt};
44
use tokio::net::{TcpListener, TcpStream};

0 commit comments

Comments
 (0)