]> git.lizzy.rs Git - rust.git/blobdiff - 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
index fc50e668c85d4ca8863afc12c74aaa1fdab91dc9..27deb0d99459c7f9eff623547714170fa6a4cf2e 100644 (file)
@@ -1,12 +1,16 @@
-use rustc::lint::*;
+use crate::utils::{is_try, match_qpath, match_trait_method, paths, span_lint};
 use rustc::hir;
-use utils::{span_lint, match_path, match_trait_method, is_try, paths};
+use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
+use rustc::{declare_tool_lint, lint_array};
 
 /// **What it does:** Checks for unused written/read amount.
 ///
-/// **Why is this bad?** `io::Write::write` and `io::Read::read` 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 partial-write/read, use
+/// **Why is this bad?** `io::Write::write` and `io::Read::read` 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
+/// partial-write/read, use
 /// `write_all`/`read_exact` instead.
 ///
 /// **Known problems:** Detects only common patterns.
@@ -20,9 +24,9 @@
 ///     Ok(())
 /// }
 /// ```
-declare_lint! {
+declare_clippy_lint! {
     pub UNUSED_IO_AMOUNT,
-    Deny,
+    correctness,
     "unused written/read amount"
 }
 
@@ -35,18 +39,17 @@ fn get_lints(&self) -> LintArray {
 }
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedIoAmount {
-    fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) {
+    fn check_stmt(&mut self, cx: &LateContext<'_, '_>, s: &hir::Stmt) {
         let expr = match s.node {
-            hir::StmtSemi(ref expr, _) |
-            hir::StmtExpr(ref expr, _) => &**expr,
+            hir::StmtKind::Semi(ref expr) | hir::StmtKind::Expr(ref expr) => &**expr,
             _ => return,
         };
 
         match expr.node {
-            hir::ExprMatch(ref res, _, _) if is_try(expr).is_some() => {
-                if let hir::ExprCall(ref func, ref args) = res.node {
-                    if let hir::ExprPath(ref path) = func.node {
-                        if match_path(path, &paths::CARRIER_TRANSLATE) && args.len() == 1 {
+            hir::ExprKind::Match(ref res, _, _) if is_try(expr).is_some() => {
+                if let hir::ExprKind::Call(ref func, ref args) = res.node {
+                    if let hir::ExprKind::Path(ref path) = func.node {
+                        if match_qpath(path, &paths::TRY_INTO_RESULT) && args.len() == 1 {
                             check_method_call(cx, &args[0], expr);
                         }
                     }
@@ -55,14 +58,11 @@ fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) {
                 }
             },
 
-            hir::ExprMethodCall(ref symbol, _, ref args) => {
-                let symbol = &*symbol.node.as_str();
-                match symbol {
-                    "expect" | "unwrap" | "unwrap_or" | "unwrap_or_else" => {
-                        check_method_call(cx, &args[0], expr);
-                    },
-                    _ => (),
-                }
+            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);
+                },
+                _ => (),
             },
 
             _ => (),
@@ -70,19 +70,23 @@ 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::ExprMethodCall(ref symbol, _, _) = call.node {
-        let symbol = &*symbol.node.as_str();
+fn check_method_call(cx: &LateContext<'_, '_>, call: &hir::Expr, expr: &hir::Expr) {
+    if let hir::ExprKind::MethodCall(ref path, _, _) = call.node {
+        let symbol = &*path.ident.as_str();
         if match_trait_method(cx, call, &paths::IO_READ) && symbol == "read" {
-            span_lint(cx,
-                      UNUSED_IO_AMOUNT,
-                      expr.span,
-                      "handle read amount returned or use `Read::read_exact` instead");
+            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(cx,
-                      UNUSED_IO_AMOUNT,
-                      expr.span,
-                      "handle written amount returned or use `Write::write_all` instead");
+            span_lint(
+                cx,
+                UNUSED_IO_AMOUNT,
+                expr.span,
+                "handle written amount returned or use `Write::write_all` instead",
+            );
         }
     }
 }