1 use super::utils::{get_arg_name, match_var, remove_blocks, snippet, span_lint_and_sugg};
5 /// **What it does:** Checks for matches being used to destructure a single-variant enum
6 /// or tuple struct where a `let` will suffice.
8 /// **Why is this bad?** Just readability – `let` doesn't nest, whereas a `match` does.
10 /// **Known problems:** None.
18 /// let wrapper = Wrapper::Data(42);
20 /// let data = match wrapper {
21 /// Wrapper::Data(i) => i,
25 /// The correct use would be:
31 /// let wrapper = Wrapper::Data(42);
32 /// let Wrapper::Data(data) = wrapper;
34 declare_clippy_lint! {
35 pub INFALLIBLE_DESTRUCTURING_MATCH,
37 "a match statement with a single infallible arm instead of a `let`"
40 #[derive(Copy, Clone, Default)]
43 impl LintPass for Pass {
44 fn get_lints(&self) -> LintArray {
45 lint_array!(INFALLIBLE_DESTRUCTURING_MATCH)
49 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
50 fn check_local(&mut self, cx: &LateContext<'a, 'tcx>, local: &'tcx Local) {
52 if let Some(ref expr) = local.init;
53 if let ExprKind::Match(ref target, ref arms, MatchSource::Normal) = expr.node;
54 if arms.len() == 1 && arms[0].pats.len() == 1 && arms[0].guard.is_none();
55 if let PatKind::TupleStruct(QPath::Resolved(None, ref variant_name), ref args, _) = arms[0].pats[0].node;
57 if let Some(arg) = get_arg_name(&args[0]);
58 let body = remove_blocks(&arms[0].body);
59 if match_var(body, arg);
64 INFALLIBLE_DESTRUCTURING_MATCH,
66 "you seem to be trying to use match to destructure a single infallible pattern. \
67 Consider using `let`",
71 snippet(cx, variant_name.span, ".."),
72 snippet(cx, local.pat.span, ".."),
73 snippet(cx, target.span, ".."),