3 use utils::{is_try, match_qpath, match_trait_method, paths, span_lint};
5 /// **What it does:** Checks for unused written/read amount.
7 /// **Why is this bad?** `io::Write::write` and `io::Read::read` are not
9 /// process the entire buffer. They return how many bytes were processed, which
11 /// than a given buffer's length. If you don't need to deal with
12 /// partial-write/read, use
13 /// `write_all`/`read_exact` instead.
15 /// **Known problems:** Detects only common patterns.
20 /// fn foo<W: io::Write>(w: &mut W) -> io::Result<()> {
21 /// // must be `w.write_all(b"foo")?;`
29 "unused written/read amount"
32 pub struct UnusedIoAmount;
34 impl LintPass for UnusedIoAmount {
35 fn get_lints(&self) -> LintArray {
36 lint_array!(UNUSED_IO_AMOUNT)
40 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedIoAmount {
41 fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) {
42 let expr = match s.node {
43 hir::StmtSemi(ref expr, _) | hir::StmtExpr(ref expr, _) => &**expr,
48 hir::ExprMatch(ref res, _, _) if is_try(expr).is_some() => {
49 if let hir::ExprCall(ref func, ref args) = res.node {
50 if let hir::ExprPath(ref path) = func.node {
51 if match_qpath(path, &paths::TRY_INTO_RESULT) && args.len() == 1 {
52 check_method_call(cx, &args[0], expr);
56 check_method_call(cx, res, expr);
60 hir::ExprMethodCall(ref path, _, ref args) => match &*path.name.as_str() {
61 "expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
62 check_method_call(cx, &args[0], expr);
72 fn check_method_call(cx: &LateContext, call: &hir::Expr, expr: &hir::Expr) {
73 if let hir::ExprMethodCall(ref path, _, _) = call.node {
74 let symbol = &*path.name.as_str();
75 if match_trait_method(cx, call, &paths::IO_READ) && symbol == "read" {
80 "handle read amount returned or use `Read::read_exact` instead",
82 } else if match_trait_method(cx, call, &paths::IO_WRITE) && symbol == "write" {
87 "handle written amount returned or use `Write::write_all` instead",