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