]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/unused_io_amount.rs
Rename `match_path_old` to `match_path`
[rust.git] / clippy_lints / src / unused_io_amount.rs
1 use rustc::lint::*;
2 use rustc::hir;
3 use utils::{span_lint, match_qpath, match_trait_method, is_try, paths};
4
5 /// **What it does:** Checks for unused written/read amount.
6 ///
7 /// **Why is this bad?** `io::Write::write` and `io::Read::read` are not
8 /// guaranteed to
9 /// process the entire buffer. They return how many bytes were processed, which
10 /// might be smaller
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.
14 ///
15 /// **Known problems:** Detects only common patterns.
16 ///
17 /// **Example:**
18 /// ```rust,ignore
19 /// use std::io;
20 /// fn foo<W: io::Write>(w: &mut W) -> io::Result<()> {
21 ///     // must be `w.write_all(b"foo")?;`
22 ///     w.write(b"foo")?;
23 ///     Ok(())
24 /// }
25 /// ```
26 declare_lint! {
27     pub UNUSED_IO_AMOUNT,
28     Deny,
29     "unused written/read amount"
30 }
31
32 pub struct UnusedIoAmount;
33
34 impl LintPass for UnusedIoAmount {
35     fn get_lints(&self) -> LintArray {
36         lint_array!(UNUSED_IO_AMOUNT)
37     }
38 }
39
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, _) |
44             hir::StmtExpr(ref expr, _) => &**expr,
45             _ => return,
46         };
47
48         match expr.node {
49             hir::ExprMatch(ref res, _, _) if is_try(expr).is_some() => {
50                 if let hir::ExprCall(ref func, ref args) = res.node {
51                     if let hir::ExprPath(ref path) = func.node {
52                         if match_qpath(path, &paths::TRY_INTO_RESULT) && args.len() == 1 {
53                             check_method_call(cx, &args[0], expr);
54                         }
55                     }
56                 } else {
57                     check_method_call(cx, res, expr);
58                 }
59             },
60
61             hir::ExprMethodCall(ref path, _, ref args) => {
62                 match &*path.name.as_str() {
63                     "expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
64                         check_method_call(cx, &args[0], expr);
65                     },
66                     _ => (),
67                 }
68             },
69
70             _ => (),
71         }
72     }
73 }
74
75 fn check_method_call(cx: &LateContext, call: &hir::Expr, expr: &hir::Expr) {
76     if let hir::ExprMethodCall(ref path, _, _) = call.node {
77         let symbol = &*path.name.as_str();
78         if match_trait_method(cx, call, &paths::IO_READ) && symbol == "read" {
79             span_lint(
80                 cx,
81                 UNUSED_IO_AMOUNT,
82                 expr.span,
83                 "handle read amount returned or use `Read::read_exact` instead",
84             );
85         } else if match_trait_method(cx, call, &paths::IO_WRITE) && symbol == "write" {
86             span_lint(
87                 cx,
88                 UNUSED_IO_AMOUNT,
89                 expr.span,
90                 "handle written amount returned or use `Write::write_all` instead",
91             );
92         }
93     }
94 }