]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/infallible_destructuring_match.rs
Add Applicability::Unspecified to span_lint_and_sugg functions
[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, 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                 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(cx, variant_name.span, ".."),
85                         snippet(cx, local.pat.span, ".."),
86                         snippet(cx, target.span, ".."),
87                     ),
88                     Applicability::Unspecified,
89                 );
90             }
91         }
92     }
93 }