]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/escape.rs
Auto merge of #3680 - g-bartoszek:needless-bool-else-if-brackets, r=oli-obk
[rust.git] / clippy_lints / src / escape.rs
index ebbc2c34811b51e34c68697174733f7556da5c0b..a7b47fd1e5463f32ddcc00cb3d98a9e53c1064e2 100644 (file)
@@ -1,16 +1,15 @@
-use rustc::hir::*;
+use crate::utils::span_lint;
 use rustc::hir::intravisit as visit;
-use rustc::hir::map::Node::{NodeExpr, NodeStmt};
-use rustc::lint::*;
-use rustc::{declare_lint, lint_array};
+use rustc::hir::*;
+use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
 use rustc::middle::expr_use_visitor::*;
 use rustc::middle::mem_categorization::{cmt_, Categorization};
-use rustc::ty::{self, Ty};
 use rustc::ty::layout::LayoutOf;
+use rustc::ty::{self, Ty};
 use rustc::util::nodemap::NodeSet;
+use rustc::{declare_tool_lint, lint_array};
 use syntax::ast::NodeId;
-use syntax::codemap::Span;
-use crate::utils::span_lint;
+use syntax::source_map::Span;
 
 pub struct Pass {
     pub too_large_for_stack: u64,
@@ -65,13 +64,23 @@ fn check_fn(
         _: Span,
         node_id: NodeId,
     ) {
-        let fn_def_id = cx.tcx.hir.local_def_id(node_id);
+        // If the method is an impl for a trait, don't warn
+        let parent_id = cx.tcx.hir().get_parent(node_id);
+        let parent_node = cx.tcx.hir().find(parent_id);
+
+        if let Some(Node::Item(item)) = parent_node {
+            if let ItemKind::Impl(_, _, _, _, Some(..), _, _) = item.node {
+                return;
+            }
+        }
+
         let mut v = EscapeDelegate {
             cx,
-            set: NodeSet(),
+            set: NodeSet::default(),
             too_large_for_stack: self.too_large_for_stack,
         };
 
+        let fn_def_id = cx.tcx.hir().local_def_id(node_id);
         let region_scope_tree = &cx.tcx.region_scope_tree(fn_def_id);
         ExprUseVisitor::new(&mut v, cx.tcx, cx.param_env, region_scope_tree, cx.tables, None).consume_body(body);
 
@@ -79,7 +88,7 @@ fn check_fn(
             span_lint(
                 cx,
                 BOXED_LOCAL,
-                cx.tcx.hir.span(node),
+                cx.tcx.hir().span(node),
                 "local variable doesn't need to be boxed here",
             );
         }
@@ -97,10 +106,10 @@ fn consume(&mut self, _: NodeId, _: Span, cmt: &cmt_<'tcx>, mode: ConsumeMode) {
     }
     fn matched_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: MatchMode) {}
     fn consume_pat(&mut self, consume_pat: &Pat, cmt: &cmt_<'tcx>, _: ConsumeMode) {
-        let map = &self.cx.tcx.hir;
+        let map = &self.cx.tcx.hir();
         if map.is_argument(consume_pat.id) {
             // Skip closure arguments
-            if let Some(NodeExpr(..)) = map.find(map.get_parent_node(consume_pat.id)) {
+            if let Some(Node::Expr(..)) = map.find(map.get_parent_node(consume_pat.id)) {
                 return;
             }
             if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
@@ -110,19 +119,17 @@ fn consume_pat(&mut self, consume_pat: &Pat, cmt: &cmt_<'tcx>, _: ConsumeMode) {
         }
         if let Categorization::Rvalue(..) = cmt.cat {
             let id = map.hir_to_node_id(cmt.hir_id);
-            if let Some(NodeStmt(st)) = map.find(map.get_parent_node(id)) {
-                if let StmtKind::Decl(ref decl, _) = st.node {
-                    if let DeclKind::Local(ref loc) = decl.node {
-                        if let Some(ref ex) = loc.init {
-                            if let ExprKind::Box(..) = ex.node {
-                                if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
-                                    // let x = box (...)
-                                    self.set.insert(consume_pat.id);
-                                }
-                                // TODO Box::new
-                                // TODO vec![]
-                                // TODO "foo".to_owned() and friends
+            if let Some(Node::Stmt(st)) = map.find(map.get_parent_node(id)) {
+                if let StmtKind::Local(ref loc) = st.node {
+                    if let Some(ref ex) = loc.init {
+                        if let ExprKind::Box(..) = ex.node {
+                            if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
+                                // let x = box (...)
+                                self.set.insert(consume_pat.id);
                             }
+                            // TODO Box::new
+                            // TODO vec![]
+                            // TODO "foo".to_owned() and friends
                         }
                     }
                 }
@@ -137,7 +144,15 @@ fn consume_pat(&mut self, consume_pat: &Pat, cmt: &cmt_<'tcx>, _: ConsumeMode) {
             }
         }
     }
-    fn borrow(&mut self, _: NodeId, _: Span, cmt: &cmt_<'tcx>, _: ty::Region<'_>, _: ty::BorrowKind, loan_cause: LoanCause) {
+    fn borrow(
+        &mut self,
+        _: NodeId,
+        _: Span,
+        cmt: &cmt_<'tcx>,
+        _: ty::Region<'_>,
+        _: ty::BorrowKind,
+        loan_cause: LoanCause,
+    ) {
         if let Categorization::Local(lid) = cmt.cat {
             match loan_cause {
                 // x.foo()