X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Finfallible_destructuring_match.rs;h=a9539534ea9cdd6852ca2cc4715fe67ab1ba2cce;hb=6d1225981177587fbb68d9c4902c770c3dbaafb0;hp=704b583f813878b81519aa876a9899ae83505bf4;hpb=13421e3945a28dad49226c4d08986eaacd1033d9;p=rust.git diff --git a/clippy_lints/src/infallible_destructuring_match.rs b/clippy_lints/src/infallible_destructuring_match.rs index 704b583f813..a9539534ea9 100644 --- a/clippy_lints/src/infallible_destructuring_match.rs +++ b/clippy_lints/src/infallible_destructuring_match.rs @@ -1,65 +1,55 @@ use super::utils::{get_arg_name, match_var, remove_blocks, snippet_with_applicability, span_lint_and_sugg}; use if_chain::if_chain; +use rustc::declare_lint_pass; use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; -use rustc::{declare_tool_lint, lint_array}; use rustc_errors::Applicability; +use rustc_session::declare_tool_lint; -/// **What it does:** Checks for matches being used to destructure a single-variant enum -/// or tuple struct where a `let` will suffice. -/// -/// **Why is this bad?** Just readability – `let` doesn't nest, whereas a `match` does. -/// -/// **Known problems:** None. -/// -/// **Example:** -/// ```rust -/// enum Wrapper { -/// Data(i32), -/// } -/// -/// let wrapper = Wrapper::Data(42); -/// -/// let data = match wrapper { -/// Wrapper::Data(i) => i, -/// }; -/// ``` -/// -/// The correct use would be: -/// ```rust -/// enum Wrapper { -/// Data(i32), -/// } -/// -/// let wrapper = Wrapper::Data(42); -/// let Wrapper::Data(data) = wrapper; -/// ``` declare_clippy_lint! { + /// **What it does:** Checks for matches being used to destructure a single-variant enum + /// or tuple struct where a `let` will suffice. + /// + /// **Why is this bad?** Just readability – `let` doesn't nest, whereas a `match` does. + /// + /// **Known problems:** None. + /// + /// **Example:** + /// ```rust + /// enum Wrapper { + /// Data(i32), + /// } + /// + /// let wrapper = Wrapper::Data(42); + /// + /// let data = match wrapper { + /// Wrapper::Data(i) => i, + /// }; + /// ``` + /// + /// The correct use would be: + /// ```rust + /// enum Wrapper { + /// Data(i32), + /// } + /// + /// let wrapper = Wrapper::Data(42); + /// let Wrapper::Data(data) = wrapper; + /// ``` pub INFALLIBLE_DESTRUCTURING_MATCH, style, "a match statement with a single infallible arm instead of a `let`" } -#[derive(Copy, Clone, Default)] -pub struct Pass; +declare_lint_pass!(InfallibleDestructingMatch => [INFALLIBLE_DESTRUCTURING_MATCH]); -impl LintPass for Pass { - fn get_lints(&self) -> LintArray { - lint_array!(INFALLIBLE_DESTRUCTURING_MATCH) - } - - fn name(&self) -> &'static str { - "InfallibleDestructingMatch" - } -} - -impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InfallibleDestructingMatch { fn check_local(&mut self, cx: &LateContext<'a, 'tcx>, local: &'tcx Local) { if_chain! { if let Some(ref expr) = local.init; - if let ExprKind::Match(ref target, ref arms, MatchSource::Normal) = expr.node; - if arms.len() == 1 && arms[0].pats.len() == 1 && arms[0].guard.is_none(); - if let PatKind::TupleStruct(QPath::Resolved(None, ref variant_name), ref args, _) = arms[0].pats[0].node; + if let ExprKind::Match(ref target, ref arms, MatchSource::Normal) = expr.kind; + if arms.len() == 1 && arms[0].guard.is_none(); + if let PatKind::TupleStruct(QPath::Resolved(None, ref variant_name), ref args, _) = arms[0].pat.kind; if args.len() == 1; if let Some(arg) = get_arg_name(&args[0]); let body = remove_blocks(&arms[0].body);