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 a149488
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 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() {}
8 changes: 7 additions & 1 deletion tests/ui/unused_io_amount.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,11 @@ error: written amount is not handled
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:28:5
|
LL | reader.read(&mut result).ok()?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 7 previous errors

0 comments on commit a149488

Please sign in to comment.