1 use crate::utils::{is_try, match_qpath, match_trait_method, paths, span_lint};
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc::{declare_tool_lint, lint_array};
6 /// **What it does:** Checks for unused written/read amount.
8 /// **Why is this bad?** `io::Write::write` and `io::Read::read` are not
10 /// process the entire buffer. They return how many bytes were processed, which
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.
16 /// **Known problems:** Detects only common patterns.
21 /// fn foo<W: io::Write>(w: &mut W) -> io::Result<()> {
22 /// // must be `w.write_all(b"foo")?;`
27 declare_clippy_lint! {
30 "unused written/read amount"
33 pub struct UnusedIoAmount;
35 impl LintPass for UnusedIoAmount {
36 fn get_lints(&self) -> LintArray {
37 lint_array!(UNUSED_IO_AMOUNT)
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,
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);
57 check_method_call(cx, res, expr);
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);
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" {
81 "handle read amount returned or use `Read::read_exact` instead",
83 } else if match_trait_method(cx, call, &paths::IO_WRITE) && symbol == "write" {
88 "handle written amount returned or use `Write::write_all` instead",