]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/unused_io_amount.rs
Update Clippy for MethodCall changes
[rust.git] / clippy_lints / src / unused_io_amount.rs
index bf31b774bba24fd66c9f50c5de431805a3253f65..5f4b5fd9dd91cb2c3942e8923435188065c655a7 100644 (file)
@@ -1,13 +1,13 @@
 use crate::utils::{is_try, match_qpath, match_trait_method, paths, span_lint};
-use rustc::hir;
-use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use rustc::{declare_tool_lint, lint_array};
+use rustc_hir as hir;
+use rustc_lint::{LateContext, LateLintPass};
+use rustc_session::{declare_lint_pass, declare_tool_lint};
 
 declare_clippy_lint! {
     /// **What it does:** Checks for unused written/read amount.
     ///
-    /// **Why is this bad?** `io::Write::write` and `io::Read::read` are not
-    /// guaranteed to
+    /// **Why is this bad?** `io::Write::write(_vectored)` and
+    /// `io::Read::read(_vectored)` are not guaranteed to
     /// process the entire buffer. They return how many bytes were processed, which
     /// might be smaller
     /// than a given buffer's length. If you don't need to deal with
     "unused written/read amount"
 }
 
-pub struct UnusedIoAmount;
-
-impl LintPass for UnusedIoAmount {
-    fn get_lints(&self) -> LintArray {
-        lint_array!(UNUSED_IO_AMOUNT)
-    }
-
-    fn name(&self) -> &'static str {
-        "UnusedIoAmount"
-    }
-}
+declare_lint_pass!(UnusedIoAmount => [UNUSED_IO_AMOUNT]);
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedIoAmount {
-    fn check_stmt(&mut self, cx: &LateContext<'_, '_>, s: &hir::Stmt) {
-        let expr = match s.node {
+    fn check_stmt(&mut self, cx: &LateContext<'_, '_>, s: &hir::Stmt<'_>) {
+        let expr = match s.kind {
             hir::StmtKind::Semi(ref expr) | hir::StmtKind::Expr(ref expr) => &**expr,
             _ => return,
         };
 
-        match expr.node {
-            hir::ExprKind::Match(ref res, _, _) if is_try(cx, expr).is_some() => {
-                if let hir::ExprKind::Call(ref func, ref args) = res.node {
-                    if let hir::ExprKind::Path(ref path) = func.node {
+        match expr.kind {
+            hir::ExprKind::Match(ref res, _, _) if is_try(expr).is_some() => {
+                if let hir::ExprKind::Call(ref func, ref args) = res.kind {
+                    if let hir::ExprKind::Path(ref path) = func.kind {
                         if match_qpath(path, &paths::TRY_INTO_RESULT) && args.len() == 1 {
                             check_method_call(cx, &args[0], expr);
                         }
@@ -62,7 +52,7 @@ fn check_stmt(&mut self, cx: &LateContext<'_, '_>, s: &hir::Stmt) {
                 }
             },
 
-            hir::ExprKind::MethodCall(ref path, _, ref args) => match &*path.ident.as_str() {
+            hir::ExprKind::MethodCall(ref path, _, ref args, _) => match &*path.ident.as_str() {
                 "expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
                     check_method_call(cx, &args[0], expr);
                 },
@@ -74,23 +64,28 @@ fn check_stmt(&mut self, cx: &LateContext<'_, '_>, s: &hir::Stmt) {
     }
 }
 
-fn check_method_call(cx: &LateContext<'_, '_>, call: &hir::Expr, expr: &hir::Expr) {
-    if let hir::ExprKind::MethodCall(ref path, _, _) = call.node {
+fn check_method_call(cx: &LateContext<'_, '_>, call: &hir::Expr<'_>, expr: &hir::Expr<'_>) {
+    if let hir::ExprKind::MethodCall(ref path, _, _, _) = call.kind {
         let symbol = &*path.ident.as_str();
-        if match_trait_method(cx, call, &paths::IO_READ) && symbol == "read" {
-            span_lint(
+        let read_trait = match_trait_method(cx, call, &paths::IO_READ);
+        let write_trait = match_trait_method(cx, call, &paths::IO_WRITE);
+
+        match (read_trait, write_trait, symbol) {
+            (true, _, "read") => span_lint(
                 cx,
                 UNUSED_IO_AMOUNT,
                 expr.span,
-                "handle read amount returned or use `Read::read_exact` instead",
-            );
-        } else if match_trait_method(cx, call, &paths::IO_WRITE) && symbol == "write" {
-            span_lint(
+                "read amount is not handled. Use `Read::read_exact` instead",
+            ),
+            (true, _, "read_vectored") => span_lint(cx, UNUSED_IO_AMOUNT, expr.span, "read amount is not handled"),
+            (_, true, "write") => span_lint(
                 cx,
                 UNUSED_IO_AMOUNT,
                 expr.span,
-                "handle written amount returned or use `Write::write_all` instead",
-            );
+                "written amount is not handled. Use `Write::write_all` instead",
+            ),
+            (_, true, "write_vectored") => span_lint(cx, UNUSED_IO_AMOUNT, expr.span, "written amount is not handled"),
+            _ => (),
         }
     }
 }