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_tool_lint, lint_array};
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 #[derive(Copy, Clone, Default)]
46 impl LintPass for Pass {
47 fn get_lints(&self) -> LintArray {
48 lint_array!(INFALLIBLE_DESTRUCTURING_MATCH)
51 fn name(&self) -> &'static str {
52 "InfallibleDestructingMatch"
56 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
57 fn check_local(&mut self, cx: &LateContext<'a, 'tcx>, local: &'tcx Local) {
59 if let Some(ref expr) = local.init;
60 if let ExprKind::Match(ref target, ref arms, MatchSource::Normal) = expr.node;
61 if arms.len() == 1 && arms[0].pats.len() == 1 && arms[0].guard.is_none();
62 if let PatKind::TupleStruct(QPath::Resolved(None, ref variant_name), ref args, _) = arms[0].pats[0].node;
64 if let Some(arg) = get_arg_name(&args[0]);
65 let body = remove_blocks(&arms[0].body);
66 if match_var(body, arg);
69 let mut applicability = Applicability::MachineApplicable;
72 INFALLIBLE_DESTRUCTURING_MATCH,
74 "you seem to be trying to use match to destructure a single infallible pattern. \
75 Consider using `let`",
79 snippet_with_applicability(cx, variant_name.span, "..", &mut applicability),
80 snippet_with_applicability(cx, local.pat.span, "..", &mut applicability),
81 snippet_with_applicability(cx, target.span, "..", &mut applicability),