]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/unused_io_amount.rs
Auto merge of #3680 - g-bartoszek:needless-bool-else-if-brackets, r=oli-obk
[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 /// **What it does:** Checks for unused written/read amount.
7 ///
8 /// **Why is this bad?** `io::Write::write` and `io::Read::read` are not
9 /// guaranteed to
10 /// process the entire buffer. They return how many bytes were processed, which
11 /// might be smaller
12 /// than a given buffer's length. If you don't need to deal with
13 /// partial-write/read, use
14 /// `write_all`/`read_exact` instead.
15 ///
16 /// **Known problems:** Detects only common patterns.
17 ///
18 /// **Example:**
19 /// ```rust,ignore
20 /// use std::io;
21 /// fn foo<W: io::Write>(w: &mut W) -> io::Result<()> {
22 ///     // must be `w.write_all(b"foo")?;`
23 ///     w.write(b"foo")?;
24 ///     Ok(())
25 /// }
26 /// ```
27 declare_clippy_lint! {
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
41 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedIoAmount {
42     fn check_stmt(&mut self, cx: &LateContext<'_, '_>, s: &hir::Stmt) {
43         let expr = match s.node {
44             hir::StmtKind::Semi(ref expr) | hir::StmtKind::Expr(ref expr) => &**expr,
45             _ => return,
46         };
47
48         match expr.node {
49             hir::ExprKind::Match(ref res, _, _) if is_try(expr).is_some() => {
50                 if let hir::ExprKind::Call(ref func, ref args) = res.node {
51                     if let hir::ExprKind::Path(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::ExprKind::MethodCall(ref path, _, ref args) => match &*path.ident.as_str() {
62                 "expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
63                     check_method_call(cx, &args[0], expr);
64                 },
65                 _ => (),
66             },
67
68             _ => (),
69         }
70     }
71 }
72
73 fn check_method_call(cx: &LateContext<'_, '_>, call: &hir::Expr, expr: &hir::Expr) {
74     if let hir::ExprKind::MethodCall(ref path, _, _) = call.node {
75         let symbol = &*path.ident.as_str();
76         if match_trait_method(cx, call, &paths::IO_READ) && symbol == "read" {
77             span_lint(
78                 cx,
79                 UNUSED_IO_AMOUNT,
80                 expr.span,
81                 "handle read amount returned or use `Read::read_exact` instead",
82             );
83         } else if match_trait_method(cx, call, &paths::IO_WRITE) && symbol == "write" {
84             span_lint(
85                 cx,
86                 UNUSED_IO_AMOUNT,
87                 expr.span,
88                 "handle written amount returned or use `Write::write_all` instead",
89             );
90         }
91     }
92 }