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;
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc_errors::Applicability;
7 use rustc_session::declare_tool_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.
13 /// **Why is this bad?** Just readability – `let` doesn't nest, whereas a `match` does.
15 /// **Known problems:** None.
23 /// let wrapper = Wrapper::Data(42);
25 /// let data = match wrapper {
26 /// Wrapper::Data(i) => i,
30 /// The correct use would be:
36 /// let wrapper = Wrapper::Data(42);
37 /// let Wrapper::Data(data) = wrapper;
39 pub INFALLIBLE_DESTRUCTURING_MATCH,
41 "a match statement with a single infallible arm instead of a `let`"
44 declare_lint_pass!(InfallibleDestructingMatch => [INFALLIBLE_DESTRUCTURING_MATCH]);
46 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InfallibleDestructingMatch {
47 fn check_local(&mut self, cx: &LateContext<'a, 'tcx>, local: &'tcx Local) {
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;
54 if let Some(arg) = get_arg_name(&args[0]);
55 let body = remove_blocks(&arms[0].body);
56 if match_var(body, arg);
59 let mut applicability = Applicability::MachineApplicable;
62 INFALLIBLE_DESTRUCTURING_MATCH,
64 "you seem to be trying to use match to destructure a single infallible pattern. \
65 Consider using `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),