Skip to content

Commit 6477923

Browse files
committed
Split out infalliable_detructuring_match
1 parent fb1093c commit 6477923

File tree

2 files changed

+48
-36
lines changed

2 files changed

+48
-36
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::source::snippet_with_applicability;
3+
use clippy_utils::{path_to_local_id, peel_blocks, strip_pat_refs};
4+
use rustc_errors::Applicability;
5+
use rustc_hir::{ExprKind, Local, MatchSource, PatKind, QPath};
6+
use rustc_lint::LateContext;
7+
8+
use super::INFALLIBLE_DESTRUCTURING_MATCH;
9+
10+
pub(crate) fn check(cx: &LateContext<'_>, local: &Local<'_>) -> bool {
11+
if_chain! {
12+
if !local.span.from_expansion();
13+
if let Some(expr) = local.init;
14+
if let ExprKind::Match(target, arms, MatchSource::Normal) = expr.kind;
15+
if arms.len() == 1 && arms[0].guard.is_none();
16+
if let PatKind::TupleStruct(
17+
QPath::Resolved(None, variant_name), args, _) = arms[0].pat.kind;
18+
if args.len() == 1;
19+
if let PatKind::Binding(_, arg, ..) = strip_pat_refs(&args[0]).kind;
20+
let body = peel_blocks(arms[0].body);
21+
if path_to_local_id(body, arg);
22+
23+
then {
24+
let mut applicability = Applicability::MachineApplicable;
25+
span_lint_and_sugg(
26+
cx,
27+
INFALLIBLE_DESTRUCTURING_MATCH,
28+
local.span,
29+
"you seem to be trying to use `match` to destructure a single infallible pattern. \
30+
Consider using `let`",
31+
"try this",
32+
format!(
33+
"let {}({}) = {};",
34+
snippet_with_applicability(cx, variant_name.span, "..", &mut applicability),
35+
snippet_with_applicability(cx, local.pat.span, "..", &mut applicability),
36+
snippet_with_applicability(cx, target.span, "..", &mut applicability),
37+
),
38+
applicability,
39+
);
40+
return true;
41+
}
42+
}
43+
false
44+
}

clippy_lints/src/matches/mod.rs

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
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};
43
use if_chain::if_chain;
5-
use rustc_errors::Applicability;
64
use rustc_hir::{Arm, Expr, ExprKind, Local, MatchSource, Pat, PatKind, QPath};
75
use rustc_lint::{LateContext, LateLintPass};
86
use rustc_middle::ty;
97
use rustc_semver::RustcVersion;
108
use rustc_session::{declare_tool_lint, impl_lint_pass};
119

10+
mod infalliable_detructuring_match;
1211
mod match_as_ref;
1312
mod match_bool;
1413
mod match_like_matches;
@@ -637,38 +636,7 @@ impl<'tcx> LateLintPass<'tcx> for Matches {
637636
}
638637

639638
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);
672640
}
673641

674642
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {

0 commit comments

Comments
 (0)