]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/infallible_destructuring_match.rs
rustup https://github.com/rust-lang/rust/pull/57726
[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 /// **What it does:** Checks for matches being used to destructure a single-variant enum
9 /// or tuple struct where a `let` will suffice.
10 ///
11 /// **Why is this bad?** Just readability – `let` doesn't nest, whereas a `match` does.
12 ///
13 /// **Known problems:** None.
14 ///
15 /// **Example:**
16 /// ```rust
17 /// enum Wrapper {
18 ///     Data(i32),
19 /// }
20 ///
21 /// let wrapper = Wrapper::Data(42);
22 ///
23 /// let data = match wrapper {
24 ///     Wrapper::Data(i) => i,
25 /// };
26 /// ```
27 ///
28 /// The correct use would be:
29 /// ```rust
30 /// enum Wrapper {
31 ///     Data(i32),
32 /// }
33 ///
34 /// let wrapper = Wrapper::Data(42);
35 /// let Wrapper::Data(data) = wrapper;
36 /// ```
37 declare_clippy_lint! {
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 }