]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/infallible_destructuring_match.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[rust.git] / clippy_lints / src / infallible_destructuring_match.rs
1 use super::utils::{get_arg_name, match_var, remove_blocks, snippet_with_applicability, span_lint_and_sugg};
2 use if_chain::if_chain;
3 use rustc::hir::*;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::{declare_tool_lint, lint_array};
6 use rustc_errors::Applicability;
7
8 declare_clippy_lint! {
9     /// **What it does:** Checks for matches being used to destructure a single-variant enum
10     /// or tuple struct where a `let` will suffice.
11     ///
12     /// **Why is this bad?** Just readability – `let` doesn't nest, whereas a `match` does.
13     ///
14     /// **Known problems:** None.
15     ///
16     /// **Example:**
17     /// ```rust
18     /// enum Wrapper {
19     ///     Data(i32),
20     /// }
21     ///
22     /// let wrapper = Wrapper::Data(42);
23     ///
24     /// let data = match wrapper {
25     ///     Wrapper::Data(i) => i,
26     /// };
27     /// ```
28     ///
29     /// The correct use would be:
30     /// ```rust
31     /// enum Wrapper {
32     ///     Data(i32),
33     /// }
34     ///
35     /// let wrapper = Wrapper::Data(42);
36     /// let Wrapper::Data(data) = wrapper;
37     /// ```
38     pub INFALLIBLE_DESTRUCTURING_MATCH,
39     style,
40     "a match statement with a single infallible arm instead of a `let`"
41 }
42
43 #[derive(Copy, Clone, Default)]
44 pub struct Pass;
45
46 impl LintPass for Pass {
47     fn get_lints(&self) -> LintArray {
48         lint_array!(INFALLIBLE_DESTRUCTURING_MATCH)
49     }
50
51     fn name(&self) -> &'static str {
52         "InfallibleDestructingMatch"
53     }
54 }
55
56 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
57     fn check_local(&mut self, cx: &LateContext<'a, 'tcx>, local: &'tcx Local) {
58         if_chain! {
59             if let Some(ref expr) = local.init;
60             if let ExprKind::Match(ref target, ref arms, MatchSource::Normal) = expr.node;
61             if arms.len() == 1 && arms[0].pats.len() == 1 && arms[0].guard.is_none();
62             if let PatKind::TupleStruct(QPath::Resolved(None, ref variant_name), ref args, _) = arms[0].pats[0].node;
63             if args.len() == 1;
64             if let Some(arg) = get_arg_name(&args[0]);
65             let body = remove_blocks(&arms[0].body);
66             if match_var(body, arg);
67
68             then {
69                 let mut applicability = Applicability::MachineApplicable;
70                 span_lint_and_sugg(
71                     cx,
72                     INFALLIBLE_DESTRUCTURING_MATCH,
73                     local.span,
74                     "you seem to be trying to use match to destructure a single infallible pattern. \
75                      Consider using `let`",
76                     "try this",
77                     format!(
78                         "let {}({}) = {};",
79                         snippet_with_applicability(cx, variant_name.span, "..", &mut applicability),
80                         snippet_with_applicability(cx, local.pat.span, "..", &mut applicability),
81                         snippet_with_applicability(cx, target.span, "..", &mut applicability),
82                     ),
83                     applicability,
84                 );
85             }
86         }
87     }
88 }