]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/unused_io_amount.rs
Auto merge of #3946 - rchaser53:issue-3920, 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::hir;
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc::{declare_tool_lint, lint_array};
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 pub struct UnusedIoAmount;
34
35 impl LintPass for UnusedIoAmount {
36     fn get_lints(&self) -> LintArray {
37         lint_array!(UNUSED_IO_AMOUNT)
38     }
39
40     fn name(&self) -> &'static str {
41         "UnusedIoAmount"
42     }
43 }
44
45 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedIoAmount {
46     fn check_stmt(&mut self, cx: &LateContext<'_, '_>, s: &hir::Stmt) {
47         let expr = match s.node {
48             hir::StmtKind::Semi(ref expr) | hir::StmtKind::Expr(ref expr) => &**expr,
49             _ => return,
50         };
51
52         match expr.node {
53             hir::ExprKind::Match(ref res, _, _) if is_try(cx, expr).is_some() => {
54                 if let hir::ExprKind::Call(ref func, ref args) = res.node {
55                     if let hir::ExprKind::Path(ref path) = func.node {
56                         if match_qpath(path, &paths::TRY_INTO_RESULT) && args.len() == 1 {
57                             check_method_call(cx, &args[0], expr);
58                         }
59                     }
60                 } else {
61                     check_method_call(cx, res, expr);
62                 }
63             },
64
65             hir::ExprKind::MethodCall(ref path, _, ref args) => match &*path.ident.as_str() {
66                 "expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
67                     check_method_call(cx, &args[0], expr);
68                 },
69                 _ => (),
70             },
71
72             _ => (),
73         }
74     }
75 }
76
77 fn check_method_call(cx: &LateContext<'_, '_>, call: &hir::Expr, expr: &hir::Expr) {
78     if let hir::ExprKind::MethodCall(ref path, _, _) = call.node {
79         let symbol = &*path.ident.as_str();
80         if match_trait_method(cx, call, &paths::IO_READ) && symbol == "read" {
81             span_lint(
82                 cx,
83                 UNUSED_IO_AMOUNT,
84                 expr.span,
85                 "handle read amount returned or use `Read::read_exact` instead",
86             );
87         } else if match_trait_method(cx, call, &paths::IO_WRITE) && symbol == "write" {
88             span_lint(
89                 cx,
90                 UNUSED_IO_AMOUNT,
91                 expr.span,
92                 "handle written amount returned or use `Write::write_all` instead",
93             );
94         }
95     }
96 }