Skip to content

Commit

Permalink
improve unused_io_amount
Browse files Browse the repository at this point in the history
  • Loading branch information
ABouttefeux committed Apr 16, 2021
1 parent 28dbcd8 commit 55f4564
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
51 changes: 31 additions & 20 deletions clippy_lints/src/unused_io_amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,38 @@ impl<'tcx> LateLintPass<'tcx> for UnusedIoAmount {
}

fn check_method_call(cx: &LateContext<'_>, call: &hir::Expr<'_>, expr: &hir::Expr<'_>) {
if let hir::ExprKind::MethodCall(path, _, _, _) = call.kind {
let symbol = &*path.ident.as_str();
let read_trait = match_trait_method(cx, call, &paths::IO_READ);
let write_trait = match_trait_method(cx, call, &paths::IO_WRITE);
if let hir::ExprKind::MethodCall(ref path, _, ref args, _) = call.kind {
match &*path.ident.as_str() {
"ok" | "or" | "or_else" => {
check_method_call(cx, &args[0], expr);
},
_ => {
let symbol = &*path.ident.as_str();
let read_trait = match_trait_method(cx, call, &paths::IO_READ);
let write_trait = match_trait_method(cx, call, &paths::IO_WRITE);

match (read_trait, write_trait, symbol) {
(true, _, "read") => span_lint(
cx,
UNUSED_IO_AMOUNT,
expr.span,
"read amount is not handled. Use `Read::read_exact` instead",
),
(true, _, "read_vectored") => span_lint(cx, UNUSED_IO_AMOUNT, expr.span, "read amount is not handled"),
(_, true, "write") => span_lint(
cx,
UNUSED_IO_AMOUNT,
expr.span,
"written amount is not handled. Use `Write::write_all` instead",
),
(_, true, "write_vectored") => span_lint(cx, UNUSED_IO_AMOUNT, expr.span, "written amount is not handled"),
_ => (),
match (read_trait, write_trait, symbol) {
(true, _, "read") => span_lint(
cx,
UNUSED_IO_AMOUNT,
expr.span,
"read amount is not handled. Use `Read::read_exact` instead",
),
(true, _, "read_vectored") => {
span_lint(cx, UNUSED_IO_AMOUNT, expr.span, "read amount is not handled")
},
(_, true, "write") => span_lint(
cx,
UNUSED_IO_AMOUNT,
expr.span,
"written amount is not handled. Use `Write::write_all` instead",
),
(_, true, "write_vectored") => {
span_lint(cx, UNUSED_IO_AMOUNT, expr.span, "written amount is not handled")
},
_ => (),
}
},
}
}
}
9 changes: 8 additions & 1 deletion tests/ui/unused_io_amount.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(dead_code)]
#![warn(clippy::unused_io_amount)]

use std::io;
use std::io::{self, Read};

fn question_mark<T: io::Read + io::Write>(s: &mut T) -> io::Result<()> {
s.write(b"test")?;
Expand All @@ -22,4 +22,11 @@ fn vectored<T: io::Read + io::Write>(s: &mut T) -> io::Result<()> {
Ok(())
}

fn test(file: &str) -> Option<()> {
let mut reader = std::fs::File::open(file).ok()?;
let mut result = [0u8; 0];
reader.read(&mut result).ok()?;
Some(())
}

fn main() {}
20 changes: 13 additions & 7 deletions tests/ui/unused_io_amount.stderr
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
error: written amount is not handled. Use `Write::write_all` instead
--> $DIR/unused_io_amount.rs:7:5
--> $DIR/unused_io_amount.rs:8:5
|
LL | s.write(b"test")?;
| ^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::unused-io-amount` implied by `-D warnings`

error: read amount is not handled. Use `Read::read_exact` instead
--> $DIR/unused_io_amount.rs:9:5
--> $DIR/unused_io_amount.rs:10:5
|
LL | s.read(&mut buf)?;
| ^^^^^^^^^^^^^^^^^

error: written amount is not handled. Use `Write::write_all` instead
--> $DIR/unused_io_amount.rs:14:5
--> $DIR/unused_io_amount.rs:15:5
|
LL | s.write(b"test").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: read amount is not handled. Use `Read::read_exact` instead
--> $DIR/unused_io_amount.rs:16:5
--> $DIR/unused_io_amount.rs:17:5
|
LL | s.read(&mut buf).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: read amount is not handled
--> $DIR/unused_io_amount.rs:20:5
--> $DIR/unused_io_amount.rs:21:5
|
LL | s.read_vectored(&mut [io::IoSliceMut::new(&mut [])])?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: written amount is not handled
--> $DIR/unused_io_amount.rs:21:5
--> $DIR/unused_io_amount.rs:22:5
|
LL | s.write_vectored(&[io::IoSlice::new(&[])])?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 6 previous errors
error: read amount is not handled. Use `Read::read_exact` instead
--> $DIR/unused_io_amount.rs:29:5
|
LL | reader.read(&mut result).ok()?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 7 previous errors

0 comments on commit 55f4564

Please sign in to comment.