]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/unused_io_amount.rs
Do some additional cleanup around `InternedString` and `Symbol`
[rust.git] / clippy_lints / src / unused_io_amount.rs
1 use rustc::lint::*;
2 use rustc::hir;
3 use utils::{span_lint, match_path, 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 guaranteed to
8 /// process the entire buffer. They return how many bytes were processed, which might be smaller
9 /// than a given buffer's length. If you don't need to deal with partial-write/read, use
10 /// `write_all`/`read_exact` instead.
11 ///
12 /// **Known problems:** Detects only common patterns.
13 ///
14 /// **Example:**
15 /// ```rust,ignore
16 /// use std::io;
17 /// fn foo<W: io::Write>(w: &mut W) -> io::Result<()> {
18 ///     // must be `w.write_all(b"foo")?;`
19 ///     w.write(b"foo")?;
20 ///     Ok(())
21 /// }
22 /// ```
23 declare_lint! {
24     pub UNUSED_IO_AMOUNT,
25     Deny,
26     "unused written/read amount"
27 }
28
29 pub struct UnusedIoAmount;
30
31 impl LintPass for UnusedIoAmount {
32     fn get_lints(&self) -> LintArray {
33         lint_array!(UNUSED_IO_AMOUNT)
34     }
35 }
36
37 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedIoAmount {
38     fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) {
39         let expr = match s.node {
40             hir::StmtSemi(ref expr, _) |
41             hir::StmtExpr(ref expr, _) => &**expr,
42             _ => return,
43         };
44
45         match expr.node {
46             hir::ExprMatch(ref res, _, _) if is_try(expr).is_some() => {
47                 if let hir::ExprCall(ref func, ref args) = res.node {
48                     if let hir::ExprPath(ref path) = func.node {
49                         if match_path(path, &paths::CARRIER_TRANSLATE) && args.len() == 1 {
50                             check_method_call(cx, &args[0], expr);
51                         }
52                     }
53                 } else {
54                     check_method_call(cx, res, expr);
55                 }
56             },
57
58             hir::ExprMethodCall(ref symbol, _, ref args) => {
59                 match &*symbol.node.as_str() {
60                     "expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
61                         check_method_call(cx, &args[0], expr);
62                     },
63                     _ => (),
64                 }
65             },
66
67             _ => (),
68         }
69     }
70 }
71
72 fn check_method_call(cx: &LateContext, call: &hir::Expr, expr: &hir::Expr) {
73     if let hir::ExprMethodCall(ref symbol, _, _) = call.node {
74         let symbol = &*symbol.node.as_str();
75         if match_trait_method(cx, call, &paths::IO_READ) && symbol == "read" {
76             span_lint(cx,
77                       UNUSED_IO_AMOUNT,
78                       expr.span,
79                       "handle read amount returned or use `Read::read_exact` instead");
80         } else if match_trait_method(cx, call, &paths::IO_WRITE) && symbol == "write" {
81             span_lint(cx,
82                       UNUSED_IO_AMOUNT,
83                       expr.span,
84                       "handle written amount returned or use `Write::write_all` instead");
85         }
86     }
87 }