]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/slow_vector_initialization.rs
Auto merge of #4551 - mikerite:fix-ice-reporting, r=llogiq
[rust.git] / clippy_lints / src / slow_vector_initialization.rs
index b8ab32491a3bcd269b0fbf223649e4661a0dd17c..7e543bfe60f7239cd9ae184350b6506368a9d9aa 100644 (file)
@@ -4,44 +4,35 @@
 use rustc::hir::intravisit::{walk_block, walk_expr, walk_stmt, NestedVisitorMap, Visitor};
 use rustc::hir::*;
 use rustc::lint::{LateContext, LateLintPass, Lint, LintArray, LintPass};
-use rustc::{declare_tool_lint, lint_array};
+use rustc::{declare_lint_pass, declare_tool_lint};
 use rustc_errors::Applicability;
-use syntax::ast::{LitKind, NodeId};
+use syntax::ast::LitKind;
 use syntax_pos::symbol::Symbol;
 
-/// **What it does:** Checks slow zero-filled vector initialization
-///
-/// **Why is this bad?** These structures are non-idiomatic and less efficient than simply using
-/// `vec![0; len]`.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-/// ```rust
-/// let mut vec1 = Vec::with_capacity(len);
-/// vec1.resize(len, 0);
-///
-/// let mut vec2 = Vec::with_capacity(len);
-/// vec2.extend(repeat(0).take(len))
-/// ```
 declare_clippy_lint! {
+    /// **What it does:** Checks slow zero-filled vector initialization
+    ///
+    /// **Why is this bad?** These structures are non-idiomatic and less efficient than simply using
+    /// `vec![0; len]`.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    /// ```rust
+    /// # use core::iter::repeat;
+    /// # let len = 4;
+    /// let mut vec1 = Vec::with_capacity(len);
+    /// vec1.resize(len, 0);
+    ///
+    /// let mut vec2 = Vec::with_capacity(len);
+    /// vec2.extend(repeat(0).take(len))
+    /// ```
     pub SLOW_VECTOR_INITIALIZATION,
     perf,
     "slow vector initialization"
 }
 
-#[derive(Copy, Clone, Default)]
-pub struct Pass;
-
-impl LintPass for Pass {
-    fn get_lints(&self) -> LintArray {
-        lint_array!(SLOW_VECTOR_INITIALIZATION,)
-    }
-
-    fn name(&self) -> &'static str {
-        "SlowVectorInit"
-    }
-}
+declare_lint_pass!(SlowVectorInit => [SLOW_VECTOR_INITIALIZATION]);
 
 /// `VecAllocation` contains data regarding a vector allocated with `with_capacity` and then
 /// assigned to a variable. For example, `let mut vec = Vec::with_capacity(0)` or
@@ -67,7 +58,7 @@ enum InitializationType<'tcx> {
     Resize(&'tcx Expr),
 }
 
-impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SlowVectorInit {
     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
         // Matches initialization on reassignements. For example: `vec = Vec::with_capacity(100)`
         if_chain! {
@@ -87,7 +78,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
                     len_expr: len_arg,
                 };
 
-                Self::search_initialization(cx, vi, expr.id);
+                Self::search_initialization(cx, vi, expr.hir_id);
             }
         }
     }
@@ -107,13 +98,13 @@ fn check_stmt(&mut self, cx: &LateContext<'a, 'tcx>, stmt: &'tcx Stmt) {
                     len_expr: len_arg,
                 };
 
-                Self::search_initialization(cx, vi, stmt.id);
+                Self::search_initialization(cx, vi, stmt.hir_id);
             }
         }
     }
 }
 
-impl Pass {
+impl SlowVectorInit {
     /// Checks if the given expression is `Vec::with_capacity(..)`. It will return the expression
     /// of the first argument of `with_capacity` call if it matches or `None` if it does not.
     fn is_vec_with_capacity(expr: &Expr) -> Option<&Expr> {
@@ -132,7 +123,7 @@ fn is_vec_with_capacity(expr: &Expr) -> Option<&Expr> {
     }
 
     /// Search initialization for the given vector
-    fn search_initialization<'tcx>(cx: &LateContext<'_, 'tcx>, vec_alloc: VecAllocation<'tcx>, parent_node: NodeId) {
+    fn search_initialization<'tcx>(cx: &LateContext<'_, 'tcx>, vec_alloc: VecAllocation<'tcx>, parent_node: HirId) {
         let enclosing_body = get_enclosing_block(cx, parent_node);
 
         if enclosing_body.is_none() {
@@ -191,16 +182,16 @@ fn emit_lint<'tcx>(
 
 /// `VectorInitializationVisitor` searches for unsafe or slow vector initializations for the given
 /// vector.
-struct VectorInitializationVisitor<'a, 'tcx: 'a> {
+struct VectorInitializationVisitor<'a, 'tcx> {
     cx: &'a LateContext<'a, 'tcx>,
 
-    /// Contains the information
+    /// Contains the information.
     vec_alloc: VecAllocation<'tcx>,
 
-    /// Contains, if found, the slow initialization expression
+    /// Contains the slow initialization expression, if one was found.
     slow_expression: Option<InitializationType<'tcx>>,
 
-    /// true if the initialization of the vector has been found on the visited block
+    /// `true` if the initialization of the vector has been found on the visited block.
     initialization_found: bool,
 }
 
@@ -211,8 +202,8 @@ fn search_slow_extend_filling(&mut self, expr: &'tcx Expr) {
             if self.initialization_found;
             if let ExprKind::MethodCall(ref path, _, ref args) = expr.node;
             if let ExprKind::Path(ref qpath_subj) = args[0].node;
-            if match_qpath(&qpath_subj, &[&self.vec_alloc.variable_name.to_string()]);
-            if path.ident.name == "extend";
+            if match_qpath(&qpath_subj, &[&*self.vec_alloc.variable_name.as_str()]);
+            if path.ident.name == sym!(extend);
             if let Some(ref extend_arg) = args.get(1);
             if self.is_repeat_take(extend_arg);
 
@@ -228,8 +219,8 @@ fn search_slow_resize_filling(&mut self, expr: &'tcx Expr) {
             if self.initialization_found;
             if let ExprKind::MethodCall(ref path, _, ref args) = expr.node;
             if let ExprKind::Path(ref qpath_subj) = args[0].node;
-            if match_qpath(&qpath_subj, &[&self.vec_alloc.variable_name.to_string()]);
-            if path.ident.name == "resize";
+            if match_qpath(&qpath_subj, &[&*self.vec_alloc.variable_name.as_str()]);
+            if path.ident.name == sym!(resize);
             if let (Some(ref len_arg), Some(fill_arg)) = (args.get(1), args.get(2));
 
             // Check that is filled with 0
@@ -249,7 +240,7 @@ fn search_slow_resize_filling(&mut self, expr: &'tcx Expr) {
     fn is_repeat_take(&self, expr: &Expr) -> bool {
         if_chain! {
             if let ExprKind::MethodCall(ref take_path, _, ref take_args) = expr.node;
-            if take_path.ident.name == "take";
+            if take_path.ident.name == sym!(take);
 
             // Check that take is applied to `repeat(0)`
             if let Some(ref repeat_expr) = take_args.get(0);
@@ -317,7 +308,7 @@ fn visit_block(&mut self, block: &'tcx Block) {
 
     fn visit_expr(&mut self, expr: &'tcx Expr) {
         // Skip all the expressions previous to the vector initialization
-        if self.vec_alloc.allocation_expr.id == expr.id {
+        if self.vec_alloc.allocation_expr.hir_id == expr.hir_id {
             self.initialization_found = true;
         }