]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/infallible_destructuring_match.rs
Merge pull request #3465 from flip1995/rustfmt
[rust.git] / clippy_lints / src / infallible_destructuring_match.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 use super::utils::{get_arg_name, match_var, remove_blocks, snippet_with_applicability, span_lint_and_sugg};
11 use crate::rustc::hir::*;
12 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
13 use crate::rustc::{declare_tool_lint, lint_array};
14 use crate::rustc_errors::Applicability;
15 use if_chain::if_chain;
16
17 /// **What it does:** Checks for matches being used to destructure a single-variant enum
18 /// or tuple struct where a `let` will suffice.
19 ///
20 /// **Why is this bad?** Just readability – `let` doesn't nest, whereas a `match` does.
21 ///
22 /// **Known problems:** None.
23 ///
24 /// **Example:**
25 /// ```rust
26 /// enum Wrapper {
27 ///     Data(i32),
28 /// }
29 ///
30 /// let wrapper = Wrapper::Data(42);
31 ///
32 /// let data = match wrapper {
33 ///     Wrapper::Data(i) => i,
34 /// };
35 /// ```
36 ///
37 /// The correct use would be:
38 /// ```rust
39 /// enum Wrapper {
40 ///     Data(i32),
41 /// }
42 ///
43 /// let wrapper = Wrapper::Data(42);
44 /// let Wrapper::Data(data) = wrapper;
45 /// ```
46 declare_clippy_lint! {
47     pub INFALLIBLE_DESTRUCTURING_MATCH,
48     style,
49     "a match statement with a single infallible arm instead of a `let`"
50 }
51
52 #[derive(Copy, Clone, Default)]
53 pub struct Pass;
54
55 impl LintPass for Pass {
56     fn get_lints(&self) -> LintArray {
57         lint_array!(INFALLIBLE_DESTRUCTURING_MATCH)
58     }
59 }
60
61 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
62     fn check_local(&mut self, cx: &LateContext<'a, 'tcx>, local: &'tcx Local) {
63         if_chain! {
64             if let Some(ref expr) = local.init;
65             if let ExprKind::Match(ref target, ref arms, MatchSource::Normal) = expr.node;
66             if arms.len() == 1 && arms[0].pats.len() == 1 && arms[0].guard.is_none();
67             if let PatKind::TupleStruct(QPath::Resolved(None, ref variant_name), ref args, _) = arms[0].pats[0].node;
68             if args.len() == 1;
69             if let Some(arg) = get_arg_name(&args[0]);
70             let body = remove_blocks(&arms[0].body);
71             if match_var(body, arg);
72
73             then {
74                 let mut applicability = Applicability::MachineApplicable;
75                 span_lint_and_sugg(
76                     cx,
77                     INFALLIBLE_DESTRUCTURING_MATCH,
78                     local.span,
79                     "you seem to be trying to use match to destructure a single infallible pattern. \
80                      Consider using `let`",
81                     "try this",
82                     format!(
83                         "let {}({}) = {};",
84                         snippet_with_applicability(cx, variant_name.span, "..", &mut applicability),
85                         snippet_with_applicability(cx, local.pat.span, "..", &mut applicability),
86                         snippet_with_applicability(cx, target.span, "..", &mut applicability),
87                     ),
88                     applicability,
89                 );
90             }
91         }
92     }
93 }