]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/utils/author.rs
Change Hash{Map, Set} to FxHash{Map, Set}
[rust.git] / clippy_lints / src / utils / author.rs
index e10fa60a38b31b552e4ffcf7dddb11a5432fa068..541d5353daf03f86fdc321c01bb0f8c87c831b7c 100644 (file)
@@ -1,14 +1,15 @@
 //! A group of attributes that can be attached to Rust code in order
 //! to generate a clippy lint detecting said code automatically.
 
-#![allow(print_stdout, use_debug)]
+#![allow(clippy::print_stdout, clippy::use_debug)]
 
-use rustc::lint::*;
+use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
+use rustc::{declare_tool_lint, lint_array};
 use rustc::hir;
-use rustc::hir::{Expr, Expr_, QPath, Ty_, Pat, PatKind, BindingAnnotation, StmtSemi, StmtExpr, StmtDecl, Decl_, Stmt};
+use rustc::hir::{Expr, ExprKind, QPath, TyKind, Pat, PatKind, BindingAnnotation, StmtKind, DeclKind, Stmt};
 use rustc::hir::intravisit::{NestedVisitorMap, Visitor};
+use rustc_data_structures::fx::FxHashMap;
 use syntax::ast::{Attribute, LitKind, DUMMY_NODE_ID};
-use std::collections::HashMap;
 use crate::utils::get_attr;
 
 /// **What it does:** Generates clippy code that detects the offending pattern
 /// ```rust
 /// // ./tests/ui/new_lint.stdout
 /// if_chain!{
-///     if let Expr_::ExprIf(ref cond, ref then, None) = item.node,
-///     if let Expr_::ExprBinary(BinOp::Eq, ref left, ref right) = cond.node,
-///     if let Expr_::ExprPath(ref path) = left.node,
-///     if let Expr_::ExprLit(ref lit) = right.node,
+///     if let ExprKind::If(ref cond, ref then, None) = item.node,
+///     if let ExprKind::Binary(BinOp::Eq, ref left, ref right) = cond.node,
+///     if let ExprKind::Path(ref path) = left.node,
+///     if let ExprKind::Lit(ref lit) = right.node,
 ///     if let LitKind::Int(42, _) = lit.node,
 ///     then {
 ///         // report your lint here
@@ -153,7 +154,7 @@ fn check_foreign_item(&mut self, _cx: &LateContext<'a, 'tcx>, item: &'tcx hir::F
 impl PrintVisitor {
     fn new(s: &'static str) -> Self {
         Self {
-            ids: HashMap::new(),
+            ids: FxHashMap::default(),
             current: s.to_owned(),
         }
     }
@@ -185,23 +186,23 @@ fn print_qpath(&mut self, path: &QPath) {
 struct PrintVisitor {
     /// Fields are the current index that needs to be appended to pattern
     /// binding names
-    ids: HashMap<&'static str, usize>,
+    ids: FxHashMap<&'static str, usize>,
     /// the name that needs to be destructured
     current: String,
 }
 
 impl<'tcx> Visitor<'tcx> for PrintVisitor {
     fn visit_expr(&mut self, expr: &Expr) {
-        print!("    if let Expr_::Expr");
+        print!("    if let ExprKind::");
         let current = format!("{}.node", self.current);
         match expr.node {
-            Expr_::ExprBox(ref inner) => {
+            ExprKind::Box(ref inner) => {
                 let inner_pat = self.next("inner");
                 println!("Box(ref {}) = {};", inner_pat, current);
                 self.current = inner_pat;
                 self.visit_expr(inner);
             },
-            Expr_::ExprArray(ref elements) => {
+            ExprKind::Array(ref elements) => {
                 let elements_pat = self.next("elements");
                 println!("Array(ref {}) = {};", elements_pat, current);
                 println!("    if {}.len() == {};", elements_pat, elements.len());
@@ -210,7 +211,7 @@ fn visit_expr(&mut self, expr: &Expr) {
                     self.visit_expr(element);
                 }
             },
-            Expr_::ExprCall(ref func, ref args) => {
+            ExprKind::Call(ref func, ref args) => {
                 let func_pat = self.next("func");
                 let args_pat = self.next("args");
                 println!("Call(ref {}, ref {}) = {};", func_pat, args_pat, current);
@@ -222,11 +223,11 @@ fn visit_expr(&mut self, expr: &Expr) {
                     self.visit_expr(arg);
                 }
             },
-            Expr_::ExprMethodCall(ref _method_name, ref _generics, ref _args) => {
+            ExprKind::MethodCall(ref _method_name, ref _generics, ref _args) => {
                 println!("MethodCall(ref method_name, ref generics, ref args) = {};", current);
-                println!("    // unimplemented: `ExprMethodCall` is not further destructured at the moment");
+                println!("    // unimplemented: `ExprKind::MethodCall` is not further destructured at the moment");
             },
-            Expr_::ExprTup(ref elements) => {
+            ExprKind::Tup(ref elements) => {
                 let elements_pat = self.next("elements");
                 println!("Tup(ref {}) = {};", elements_pat, current);
                 println!("    if {}.len() == {};", elements_pat, elements.len());
@@ -235,24 +236,24 @@ fn visit_expr(&mut self, expr: &Expr) {
                     self.visit_expr(element);
                 }
             },
-            Expr_::ExprBinary(ref op, ref left, ref right) => {
+            ExprKind::Binary(ref op, ref left, ref right) => {
                 let op_pat = self.next("op");
                 let left_pat = self.next("left");
                 let right_pat = self.next("right");
                 println!("Binary(ref {}, ref {}, ref {}) = {};", op_pat, left_pat, right_pat, current);
-                println!("    if BinOp_::{:?} == {}.node;", op.node, op_pat);
+                println!("    if BinOpKind::{:?} == {}.node;", op.node, op_pat);
                 self.current = left_pat;
                 self.visit_expr(left);
                 self.current = right_pat;
                 self.visit_expr(right);
             },
-            Expr_::ExprUnary(ref op, ref inner) => {
+            ExprKind::Unary(ref op, ref inner) => {
                 let inner_pat = self.next("inner");
                 println!("Unary(UnOp::{:?}, ref {}) = {};", op, inner_pat, current);
                 self.current = inner_pat;
                 self.visit_expr(inner);
             },
-            Expr_::ExprLit(ref lit) => {
+            ExprKind::Lit(ref lit) => {
                 let lit_pat = self.next("lit");
                 println!("Lit(ref {}) = {};", lit_pat, current);
                 match lit.node {
@@ -277,27 +278,27 @@ fn visit_expr(&mut self, expr: &Expr) {
                     },
                 }
             },
-            Expr_::ExprCast(ref expr, ref ty) => {
+            ExprKind::Cast(ref expr, ref ty) => {
                 let cast_pat = self.next("expr");
                 let cast_ty = self.next("cast_ty");
                 let qp_label = self.next("qp");
 
                 println!("Cast(ref {}, ref {}) = {};", cast_pat, cast_ty, current);
-                if let Ty_::TyPath(ref qp) = ty.node {
-                    println!("    if let Ty_::TyPath(ref {}) = {}.node;", qp_label, cast_ty);
+                if let TyKind::Path(ref qp) = ty.node {
+                    println!("    if let TyKind::Path(ref {}) = {}.node;", qp_label, cast_ty);
                     self.current = qp_label;
                     self.print_qpath(qp);
                 }
                 self.current = cast_pat;
                 self.visit_expr(expr);
             },
-            Expr_::ExprType(ref expr, ref _ty) => {
+            ExprKind::Type(ref expr, ref _ty) => {
                 let cast_pat = self.next("expr");
                 println!("Type(ref {}, _) = {};", cast_pat, current);
                 self.current = cast_pat;
                 self.visit_expr(expr);
             },
-            Expr_::ExprIf(ref cond, ref then, ref opt_else) => {
+            ExprKind::If(ref cond, ref then, ref opt_else) => {
                 let cond_pat = self.next("cond");
                 let then_pat = self.next("then");
                 if let Some(ref else_) = *opt_else {
@@ -313,7 +314,7 @@ fn visit_expr(&mut self, expr: &Expr) {
                 self.current = then_pat;
                 self.visit_expr(then);
             },
-            Expr_::ExprWhile(ref cond, ref body, _) => {
+            ExprKind::While(ref cond, ref body, _) => {
                 let cond_pat = self.next("cond");
                 let body_pat = self.next("body");
                 let label_pat = self.next("label");
@@ -323,7 +324,7 @@ fn visit_expr(&mut self, expr: &Expr) {
                 self.current = body_pat;
                 self.visit_block(body);
             },
-            Expr_::ExprLoop(ref body, _, desugaring) => {
+            ExprKind::Loop(ref body, _, desugaring) => {
                 let body_pat = self.next("body");
                 let des = loop_desugaring_name(desugaring);
                 let label_pat = self.next("label");
@@ -331,7 +332,7 @@ fn visit_expr(&mut self, expr: &Expr) {
                 self.current = body_pat;
                 self.visit_block(body);
             },
-            Expr_::ExprMatch(ref expr, ref arms, desugaring) => {
+            ExprKind::Match(ref expr, ref arms, desugaring) => {
                 let des = desugaring_name(desugaring);
                 let expr_pat = self.next("expr");
                 let arms_pat = self.next("arms");
@@ -344,9 +345,15 @@ fn visit_expr(&mut self, expr: &Expr) {
                     self.visit_expr(&arm.body);
                     if let Some(ref guard) = arm.guard {
                         let guard_pat = self.next("guard");
-                        println!("    if let Some(ref {}) = {}[{}].guard", guard_pat, arms_pat, i);
-                        self.current = guard_pat;
-                        self.visit_expr(guard);
+                        println!("    if let Some(ref {}) = {}[{}].guard;", guard_pat, arms_pat, i);
+                        match guard {
+                            hir::Guard::If(ref if_expr) => {
+                                let if_expr_pat = self.next("expr");
+                                println!("    if let Guard::If(ref {}) = {};", if_expr_pat, guard_pat);
+                                self.current = if_expr_pat;
+                                self.visit_expr(if_expr);
+                            }
+                        }
                     }
                     println!("    if {}[{}].pats.len() == {};", arms_pat, i, arm.pats.len());
                     for (j, pat) in arm.pats.iter().enumerate() {
@@ -355,23 +362,23 @@ fn visit_expr(&mut self, expr: &Expr) {
                     }
                 }
             },
-            Expr_::ExprClosure(ref _capture_clause, ref _func, _, _, _) => {
+            ExprKind::Closure(ref _capture_clause, ref _func, _, _, _) => {
                 println!("Closure(ref capture_clause, ref func, _, _, _) = {};", current);
-                println!("    // unimplemented: `ExprClosure` is not further destructured at the moment");
+                println!("    // unimplemented: `ExprKind::Closure` is not further destructured at the moment");
             },
-            Expr_::ExprYield(ref sub) => {
+            ExprKind::Yield(ref sub) => {
                 let sub_pat = self.next("sub");
                 println!("Yield(ref sub) = {};", current);
                 self.current = sub_pat;
                 self.visit_expr(sub);
             },
-            Expr_::ExprBlock(ref block, _) => {
+            ExprKind::Block(ref block, _) => {
                 let block_pat = self.next("block");
                 println!("Block(ref {}) = {};", block_pat, current);
                 self.current = block_pat;
                 self.visit_block(block);
             },
-            Expr_::ExprAssign(ref target, ref value) => {
+            ExprKind::Assign(ref target, ref value) => {
                 let target_pat = self.next("target");
                 let value_pat = self.next("value");
                 println!("Assign(ref {}, ref {}) = {};", target_pat, value_pat, current);
@@ -380,26 +387,26 @@ fn visit_expr(&mut self, expr: &Expr) {
                 self.current = value_pat;
                 self.visit_expr(value);
             },
-            Expr_::ExprAssignOp(ref op, ref target, ref value) => {
+            ExprKind::AssignOp(ref op, ref target, ref value) => {
                 let op_pat = self.next("op");
                 let target_pat = self.next("target");
                 let value_pat = self.next("value");
                 println!("AssignOp(ref {}, ref {}, ref {}) = {};", op_pat, target_pat, value_pat, current);
-                println!("    if BinOp_::{:?} == {}.node;", op.node, op_pat);
+                println!("    if BinOpKind::{:?} == {}.node;", op.node, op_pat);
                 self.current = target_pat;
                 self.visit_expr(target);
                 self.current = value_pat;
                 self.visit_expr(value);
             },
-            Expr_::ExprField(ref object, ref field_ident) => {
+            ExprKind::Field(ref object, ref field_ident) => {
                 let obj_pat = self.next("object");
                 let field_name_pat = self.next("field_name");
                 println!("Field(ref {}, ref {}) = {};", obj_pat, field_name_pat, current);
-                println!("    if {}.node.as_str() == {:?}", field_name_pat, field_ident.name.as_str());
+                println!("    if {}.node.as_str() == {:?}", field_name_pat, field_ident.as_str());
                 self.current = obj_pat;
                 self.visit_expr(object);
             },
-            Expr_::ExprIndex(ref object, ref index) => {
+            ExprKind::Index(ref object, ref index) => {
                 let object_pat = self.next("object");
                 let index_pat = self.next("index");
                 println!("Index(ref {}, ref {}) = {};", object_pat, index_pat, current);
@@ -408,19 +415,19 @@ fn visit_expr(&mut self, expr: &Expr) {
                 self.current = index_pat;
                 self.visit_expr(index);
             },
-            Expr_::ExprPath(ref path) => {
+            ExprKind::Path(ref path) => {
                 let path_pat = self.next("path");
                 println!("Path(ref {}) = {};", path_pat, current);
                 self.current = path_pat;
                 self.print_qpath(path);
             },
-            Expr_::ExprAddrOf(mutability, ref inner) => {
+            ExprKind::AddrOf(mutability, ref inner) => {
                 let inner_pat = self.next("inner");
                 println!("AddrOf({:?}, ref {}) = {};", mutability, inner_pat, current);
                 self.current = inner_pat;
                 self.visit_expr(inner);
             },
-            Expr_::ExprBreak(ref _destination, ref opt_value) => {
+            ExprKind::Break(ref _destination, ref opt_value) => {
                 let destination_pat = self.next("destination");
                 if let Some(ref value) = *opt_value {
                     let value_pat = self.next("value");
@@ -432,12 +439,12 @@ fn visit_expr(&mut self, expr: &Expr) {
                 }
                 // FIXME: implement label printing
             },
-            Expr_::ExprAgain(ref _destination) => {
+            ExprKind::Continue(ref _destination) => {
                 let destination_pat = self.next("destination");
                 println!("Again(ref {}) = {};", destination_pat, current);
                 // FIXME: implement label printing
             },
-            Expr_::ExprRet(ref opt_value) => if let Some(ref value) = *opt_value {
+            ExprKind::Ret(ref opt_value) => if let Some(ref value) = *opt_value {
                 let value_pat = self.next("value");
                 println!("Ret(Some(ref {})) = {};", value_pat, current);
                 self.current = value_pat;
@@ -445,11 +452,11 @@ fn visit_expr(&mut self, expr: &Expr) {
             } else {
                 println!("Ret(None) = {};", current);
             },
-            Expr_::ExprInlineAsm(_, ref _input, ref _output) => {
+            ExprKind::InlineAsm(_, ref _input, ref _output) => {
                 println!("InlineAsm(_, ref input, ref output) = {};", current);
-                println!("    // unimplemented: `ExprInlineAsm` is not further destructured at the moment");
+                println!("    // unimplemented: `ExprKind::InlineAsm` is not further destructured at the moment");
             },
-            Expr_::ExprStruct(ref path, ref fields, ref opt_base) => {
+            ExprKind::Struct(ref path, ref fields, ref opt_base) => {
                 let path_pat = self.next("path");
                 let fields_pat = self.next("fields");
                 if let Some(ref base) = *opt_base {
@@ -472,7 +479,7 @@ fn visit_expr(&mut self, expr: &Expr) {
                 println!("    // unimplemented: field checks");
             },
             // FIXME: compute length (needs type info)
-            Expr_::ExprRepeat(ref value, _) => {
+            ExprKind::Repeat(ref value, _) => {
                 let value_pat = self.next("value");
                 println!("Repeat(ref {}, _) = {};", value_pat, current);
                 println!("// unimplemented: repeat count check");
@@ -487,7 +494,7 @@ fn visit_pat(&mut self, pat: &Pat) {
         let current = format!("{}.node", self.current);
         match pat.node {
             PatKind::Wild => println!("Wild = {};", current),
-            PatKind::Binding(anno, _, name, ref sub) => {
+            PatKind::Binding(anno, _, ident, ref sub) => {
                 let anno_pat = match anno {
                     BindingAnnotation::Unannotated => "BindingAnnotation::Unannotated",
                     BindingAnnotation::Mutable => "BindingAnnotation::Mutable",
@@ -503,7 +510,7 @@ fn visit_pat(&mut self, pat: &Pat) {
                 } else {
                     println!("Binding({}, _, {}, None) = {};", anno_pat, name_pat, current);
                 }
-                println!("    if {}.node.as_str() == \"{}\";", name_pat, name.node.as_str());
+                println!("    if {}.node.as_str() == \"{}\";", name_pat, ident.as_str());
             }
             PatKind::Struct(ref path, ref fields, ignore) => {
                 let path_pat = self.next("path");
@@ -588,20 +595,20 @@ fn visit_pat(&mut self, pat: &Pat) {
     }
 
     fn visit_stmt(&mut self, s: &Stmt) {
-        print!("    if let Stmt_::");
+        print!("    if let StmtKind::");
         let current = format!("{}.node", self.current);
         match s.node {
             // Could be an item or a local (let) binding:
-            StmtDecl(ref decl, _) => {
+            StmtKind::Decl(ref decl, _) => {
                 let decl_pat = self.next("decl");
-                println!("StmtDecl(ref {}, _) = {}", decl_pat, current);
-                print!("    if let Decl_::");
+                println!("Decl(ref {}, _) = {}", decl_pat, current);
+                print!("    if let DeclKind::");
                 let current = format!("{}.node", decl_pat);
                 match decl.node {
                     // A local (let) binding:
-                    Decl_::DeclLocal(ref local) => {
+                    DeclKind::Local(ref local) => {
                         let local_pat = self.next("local");
-                        println!("DeclLocal(ref {}) = {};", local_pat, current);
+                        println!("Local(ref {}) = {};", local_pat, current);
                         if let Some(ref init) = local.init {
                             let init_pat = self.next("init");
                             println!("    if let Some(ref {}) = {}.init", init_pat, local_pat);
@@ -612,24 +619,24 @@ fn visit_stmt(&mut self, s: &Stmt) {
                         self.visit_pat(&local.pat);
                     },
                     // An item binding:
-                    Decl_::DeclItem(_) => {
-                        println!("DeclItem(item_id) = {};", current);
+                    DeclKind::Item(_) => {
+                        println!("Item(item_id) = {};", current);
                     },
                 }
             }
 
             // Expr without trailing semi-colon (must have unit type):
-            StmtExpr(ref e, _) => {
+            StmtKind::Expr(ref e, _) => {
                 let e_pat = self.next("e");
-                println!("StmtExpr(ref {}, _) = {}", e_pat, current);
+                println!("Expr(ref {}, _) = {}", e_pat, current);
                 self.current = e_pat;
                 self.visit_expr(e);
             },
 
             // Expr with trailing semi-colon (may have any type):
-            StmtSemi(ref e, _) => {
+            StmtKind::Semi(ref e, _) => {
                 let e_pat = self.next("e");
-                println!("StmtSemi(ref {}, _) = {}", e_pat, current);
+                println!("Semi(ref {}, _) = {}", e_pat, current);
                 self.current = e_pat;
                 self.visit_expr(e);
             },
@@ -671,17 +678,17 @@ fn print_path(path: &QPath, first: &mut bool) {
             } else {
                 print!(", ");
             }
-            print!("{:?}", segment.name.as_str());
+            print!("{:?}", segment.ident.as_str());
         },
         QPath::TypeRelative(ref ty, ref segment) => match ty.node {
-            hir::Ty_::TyPath(ref inner_path) => {
+            hir::TyKind::Path(ref inner_path) => {
                 print_path(inner_path, first);
                 if *first {
                     *first = false;
                 } else {
                     print!(", ");
                 }
-                print!("{:?}", segment.name.as_str());
+                print!("{:?}", segment.ident.as_str());
             },
             ref other => print!("/* unimplemented: {:?}*/", other),
         },