]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/matches.rs
Auto merge of #3985 - phansch:move_some_cast_tests, r=flip1995
[rust.git] / clippy_lints / src / matches.rs
index e7e1df5cffff201855942b4892326f4a9a289401..bcd3119b7bc58b86beae72ab7f8c336b599f33ae 100644 (file)
@@ -9,8 +9,8 @@
 use rustc::hir::def::CtorKind;
 use rustc::hir::*;
 use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
-use rustc::ty::{self, Ty, TyKind};
-use rustc::{declare_tool_lint, lint_array};
+use rustc::ty::{self, Ty};
+use rustc::{declare_lint_pass, declare_tool_lint};
 use rustc_errors::Applicability;
 use std::cmp::Ordering;
 use std::collections::Bound;
@@ -27,7 +27,9 @@
     /// **Known problems:** None.
     ///
     /// **Example:**
-    /// ```ignore
+    /// ```rust
+    /// # fn bar(stool: &str) {}
+    /// # let x = Some("abc");
     /// match x {
     ///     Some(ref foo) => bar(foo),
     ///     _ => (),
@@ -59,7 +61,7 @@
     ///
     /// Using `if let` with `else`:
     ///
-    /// ```ignore
+    /// ```rust
     /// if let Some(ref foo) = x {
     ///     bar(foo);
     /// } else {
@@ -82,7 +84,7 @@
     /// **Known problems:** None.
     ///
     /// **Example:**
-    /// ```ignore
+    /// ```rust,ignore
     /// match x {
     ///     &A(ref y) => foo(y),
     ///     &B => bar(),
     /// **Known problems:** None.
     ///
     /// **Example:**
-    /// ```ignore
+    /// ```rust
+    /// # fn foo() {}
+    /// # fn bar() {}
     /// let condition: bool = true;
     /// match condition {
     ///     true => foo(),
     /// }
     /// ```
     /// Use if/else instead:
-    /// ```ignore
+    /// ```rust
+    /// # fn foo() {}
+    /// # fn bar() {}
     /// let condition: bool = true;
     /// if condition {
     ///     foo();
     "a wildcard enum match arm using `_`"
 }
 
-#[allow(missing_copy_implementations)]
-pub struct MatchPass;
-
-impl LintPass for MatchPass {
-    fn get_lints(&self) -> LintArray {
-        lint_array!(
-            SINGLE_MATCH,
-            MATCH_REF_PATS,
-            MATCH_BOOL,
-            SINGLE_MATCH_ELSE,
-            MATCH_OVERLAPPING_ARM,
-            MATCH_WILD_ERR_ARM,
-            MATCH_AS_REF,
-            WILDCARD_ENUM_MATCH_ARM
-        )
-    }
-
-    fn name(&self) -> &'static str {
-        "Matches"
-    }
-}
-
-impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MatchPass {
+declare_lint_pass!(Matches => [
+    SINGLE_MATCH,
+    MATCH_REF_PATS,
+    MATCH_BOOL,
+    SINGLE_MATCH_ELSE,
+    MATCH_OVERLAPPING_ARM,
+    MATCH_WILD_ERR_ARM,
+    MATCH_AS_REF,
+    WILDCARD_ENUM_MATCH_ARM
+]);
+
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Matches {
     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
         if in_external_macro(cx.sess(), expr.span) {
             return;
@@ -494,7 +489,7 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
         // already covered.
 
         let mut missing_variants = vec![];
-        if let TyKind::Adt(def, _) = ty.sty {
+        if let ty::Adt(def, _) = ty.sty {
             for variant in &def.variants {
                 missing_variants.push(variant);
             }
@@ -510,11 +505,11 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
             for pat in &arm.pats {
                 if let PatKind::Path(ref path) = pat.deref().node {
                     if let QPath::Resolved(_, p) = path {
-                        missing_variants.retain(|e| e.did != p.def.def_id());
+                        missing_variants.retain(|e| e.ctor_def_id != Some(p.def.def_id()));
                     }
                 } else if let PatKind::TupleStruct(ref path, ..) = pat.deref().node {
                     if let QPath::Resolved(_, p) = path {
-                        missing_variants.retain(|e| e.did != p.def.def_id());
+                        missing_variants.retain(|e| e.ctor_def_id != Some(p.def.def_id()));
                     }
                 }
             }
@@ -533,7 +528,7 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) {
                     String::new()
                 };
                 // This path assumes that the enum type is imported into scope.
-                format!("{}{}{}", ident_str, cx.tcx.item_path_str(v.did), suffix)
+                format!("{}{}{}", ident_str, cx.tcx.def_path_str(v.def_id), suffix)
             })
             .collect();