diff --git a/clippy_lints/src/unused_io_amount.rs b/clippy_lints/src/unused_io_amount.rs index 5e8e530f480f..3e5a0bcc78d0 100644 --- a/clippy_lints/src/unused_io_amount.rs +++ b/clippy_lints/src/unused_io_amount.rs @@ -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") + }, + _ => (), + } + }, } } } diff --git a/tests/ui/unused_io_amount.rs b/tests/ui/unused_io_amount.rs index ebaba9629db1..136143b62f2c 100644 --- a/tests/ui/unused_io_amount.rs +++ b/tests/ui/unused_io_amount.rs @@ -1,7 +1,7 @@ #![allow(dead_code)] #![warn(clippy::unused_io_amount)] -use std::io; +use std::io::{self, Read}; fn question_mark(s: &mut T) -> io::Result<()> { s.write(b"test")?; @@ -22,4 +22,11 @@ fn vectored(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() {} diff --git a/tests/ui/unused_io_amount.stderr b/tests/ui/unused_io_amount.stderr index 5219d63980b4..a720a860c4d0 100644 --- a/tests/ui/unused_io_amount.stderr +++ b/tests/ui/unused_io_amount.stderr @@ -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