use rustc_span::source_map::Spanned;
use rustc_span::{BytePos, DesugaringKind, ExpnKind, Span};
-crate fn check_match(tcx: TyCtxt<'_>, def_id: DefId) {
+pub(crate) fn check_match(tcx: TyCtxt<'_>, def_id: DefId) {
let body_id = match def_id.as_local() {
None => return,
Some(id) => tcx.hir().body_owned_by(tcx.hir().local_def_id_to_hir_id(id)),
for arm in hir_arms {
// Check the arm for some things unrelated to exhaustiveness.
self.check_patterns(&arm.pat, Refutable);
- if let Some(hir::Guard::IfLet(ref pat, _)) = arm.guard {
- self.check_patterns(pat, Refutable);
- let tpat = self.lower_pattern(&mut cx, pat, &mut false);
- self.check_let_reachability(&mut cx, pat.hir_id, tpat, tpat.span());
+ if let Some(hir::Guard::IfLet(ref let_expr)) = arm.guard {
+ self.check_patterns(let_expr.pat, Refutable);
+ let tpat = self.lower_pattern(&mut cx, let_expr.pat, &mut false);
+ self.check_let_reachability(&mut cx, let_expr.pat.hir_id, tpat, tpat.span());
}
}
let mut suggestion = None;
let sm = cx.tcx.sess.source_map();
match arms {
- [] if sp.ctxt() == expr_span.ctxt() => {
+ [] if sp.eq_ctxt(expr_span) => {
// Get the span for the empty match body `{}`.
let (indentation, more) = if let Some(snippet) = sm.indentation_before(sp) {
(format!("\n{}", snippet), " ")
));
}
[only] => {
- let pre_indentation = if let (Some(snippet), true) = (
- sm.indentation_before(only.span),
- sm.is_multiline(sp.shrink_to_hi().with_hi(only.span.lo())),
- ) {
- format!("\n{}", snippet)
+ let (pre_indentation, is_multiline) = if let Some(snippet) = sm.indentation_before(only.span)
+ && let Ok(with_trailing) = sm.span_extend_while(only.span, |c| c.is_whitespace() || c == ',')
+ && sm.is_multiline(with_trailing)
+ {
+ (format!("\n{}", snippet), true)
+ } else {
+ (" ".to_string(), false)
+ };
+ let comma = if matches!(only.body.kind, hir::ExprKind::Block(..))
+ && only.span.eq_ctxt(only.body.span)
+ && is_multiline
+ {
+ ""
} else {
- " ".to_string()
+ ","
};
- let comma = if matches!(only.body.kind, hir::ExprKind::Block(..)) { "" } else { "," };
suggestion = Some((
only.span.shrink_to_hi(),
format!("{}{}{} => todo!()", comma, pre_indentation, pattern),
));
}
- [.., prev, last] if prev.span.ctxt() == last.span.ctxt() => {
+ [.., prev, last] if prev.span.eq_ctxt(last.span) => {
if let Ok(snippet) = sm.span_to_snippet(prev.span.between(last.span)) {
- let comma =
- if matches!(last.body.kind, hir::ExprKind::Block(..)) { "" } else { "," };
+ let comma = if matches!(last.body.kind, hir::ExprKind::Block(..))
+ && last.span.eq_ctxt(last.body.span)
+ {
+ ""
+ } else {
+ ","
+ };
suggestion = Some((
last.span.shrink_to_hi(),
format!(
err.emit();
}
-crate fn joined_uncovered_patterns<'p, 'tcx>(
+pub(crate) fn joined_uncovered_patterns<'p, 'tcx>(
cx: &MatchCheckCtxt<'p, 'tcx>,
witnesses: &[DeconstructedPat<'p, 'tcx>],
) -> String {
}
}
-crate fn pattern_not_covered_label(
+pub(crate) fn pattern_not_covered_label(
witnesses: &[DeconstructedPat<'_, '_>],
joined_patterns: &str,
) -> String {
match parent_node {
hir::Node::Arm(hir::Arm {
- guard: Some(hir::Guard::IfLet(&hir::Pat { hir_id, .. }, _)),
+ guard: Some(hir::Guard::IfLet(&hir::Let { pat: hir::Pat { hir_id, .. }, .. })),
..
- }) if Some(hir_id) == pat_id => {
+ }) if Some(*hir_id) == pat_id => {
return LetSource::IfLetGuard;
}
hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Let(..), span, .. }) => {