1 use super::utils::{get_arg_name, match_var, remove_blocks, snippet_with_applicability, span_lint_and_sugg};
2 use if_chain::if_chain;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::{declare_lint_pass, declare_tool_lint};
6 use rustc_errors::Applicability;
9 /// **What it does:** Checks for matches being used to destructure a single-variant enum
10 /// or tuple struct where a `let` will suffice.
12 /// **Why is this bad?** Just readability – `let` doesn't nest, whereas a `match` does.
14 /// **Known problems:** None.
22 /// let wrapper = Wrapper::Data(42);
24 /// let data = match wrapper {
25 /// Wrapper::Data(i) => i,
29 /// The correct use would be:
35 /// let wrapper = Wrapper::Data(42);
36 /// let Wrapper::Data(data) = wrapper;
38 pub INFALLIBLE_DESTRUCTURING_MATCH,
40 "a match statement with a single infallible arm instead of a `let`"
43 declare_lint_pass!(InfallibleDestructingMatch => [INFALLIBLE_DESTRUCTURING_MATCH]);
45 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InfallibleDestructingMatch {
46 fn check_local(&mut self, cx: &LateContext<'a, 'tcx>, local: &'tcx Local) {
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;
53 if let Some(arg) = get_arg_name(&args[0]);
54 let body = remove_blocks(&arms[0].body);
55 if match_var(body, arg);
58 let mut applicability = Applicability::MachineApplicable;
61 INFALLIBLE_DESTRUCTURING_MATCH,
63 "you seem to be trying to use match to destructure a single infallible pattern. \
64 Consider using `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),