]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/unused_io_amount.rs
Auto merge of #5334 - flip1995:backport_remerge, 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 as hir;
3 use rustc_lint::{LateContext, LateLintPass};
4 use rustc_session::{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(_vectored)` and
10     /// `io::Read::read(_vectored)` are not 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         let read_trait = match_trait_method(cx, call, &paths::IO_READ);
71         let write_trait = match_trait_method(cx, call, &paths::IO_WRITE);
72
73         match (read_trait, write_trait, symbol) {
74             (true, _, "read") => span_lint(
75                 cx,
76                 UNUSED_IO_AMOUNT,
77                 expr.span,
78                 "read amount is not handled. Use `Read::read_exact` instead",
79             ),
80             (true, _, "read_vectored") => span_lint(cx, UNUSED_IO_AMOUNT, expr.span, "read amount is not handled"),
81             (_, true, "write") => span_lint(
82                 cx,
83                 UNUSED_IO_AMOUNT,
84                 expr.span,
85                 "written amount is not handled. Use `Write::write_all` instead",
86             ),
87             (_, true, "write_vectored") => span_lint(cx, UNUSED_IO_AMOUNT, expr.span, "written amount is not handled"),
88             _ => (),
89         }
90     }
91 }