]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/utils/hir_utils.rs
Merge pull request #3303 from shssoichiro/3069-unnecessary-fold-pattern-guard
[rust.git] / clippy_lints / src / utils / hir_utils.rs
index 5ff847a53b43da442cab187c7227c3fc7cc7c9a3..7a0b28d15d875f47b53d78caed01d7bb1f5f47f0 100644 (file)
@@ -1,11 +1,21 @@
+// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+
 use crate::consts::{constant_simple, constant_context};
-use rustc::lint::*;
-use rustc::hir::*;
-use rustc::ty::{TypeckTables};
+use crate::rustc::lint::LateContext;
+use crate::rustc::hir::*;
+use crate::rustc::ty::{TypeckTables};
 use std::hash::{Hash, Hasher};
 use std::collections::hash_map::DefaultHasher;
-use syntax::ast::Name;
-use syntax::ptr::P;
+use crate::syntax::ast::Name;
+use crate::syntax::ptr::P;
 use crate::utils::differing_macro_contexts;
 
 /// Type used to check whether two ast are the same. This is different from the
@@ -43,14 +53,14 @@ pub fn ignore_fn(self) -> Self {
     /// Check whether two statements are the same.
     pub fn eq_stmt(&mut self, left: &Stmt, right: &Stmt) -> bool {
         match (&left.node, &right.node) {
-            (&StmtDecl(ref l, _), &StmtDecl(ref r, _)) => {
-                if let (&DeclLocal(ref l), &DeclLocal(ref r)) = (&l.node, &r.node) {
+            (&StmtKind::Decl(ref l, _), &StmtKind::Decl(ref r, _)) => {
+                if let (&DeclKind::Local(ref l), &DeclKind::Local(ref r)) = (&l.node, &r.node) {
                     both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) && both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
                 } else {
                     false
                 }
             },
-            (&StmtExpr(ref l, _), &StmtExpr(ref r, _)) | (&StmtSemi(ref l, _), &StmtSemi(ref r, _)) => {
+            (&StmtKind::Expr(ref l, _), &StmtKind::Expr(ref r, _)) | (&StmtKind::Semi(ref l, _), &StmtKind::Semi(ref r, _)) => {
                 self.eq_expr(l, r)
             },
             _ => false,
@@ -63,6 +73,7 @@ pub fn eq_block(&mut self, left: &Block, right: &Block) -> bool {
             && both(&left.expr, &right.expr, |l, r| self.eq_expr(l, r))
     }
 
+    #[allow(clippy::similar_names)]
     pub fn eq_expr(&mut self, left: &Expr, right: &Expr) -> bool {
         if self.ignore_fn && differing_macro_contexts(left.span, right.span) {
             return false;
@@ -113,7 +124,7 @@ pub fn eq_expr(&mut self, left: &Expr, right: &Expr) -> bool {
             },
             (&ExprKind::Match(ref le, ref la, ref ls), &ExprKind::Match(ref re, ref ra, ref rs)) => {
                 ls == rs && self.eq_expr(le, re) && over(la, ra, |l, r| {
-                    self.eq_expr(&l.body, &r.body) && both(&l.guard, &r.guard, |l, r| self.eq_expr(l, r))
+                    self.eq_expr(&l.body, &r.body) && both(&l.guard, &r.guard, |l, r| self.eq_guard(l, r))
                         && over(&l.pats, &r.pats, |l, r| self.eq_pat(l, r))
                 })
             },
@@ -152,6 +163,12 @@ fn eq_field(&mut self, left: &Field, right: &Field) -> bool {
         left.ident.name == right.ident.name && self.eq_expr(&left.expr, &right.expr)
     }
 
+    fn eq_guard(&mut self, left: &Guard, right: &Guard) -> bool {
+        match (left, right) {
+            (Guard::If(l), Guard::If(r)) => self.eq_expr(l, r),
+        }
+    }
+
     fn eq_generic_arg(&mut self, left: &GenericArg, right: &GenericArg) -> bool {
         match (left, right) {
             (GenericArg::Lifetime(l_lt), GenericArg::Lifetime(r_lt)) => self.eq_lifetime(l_lt, r_lt),
@@ -192,6 +209,7 @@ pub fn eq_pat(&mut self, left: &Pat, right: &Pat) -> bool {
         }
     }
 
+    #[allow(clippy::similar_names)]
     fn eq_qpath(&mut self, left: &QPath, right: &QPath) -> bool {
         match (left, right) {
             (&QPath::Resolved(ref lty, ref lpath), &QPath::Resolved(ref rty, ref rpath)) => {
@@ -246,10 +264,11 @@ pub fn eq_ty(&mut self, left: &Ty, right: &Ty) -> bool {
         self.eq_ty_kind(&left.node, &right.node)
     }
 
-    pub fn eq_ty_kind(&mut self, left: &Ty_, right: &Ty_) -> bool {
+    #[allow(clippy::similar_names)]
+    pub fn eq_ty_kind(&mut self, left: &TyKind, right: &TyKind) -> bool {
         match (left, right) {
-            (&TySlice(ref l_vec), &TySlice(ref r_vec)) => self.eq_ty(l_vec, r_vec),
-            (&TyArray(ref lt, ref ll_id), &TyArray(ref rt, ref rl_id)) => {
+            (&TyKind::Slice(ref l_vec), &TyKind::Slice(ref r_vec)) => self.eq_ty(l_vec, r_vec),
+            (&TyKind::Array(ref lt, ref ll_id), &TyKind::Array(ref rt, ref rl_id)) => {
                 let full_table = self.tables;
 
                 let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(ll_id.body));
@@ -264,13 +283,13 @@ pub fn eq_ty_kind(&mut self, left: &Ty_, right: &Ty_) -> bool {
                 self.tables = full_table;
                 eq_ty && ll == rl
             },
-            (&TyPtr(ref l_mut), &TyPtr(ref r_mut)) => l_mut.mutbl == r_mut.mutbl && self.eq_ty(&*l_mut.ty, &*r_mut.ty),
-            (&TyRptr(_, ref l_rmut), &TyRptr(_, ref r_rmut)) => {
+            (&TyKind::Ptr(ref l_mut), &TyKind::Ptr(ref r_mut)) => l_mut.mutbl == r_mut.mutbl && self.eq_ty(&*l_mut.ty, &*r_mut.ty),
+            (&TyKind::Rptr(_, ref l_rmut), &TyKind::Rptr(_, ref r_rmut)) => {
                 l_rmut.mutbl == r_rmut.mutbl && self.eq_ty(&*l_rmut.ty, &*r_rmut.ty)
             },
-            (&TyPath(ref l), &TyPath(ref r)) => self.eq_qpath(l, r),
-            (&TyTup(ref l), &TyTup(ref r)) => over(l, r, |l, r| self.eq_ty(l, r)),
-            (&TyInfer, &TyInfer) => true,
+            (&TyKind::Path(ref l), &TyKind::Path(ref r)) => self.eq_qpath(l, r),
+            (&TyKind::Tup(ref l), &TyKind::Tup(ref r)) => over(l, r, |l, r| self.eq_ty(l, r)),
+            (&TyKind::Infer, &TyKind::Infer) => true,
             _ => false,
         }
     }
@@ -364,7 +383,7 @@ pub fn hash_block(&mut self, b: &Block) {
         }.hash(&mut self.s);
     }
 
-    #[allow(many_single_char_names)]
+    #[allow(clippy::many_single_char_names)]
     pub fn hash_expr(&mut self, e: &Expr) {
         if let Some(e) = constant_simple(self.cx, self.tables, e) {
             return e.hash(&mut self.s);
@@ -496,7 +515,7 @@ pub fn hash_expr(&mut self, e: &Expr) {
                 for arm in arms {
                     // TODO: arm.pat?
                     if let Some(ref e) = arm.guard {
-                        self.hash_expr(e);
+                        self.hash_guard(e);
                     }
                     self.hash_expr(&arm.body);
                 }
@@ -613,26 +632,36 @@ pub fn hash_path(&mut self, p: &Path) {
 
     pub fn hash_stmt(&mut self, b: &Stmt) {
         match b.node {
-            StmtDecl(ref decl, _) => {
-                let c: fn(_, _) -> _ = StmtDecl;
+            StmtKind::Decl(ref decl, _) => {
+                let c: fn(_, _) -> _ = StmtKind::Decl;
                 c.hash(&mut self.s);
 
-                if let DeclLocal(ref local) = decl.node {
+                if let DeclKind::Local(ref local) = decl.node {
                     if let Some(ref init) = local.init {
                         self.hash_expr(init);
                     }
                 }
             },
-            StmtExpr(ref expr, _) => {
-                let c: fn(_, _) -> _ = StmtExpr;
+            StmtKind::Expr(ref expr, _) => {
+                let c: fn(_, _) -> _ = StmtKind::Expr;
                 c.hash(&mut self.s);
                 self.hash_expr(expr);
             },
-            StmtSemi(ref expr, _) => {
-                let c: fn(_, _) -> _ = StmtSemi;
+            StmtKind::Semi(ref expr, _) => {
+                let c: fn(_, _) -> _ = StmtKind::Semi;
                 c.hash(&mut self.s);
                 self.hash_expr(expr);
             },
         }
     }
+
+    pub fn hash_guard(&mut self, g: &Guard) {
+        match g {
+            Guard::If(ref expr) => {
+                let c: fn(_) -> _ = Guard::If;
+                c.hash(&mut self.s);
+                self.hash_expr(expr);
+            }
+        }
+    }
 }