]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/types.rs
Fix a couple warnings
[rust.git] / clippy_lints / src / types.rs
index 294ca88b70f33870ea2fd2a4cb657eeec2203d77..930c44c88633b8af60fc0931cf5a3de7487e7c61 100644 (file)
@@ -1,6 +1,6 @@
 use reexport::*;
 use rustc::hir::*;
-use rustc::hir::intravisit::{FnKind, Visitor, walk_ty};
+use rustc::hir::intravisit::{FnKind, Visitor, walk_ty, NestedVisitorMap};
 use rustc::lint::*;
 use rustc::ty;
 use std::cmp::Ordering;
@@ -69,8 +69,8 @@ fn get_lints(&self) -> LintArray {
     }
 }
 
-impl LateLintPass for TypePass {
-    fn check_ty(&mut self, cx: &LateContext, ast_ty: &Ty) {
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypePass {
+    fn check_ty(&mut self, cx: &LateContext<'a, 'tcx>, ast_ty: &'tcx Ty) {
         if in_macro(cx, ast_ty.span) {
             return;
         }
@@ -153,8 +153,8 @@ fn get_lints(&self) -> LintArray {
     }
 }
 
-impl LateLintPass for LetPass {
-    fn check_decl(&mut self, cx: &LateContext, decl: &Decl) {
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetPass {
+    fn check_decl(&mut self, cx: &LateContext<'a, 'tcx>, decl: &'tcx Decl) {
         check_let_unit(cx, decl)
     }
 }
@@ -190,8 +190,8 @@ fn get_lints(&self) -> LintArray {
     }
 }
 
-impl LateLintPass for UnitCmp {
-    fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitCmp {
+    fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
         if in_macro(cx, expr.span) {
             return;
         }
@@ -447,8 +447,8 @@ fn get_lints(&self) -> LintArray {
     }
 }
 
-impl LateLintPass for CastPass {
-    fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CastPass {
+    fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
         if let ExprCast(ref ex, _) = expr.node {
             let (cast_from, cast_to) = (cx.tcx.tables().expr_ty(ex), cx.tcx.tables().expr_ty(expr));
             if cast_from.is_numeric() && cast_to.is_numeric() && !in_external_macro(cx, expr.span) {
@@ -535,17 +535,17 @@ fn get_lints(&self) -> LintArray {
     }
 }
 
-impl LateLintPass for TypeComplexityPass {
-    fn check_fn(&mut self, cx: &LateContext, _: FnKind, decl: &FnDecl, _: &Expr, _: Span, _: NodeId) {
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeComplexityPass {
+    fn check_fn(&mut self, cx: &LateContext<'a, 'tcx>, _: FnKind<'tcx>, decl: &'tcx FnDecl, _: &'tcx Expr, _: Span, _: NodeId) {
         self.check_fndecl(cx, decl);
     }
 
-    fn check_struct_field(&mut self, cx: &LateContext, field: &StructField) {
+    fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, field: &'tcx StructField) {
         // enum variants are also struct fields now
         self.check_type(cx, &field.ty);
     }
 
-    fn check_item(&mut self, cx: &LateContext, item: &Item) {
+    fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
         match item.node {
             ItemStatic(ref ty, _, _) |
             ItemConst(ref ty, _) => self.check_type(cx, ty),
@@ -554,7 +554,7 @@ fn check_item(&mut self, cx: &LateContext, item: &Item) {
         }
     }
 
-    fn check_trait_item(&mut self, cx: &LateContext, item: &TraitItem) {
+    fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
         match item.node {
             ConstTraitItem(ref ty, _) |
             TypeTraitItem(_, Some(ref ty)) => self.check_type(cx, ty),
@@ -564,7 +564,7 @@ fn check_trait_item(&mut self, cx: &LateContext, item: &TraitItem) {
         }
     }
 
-    fn check_impl_item(&mut self, cx: &LateContext, item: &ImplItem) {
+    fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem) {
         match item.node {
             ImplItemKind::Const(ref ty, _) |
             ImplItemKind::Type(ref ty) => self.check_type(cx, ty),
@@ -573,15 +573,15 @@ fn check_impl_item(&mut self, cx: &LateContext, item: &ImplItem) {
         }
     }
 
-    fn check_local(&mut self, cx: &LateContext, local: &Local) {
+    fn check_local(&mut self, cx: &LateContext<'a, 'tcx>, local: &'tcx Local) {
         if let Some(ref ty) = local.ty {
             self.check_type(cx, ty);
         }
     }
 }
 
-impl TypeComplexityPass {
-    fn check_fndecl(&self, cx: &LateContext, decl: &FnDecl) {
+impl<'a, 'tcx> TypeComplexityPass {
+    fn check_fndecl(&self, cx: &LateContext<'a, 'tcx>, decl: &'tcx FnDecl) {
         for arg in &decl.inputs {
             self.check_type(cx, &arg.ty);
         }
@@ -590,7 +590,7 @@ fn check_fndecl(&self, cx: &LateContext, decl: &FnDecl) {
         }
     }
 
-    fn check_type(&self, cx: &LateContext, ty: &Ty) {
+    fn check_type(&self, cx: &LateContext<'a, 'tcx>, ty: &'tcx Ty) {
         if in_macro(cx, ty.span) {
             return;
         }
@@ -598,6 +598,7 @@ fn check_type(&self, cx: &LateContext, ty: &Ty) {
             let mut visitor = TypeComplexityVisitor {
                 score: 0,
                 nest: 1,
+                cx: cx,
             };
             visitor.visit_ty(ty);
             visitor.score
@@ -613,15 +614,16 @@ fn check_type(&self, cx: &LateContext, ty: &Ty) {
 }
 
 /// Walks a type and assigns a complexity score to it.
-struct TypeComplexityVisitor {
+struct TypeComplexityVisitor<'a, 'tcx: 'a> {
     /// total complexity score of the type
     score: u64,
     /// current nesting level
     nest: u64,
+    cx: &'a LateContext<'a, 'tcx>,
 }
 
-impl<'v> Visitor<'v> for TypeComplexityVisitor {
-    fn visit_ty(&mut self, ty: &'v Ty) {
+impl<'a, 'tcx: 'a> Visitor<'tcx> for TypeComplexityVisitor<'a, 'tcx> {
+    fn visit_ty(&mut self, ty: &'tcx Ty) {
         let (add_score, sub_nest) = match ty.node {
             // _, &x and *x have only small overhead; don't mess with nesting level
             TyInfer | TyPtr(..) | TyRptr(..) => (1, 0),
@@ -646,6 +648,9 @@ fn visit_ty(&mut self, ty: &'v Ty) {
         walk_ty(self, ty);
         self.nest -= sub_nest;
     }
+    fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
+        NestedVisitorMap::All(&self.cx.tcx.map)
+    }
 }
 
 /// **What it does:** Checks for expressions where a character literal is cast
@@ -677,8 +682,8 @@ fn get_lints(&self) -> LintArray {
     }
 }
 
-impl LateLintPass for CharLitAsU8 {
-    fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CharLitAsU8 {
+    fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
         use syntax::ast::{LitKind, UintTy};
 
         if let ExprCast(ref e, _) = expr.node {
@@ -752,7 +757,6 @@ fn detect_absurd_comparison<'a>(cx: &LateContext, op: BinOp_, lhs: &'a Expr, rhs
     use types::ExtremeType::*;
     use types::AbsurdComparisonResult::*;
     use utils::comparisons::*;
-    type Extr<'a> = ExtremeExpr<'a>;
 
     let normalized = normalize_comparison(op, lhs, rhs);
     let (rel, normalized_lhs, normalized_rhs) = if let Some(val) = normalized {
@@ -767,17 +771,17 @@ fn detect_absurd_comparison<'a>(cx: &LateContext, op: BinOp_, lhs: &'a Expr, rhs
     Some(match rel {
         Rel::Lt => {
             match (lx, rx) {
-                (Some(l @ Extr { which: Maximum, .. }), _) => (l, AlwaysFalse), // max < x
-                (_, Some(r @ Extr { which: Minimum, .. })) => (r, AlwaysFalse), // x < min
+                (Some(l @ ExtremeExpr { which: Maximum, .. }), _) => (l, AlwaysFalse), // max < x
+                (_, Some(r @ ExtremeExpr { which: Minimum, .. })) => (r, AlwaysFalse), // x < min
                 _ => return None,
             }
         }
         Rel::Le => {
             match (lx, rx) {
-                (Some(l @ Extr { which: Minimum, .. }), _) => (l, AlwaysTrue), // min <= x
-                (Some(l @ Extr { which: Maximum, .. }), _) => (l, InequalityImpossible), //max <= x
-                (_, Some(r @ Extr { which: Minimum, .. })) => (r, InequalityImpossible), // x <= min
-                (_, Some(r @ Extr { which: Maximum, .. })) => (r, AlwaysTrue), // x <= max
+                (Some(l @ ExtremeExpr { which: Minimum, .. }), _) => (l, AlwaysTrue), // min <= x
+                (Some(l @ ExtremeExpr { which: Maximum, .. }), _) => (l, InequalityImpossible), //max <= x
+                (_, Some(r @ ExtremeExpr { which: Minimum, .. })) => (r, InequalityImpossible), // x <= min
+                (_, Some(r @ ExtremeExpr { which: Maximum, .. })) => (r, AlwaysTrue), // x <= max
                 _ => return None,
             }
         }
@@ -841,8 +845,8 @@ fn detect_extreme_expr<'a>(cx: &LateContext, expr: &'a Expr) -> Option<ExtremeEx
     })
 }
 
-impl LateLintPass for AbsurdExtremeComparisons {
-    fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AbsurdExtremeComparisons {
+    fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
         use types::ExtremeType::*;
         use types::AbsurdComparisonResult::*;
 
@@ -1066,8 +1070,8 @@ fn upcast_comparison_bounds_err(cx: &LateContext, span: &Span, rel: comparisons:
     }
 }
 
-impl LateLintPass for InvalidUpcastComparisons {
-    fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidUpcastComparisons {
+    fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
         if let ExprBinary(ref cmp, ref lhs, ref rhs) = expr.node {
 
             let normalized = comparisons::normalize_comparison(cmp.node, lhs, rhs);