]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/infallible_destructuring_match.rs
Merge remote-tracking branch 'upstream/rust-1.36.0' into backport_merge
[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_lint_pass, declare_tool_lint};
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 declare_lint_pass!(InfallibleDestructingMatch => [INFALLIBLE_DESTRUCTURING_MATCH]);
44
45 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InfallibleDestructingMatch {
46     fn check_local(&mut self, cx: &LateContext<'a, 'tcx>, local: &'tcx Local) {
47         if_chain! {
48             if let Some(ref expr) = local.init;
49             if let ExprKind::Match(ref target, ref arms, MatchSource::Normal) = expr.kind;
50             if arms.len() == 1 && arms[0].guard.is_none();
51             if let PatKind::TupleStruct(QPath::Resolved(None, ref variant_name), ref args, _) = arms[0].pat.kind;
52             if args.len() == 1;
53             if let Some(arg) = get_arg_name(&args[0]);
54             let body = remove_blocks(&arms[0].body);
55             if match_var(body, arg);
56
57             then {
58                 let mut applicability = Applicability::MachineApplicable;
59                 span_lint_and_sugg(
60                     cx,
61                     INFALLIBLE_DESTRUCTURING_MATCH,
62                     local.span,
63                     "you seem to be trying to use match to destructure a single infallible pattern. \
64                      Consider using `let`",
65                     "try this",
66                     format!(
67                         "let {}({}) = {};",
68                         snippet_with_applicability(cx, variant_name.span, "..", &mut applicability),
69                         snippet_with_applicability(cx, local.pat.span, "..", &mut applicability),
70                         snippet_with_applicability(cx, target.span, "..", &mut applicability),
71                     ),
72                     applicability,
73                 );
74             }
75         }
76     }
77 }