]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/unused_io_amount.rs
Auto merge of #4938 - flip1995:rustup, r=flip1995
[rust.git] / clippy_lints / src / unused_io_amount.rs
1 use crate::utils::{is_try, match_qpath, match_trait_method, paths, span_lint};
2 use rustc::declare_lint_pass;
3 use rustc::hir;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc_session::declare_tool_lint;
6
7 declare_clippy_lint! {
8     /// **What it does:** Checks for unused written/read amount.
9     ///
10     /// **Why is this bad?** `io::Write::write` and `io::Read::read` are not
11     /// guaranteed to
12     /// process the entire buffer. They return how many bytes were processed, which
13     /// might be smaller
14     /// than a given buffer's length. If you don't need to deal with
15     /// partial-write/read, use
16     /// `write_all`/`read_exact` instead.
17     ///
18     /// **Known problems:** Detects only common patterns.
19     ///
20     /// **Example:**
21     /// ```rust,ignore
22     /// use std::io;
23     /// fn foo<W: io::Write>(w: &mut W) -> io::Result<()> {
24     ///     // must be `w.write_all(b"foo")?;`
25     ///     w.write(b"foo")?;
26     ///     Ok(())
27     /// }
28     /// ```
29     pub UNUSED_IO_AMOUNT,
30     correctness,
31     "unused written/read amount"
32 }
33
34 declare_lint_pass!(UnusedIoAmount => [UNUSED_IO_AMOUNT]);
35
36 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedIoAmount {
37     fn check_stmt(&mut self, cx: &LateContext<'_, '_>, s: &hir::Stmt) {
38         let expr = match s.kind {
39             hir::StmtKind::Semi(ref expr) | hir::StmtKind::Expr(ref expr) => &**expr,
40             _ => return,
41         };
42
43         match expr.kind {
44             hir::ExprKind::Match(ref res, _, _) if is_try(expr).is_some() => {
45                 if let hir::ExprKind::Call(ref func, ref args) = res.kind {
46                     if let hir::ExprKind::Path(ref path) = func.kind {
47                         if match_qpath(path, &paths::TRY_INTO_RESULT) && args.len() == 1 {
48                             check_method_call(cx, &args[0], expr);
49                         }
50                     }
51                 } else {
52                     check_method_call(cx, res, expr);
53                 }
54             },
55
56             hir::ExprKind::MethodCall(ref path, _, ref args) => match &*path.ident.as_str() {
57                 "expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
58                     check_method_call(cx, &args[0], expr);
59                 },
60                 _ => (),
61             },
62
63             _ => (),
64         }
65     }
66 }
67
68 fn check_method_call(cx: &LateContext<'_, '_>, call: &hir::Expr, expr: &hir::Expr) {
69     if let hir::ExprKind::MethodCall(ref path, _, _) = call.kind {
70         let symbol = &*path.ident.as_str();
71         if match_trait_method(cx, call, &paths::IO_READ) && symbol == "read" {
72             span_lint(
73                 cx,
74                 UNUSED_IO_AMOUNT,
75                 expr.span,
76                 "handle read amount returned or use `Read::read_exact` instead",
77             );
78         } else if match_trait_method(cx, call, &paths::IO_WRITE) && symbol == "write" {
79             span_lint(
80                 cx,
81                 UNUSED_IO_AMOUNT,
82                 expr.span,
83                 "handle written amount returned or use `Write::write_all` instead",
84             );
85         }
86     }
87 }