Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b3971fd

Browse files
committed
Lint vectored IO in unused_io_amount lint
1 parent 50403de commit b3971fd

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

clippy_lints/src/unused_io_amount.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use rustc_session::declare_tool_lint;
77
declare_clippy_lint! {
88
/// **What it does:** Checks for unused written/read amount.
99
///
10-
/// **Why is this bad?** `io::Write::write` and `io::Read::read` are not
11-
/// guaranteed to
10+
/// **Why is this bad?** `io::Write::write(_vectored)` and
11+
/// `io::Read::read(_vectored)` are not guaranteed to
1212
/// process the entire buffer. They return how many bytes were processed, which
1313
/// might be smaller
1414
/// than a given buffer's length. If you don't need to deal with
@@ -68,20 +68,25 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedIoAmount {
6868
fn check_method_call(cx: &LateContext<'_, '_>, call: &hir::Expr<'_>, expr: &hir::Expr<'_>) {
6969
if let hir::ExprKind::MethodCall(ref path, _, _) = call.kind {
7070
let symbol = &*path.ident.as_str();
71-
if match_trait_method(cx, call, &paths::IO_READ) && symbol == "read" {
72-
span_lint(
71+
let read_trait = match_trait_method(cx, call, &paths::IO_READ);
72+
let write_trait = match_trait_method(cx, call, &paths::IO_WRITE);
73+
74+
match (read_trait, write_trait, symbol) {
75+
(true, _, "read") => span_lint(
7376
cx,
7477
UNUSED_IO_AMOUNT,
7578
expr.span,
76-
"handle read amount returned or use `Read::read_exact` instead",
77-
);
78-
} else if match_trait_method(cx, call, &paths::IO_WRITE) && symbol == "write" {
79-
span_lint(
79+
"read amount is not handled. Use `Read::read_exact` instead",
80+
),
81+
(true, _, "read_vectored") => span_lint(cx, UNUSED_IO_AMOUNT, expr.span, "read amount is not handled"),
82+
(_, true, "write") => span_lint(
8083
cx,
8184
UNUSED_IO_AMOUNT,
8285
expr.span,
83-
"handle written amount returned or use `Write::write_all` instead",
84-
);
86+
"written amount is not handled. Use `Write::write_all` instead",
87+
),
88+
(_, true, "write_vectored") => span_lint(cx, UNUSED_IO_AMOUNT, expr.span, "written amount is not handled"),
89+
_ => (),
8590
}
8691
}
8792
}

tests/ui/unused_io_amount.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ fn unwrap<T: io::Read + io::Write>(s: &mut T) {
1616
s.read(&mut buf).unwrap();
1717
}
1818

19+
fn vectored<T: io::Read + io::Write>(s: &mut T) -> io::Result<()> {
20+
s.read_vectored(&mut [io::IoSliceMut::new(&mut [])])?;
21+
s.write_vectored(&[io::IoSlice::new(&[])])?;
22+
Ok(())
23+
}
24+
1925
fn main() {}

tests/ui/unused_io_amount.stderr

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
1-
error: handle written amount returned or use `Write::write_all` instead
1+
error: written amount is not handled. Use `Write::write_all` instead
22
--> $DIR/unused_io_amount.rs:7:5
33
|
44
LL | s.write(b"test")?;
55
| ^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::unused-io-amount` implied by `-D warnings`
88

9-
error: handle read amount returned or use `Read::read_exact` instead
9+
error: read amount is not handled. Use `Read::read_exact` instead
1010
--> $DIR/unused_io_amount.rs:9:5
1111
|
1212
LL | s.read(&mut buf)?;
1313
| ^^^^^^^^^^^^^^^^^
1414

15-
error: handle written amount returned or use `Write::write_all` instead
15+
error: written amount is not handled. Use `Write::write_all` instead
1616
--> $DIR/unused_io_amount.rs:14:5
1717
|
1818
LL | s.write(b"test").unwrap();
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^
2020

21-
error: handle read amount returned or use `Read::read_exact` instead
21+
error: read amount is not handled. Use `Read::read_exact` instead
2222
--> $DIR/unused_io_amount.rs:16:5
2323
|
2424
LL | s.read(&mut buf).unwrap();
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^
2626

27-
error: aborting due to 4 previous errors
27+
error: read amount is not handled
28+
--> $DIR/unused_io_amount.rs:20:5
29+
|
30+
LL | s.read_vectored(&mut [io::IoSliceMut::new(&mut [])])?;
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
33+
error: written amount is not handled
34+
--> $DIR/unused_io_amount.rs:21:5
35+
|
36+
LL | s.write_vectored(&[io::IoSlice::new(&[])])?;
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38+
39+
error: aborting due to 6 previous errors
2840

0 commit comments

Comments
 (0)