|
1 |
| -use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg}; |
2 |
| -use clippy_utils::source::snippet_with_applicability; |
3 |
| -use clippy_utils::{is_wild, meets_msrv, msrvs, path_to_local_id, peel_blocks, strip_pat_refs}; |
| 1 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 2 | +use clippy_utils::{is_wild, meets_msrv, msrvs}; |
4 | 3 | use if_chain::if_chain;
|
5 |
| -use rustc_errors::Applicability; |
6 | 4 | use rustc_hir::{Arm, Expr, ExprKind, Local, MatchSource, Pat, PatKind, QPath};
|
7 | 5 | use rustc_lint::{LateContext, LateLintPass};
|
8 | 6 | use rustc_middle::ty;
|
9 | 7 | use rustc_semver::RustcVersion;
|
10 | 8 | use rustc_session::{declare_tool_lint, impl_lint_pass};
|
11 | 9 |
|
| 10 | +mod infalliable_detructuring_match; |
12 | 11 | mod match_as_ref;
|
13 | 12 | mod match_bool;
|
14 | 13 | mod match_like_matches;
|
@@ -637,38 +636,7 @@ impl<'tcx> LateLintPass<'tcx> for Matches {
|
637 | 636 | }
|
638 | 637 |
|
639 | 638 | fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'_>) {
|
640 |
| - if_chain! { |
641 |
| - if !local.span.from_expansion(); |
642 |
| - if let Some(expr) = local.init; |
643 |
| - if let ExprKind::Match(target, arms, MatchSource::Normal) = expr.kind; |
644 |
| - if arms.len() == 1 && arms[0].guard.is_none(); |
645 |
| - if let PatKind::TupleStruct( |
646 |
| - QPath::Resolved(None, variant_name), args, _) = arms[0].pat.kind; |
647 |
| - if args.len() == 1; |
648 |
| - if let PatKind::Binding(_, arg, ..) = strip_pat_refs(&args[0]).kind; |
649 |
| - let body = peel_blocks(arms[0].body); |
650 |
| - if path_to_local_id(body, arg); |
651 |
| - |
652 |
| - then { |
653 |
| - let mut applicability = Applicability::MachineApplicable; |
654 |
| - self.infallible_destructuring_match_linted = true; |
655 |
| - span_lint_and_sugg( |
656 |
| - cx, |
657 |
| - INFALLIBLE_DESTRUCTURING_MATCH, |
658 |
| - local.span, |
659 |
| - "you seem to be trying to use `match` to destructure a single infallible pattern. \ |
660 |
| - Consider using `let`", |
661 |
| - "try this", |
662 |
| - format!( |
663 |
| - "let {}({}) = {};", |
664 |
| - snippet_with_applicability(cx, variant_name.span, "..", &mut applicability), |
665 |
| - snippet_with_applicability(cx, local.pat.span, "..", &mut applicability), |
666 |
| - snippet_with_applicability(cx, target.span, "..", &mut applicability), |
667 |
| - ), |
668 |
| - applicability, |
669 |
| - ); |
670 |
| - } |
671 |
| - } |
| 639 | + self.infallible_destructuring_match_linted |= infalliable_detructuring_match::check(cx, local); |
672 | 640 | }
|
673 | 641 |
|
674 | 642 | fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
|
|
0 commit comments