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