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