X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Fmatches.rs;h=bcd3119b7bc58b86beae72ab7f8c336b599f33ae;hb=6feed2713c7740eb5868eba43745bc508b8b77aa;hp=2fc3a4fd5c176d50cb6fd1eabfd12df8b434a54e;hpb=15d1731ce8d3782ba93b0fd583307240fc814ef3;p=rust.git diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 2fc3a4fd5c1..bcd3119b7bc 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -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), /// _ => (), @@ -35,7 +37,7 @@ /// ``` pub SINGLE_MATCH, style, - "a match statement with a single nontrivial arm (i.e. where the other arm is `_ => {}`) instead of `if let`" + "a match statement with a single nontrivial arm (i.e., where the other arm is `_ => {}`) instead of `if let`" } declare_clippy_lint! { @@ -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(), @@ -103,7 +105,9 @@ /// **Known problems:** None. /// /// **Example:** - /// ```ignore + /// ```rust + /// # fn foo() {} + /// # fn bar() {} /// let condition: bool = true; /// match condition { /// true => foo(), @@ -111,7 +115,9 @@ /// } /// ``` /// Use if/else instead: - /// ```ignore + /// ```rust + /// # fn foo() {} + /// # fn bar() {} /// let condition: bool = true; /// if condition { /// foo(); @@ -209,29 +215,18 @@ "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; @@ -322,7 +317,7 @@ fn check_single_match_opt_like( ty: Ty<'_>, els: Option<&Expr>, ) { - // list of candidate Enums we know will never get any more members + // list of candidate `Enum`s we know will never get any more members let candidates = &[ (&paths::COW, "Borrowed"), (&paths::COW, "Cow::Borrowed"), @@ -335,7 +330,7 @@ fn check_single_match_opt_like( let path = match arms[1].pats[0].node { PatKind::TupleStruct(ref path, ref inner, _) => { - // contains any non wildcard patterns? e.g. Err(err) + // Contains any non wildcard patterns (e.g., `Err(err)`)? if !inner.iter().all(is_wild) { return; } @@ -354,7 +349,7 @@ fn check_single_match_opt_like( } fn check_match_bool(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr: &Expr) { - // type of expression == bool + // Type of expression is `bool`. if cx.tables.expr_ty(ex).sty == ty::Bool { span_lint_and_then( cx, @@ -482,7 +477,7 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) { for pat in &arm.pats { if let PatKind::Wild = pat.node { wildcard_span = Some(pat.span); - } else if let PatKind::Binding(_, _, _, ident, None) = pat.node { + } else if let PatKind::Binding(_, _, ident, None) = pat.node { wildcard_span = Some(pat.span); wildcard_ident = Some(ident); } @@ -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(); @@ -639,7 +634,7 @@ fn check_match_as_ref(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm], expr: & } } -/// Get all arms that are unbounded `PatRange`s. +/// Gets all arms that are unbounded `PatRange`s. fn all_ranges<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arms: &'tcx [Arm]) -> Vec> { arms.iter() .flat_map(|arm| { @@ -687,7 +682,7 @@ pub struct SpannedRange { type TypedRanges = Vec>; -/// Get all `Int` ranges or all `Uint` ranges. Mixed types are an error anyway +/// Gets all `Int` ranges or all `Uint` ranges. Mixed types are an error anyway /// and other types than /// `Uint` and `Int` probably don't make sense. fn type_ranges(ranges: &[SpannedRange]) -> TypedRanges {