Skip to content

Commit 2c7cfa8

Browse files
committed
Auto merge of #5027 - sinkuu:vectored_io, r=phansch
Lint vectored IO in unused_io_amount lint `read_vectored` & `write_vectored` require handling returned value likewise non-vectored methods. rust-lang/rust#68041 --- changelog: lint vectored IO in `unused_io_amount` lint
2 parents 3237b7a + b3971fd commit 2c7cfa8

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

clippy_lints/src/unused_io_amount.rs

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

tests/ui/unused_io_amount.rs

+6
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

+17-5
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)