Skip to content

Commit 4f534dd

Browse files
committed
Add tokio support to unused_ui_amount as well.
1 parent 26fa413 commit 4f534dd

File tree

6 files changed

+36
-3
lines changed

6 files changed

+36
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ serde = { version = "1.0", features = ["derive"] }
4949
syn = { version = "1.0", features = ["full"] }
5050
futures = "0.3"
5151
parking_lot = "0.11.2"
52+
tokio = { version = "1", features = ["io-util"] }
5253

5354
[build-dependencies]
5455
rustc_tools_util = { version = "0.2", path = "rustc_tools_util" }

clippy_lints/src/unused_io_amount.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,13 @@ fn check_method_call(cx: &LateContext<'_>, call: &hir::Expr<'_>, expr: &hir::Exp
107107
let symbol = path.ident.as_str();
108108
let read_trait = if is_await {
109109
match_trait_method(cx, call, &paths::FUTURES_IO_ASYNCREADEXT)
110+
|| match_trait_method(cx, call, &paths::TOKIO_IO_ASYNCREADEXT)
110111
} else {
111112
match_trait_method(cx, call, &paths::IO_READ)
112113
};
113114
let write_trait = if is_await {
114115
match_trait_method(cx, call, &paths::FUTURES_IO_ASYNCWRITEEXT)
116+
|| match_trait_method(cx, call, &paths::TOKIO_IO_ASYNCWRITEEXT)
115117
} else {
116118
match_trait_method(cx, call, &paths::IO_WRITE)
117119
};

clippy_utils/src/paths.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ pub const SYM_MODULE: [&str; 3] = ["rustc_span", "symbol", "sym"];
198198
pub const SYNTAX_CONTEXT: [&str; 3] = ["rustc_span", "hygiene", "SyntaxContext"];
199199
pub const TO_OWNED_METHOD: [&str; 4] = ["alloc", "borrow", "ToOwned", "to_owned"];
200200
pub const TO_STRING_METHOD: [&str; 4] = ["alloc", "string", "ToString", "to_string"];
201+
#[allow(clippy::invalid_paths)] // internal lints do not know about all external crates
202+
pub const TOKIO_IO_ASYNCREADEXT: [&str; 5] = ["tokio", "io", "util", "async_read_ext", "AsyncReadExt"];
203+
#[allow(clippy::invalid_paths)] // internal lints do not know about all external crates
204+
pub const TOKIO_IO_ASYNCWRITEEXT: [&str; 5] = ["tokio", "io", "util", "async_write_ext", "AsyncWriteExt"];
201205
pub const TRY_FROM: [&str; 4] = ["core", "convert", "TryFrom", "try_from"];
202206
pub const VEC_AS_MUT_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_mut_slice"];
203207
pub const VEC_AS_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_slice"];

tests/compile-test.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static TEST_DEPENDENCIES: &[&str] = &[
2929
"serde",
3030
"serde_derive",
3131
"syn",
32+
"tokio",
3233
"parking_lot",
3334
];
3435

@@ -50,6 +51,8 @@ extern crate parking_lot;
5051
extern crate quote;
5152
#[allow(unused_extern_crates)]
5253
extern crate syn;
54+
#[allow(unused_extern_crates)]
55+
extern crate tokio;
5356

5457
/// Produces a string with an `--extern` flag for all UI test crate
5558
/// dependencies.

tests/ui/unused_io_amount.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,15 @@ async fn async_read_nested_or<R: AsyncRead + Unpin>(r: &mut R, do_it: bool) -> R
9595
Ok(buf)
9696
}
9797

98+
use tokio::io::{AsyncRead as TokioAsyncRead, AsyncReadExt as _, AsyncWrite as TokioAsyncWrite, AsyncWriteExt as _};
99+
100+
async fn bad_async_write_tokio<W: TokioAsyncWrite + Unpin>(w: &mut W) {
101+
w.write(b"hello world").await.unwrap();
102+
}
103+
104+
async fn bad_async_read_tokio<R: TokioAsyncRead + Unpin>(r: &mut R) {
105+
let mut buf = [0u8; 0];
106+
r.read(&mut buf[..]).await.unwrap();
107+
}
108+
98109
fn main() {}

tests/ui/unused_io_amount.stderr

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,28 @@ LL | r.read(&mut buf[..]).await.unwrap();
7777
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7878

7979
error: written amount is not handled. Use `AsyncWriteExt::write_all` instead
80-
--> $DIR/unused_io_amount.rs:86:9
80+
--> $DIR/unused_io_amount.rs:85:9
8181
|
8282
LL | w.write(b"hello world").await?;
8383
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8484

8585
error: read amount is not handled. Use `AsyncReadExt::read_exact` instead
86-
--> $DIR/unused_io_amount.rs:94:9
86+
--> $DIR/unused_io_amount.rs:93:9
8787
|
8888
LL | r.read(&mut buf[..]).await.or(Err(Error::Kind))?;
8989
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9090

91-
error: aborting due to 14 previous errors
91+
error: written amount is not handled. Use `AsyncWriteExt::write_all` instead
92+
--> $DIR/unused_io_amount.rs:101:5
93+
|
94+
LL | w.write(b"hello world").await.unwrap();
95+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
96+
97+
error: read amount is not handled. Use `AsyncReadExt::read_exact` instead
98+
--> $DIR/unused_io_amount.rs:106:5
99+
|
100+
LL | r.read(&mut buf[..]).await.unwrap();
101+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
102+
103+
error: aborting due to 16 previous errors
92104

0 commit comments

Comments
 (0)