Skip to content

Commit 5435b38

Browse files
committed
check_match: extract joined_uncovered_patterns.
1 parent e2640a5 commit 5435b38

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -463,21 +463,7 @@ fn check_exhaustive<'tcx>(
463463
pats.iter().map(|w| w.single_pattern()).collect()
464464
};
465465

466-
const LIMIT: usize = 3;
467-
let joined_patterns = match witnesses.len() {
468-
0 => bug!(),
469-
1 => format!("`{}`", witnesses[0]),
470-
2..=LIMIT => {
471-
let (tail, head) = witnesses.split_last().unwrap();
472-
let head: Vec<_> = head.iter().map(|w| w.to_string()).collect();
473-
format!("`{}` and `{}`", head.join("`, `"), tail)
474-
}
475-
_ => {
476-
let (head, tail) = witnesses.split_at(LIMIT);
477-
let head: Vec<_> = head.iter().map(|w| w.to_string()).collect();
478-
format!("`{}` and {} more", head.join("`, `"), tail.len())
479-
}
480-
};
466+
let joined_patterns = joined_uncovered_patterns(&witnesses);
481467

482468
let mut err = create_e0004(cx.tcx.sess, sp, format!(
483469
"non-exhaustive patterns: {} not covered",
@@ -501,6 +487,24 @@ fn check_exhaustive<'tcx>(
501487
err.emit();
502488
}
503489

490+
fn joined_uncovered_patterns(witnesses: &[&Pattern<'_>]) -> String {
491+
const LIMIT: usize = 3;
492+
match witnesses.len() {
493+
0 => bug!(),
494+
1 => format!("`{}`", witnesses[0]),
495+
2..=LIMIT => {
496+
let (tail, head) = witnesses.split_last().unwrap();
497+
let head: Vec<_> = head.iter().map(|w| w.to_string()).collect();
498+
format!("`{}` and `{}`", head.join("`, `"), tail)
499+
}
500+
_ => {
501+
let (head, tail) = witnesses.split_at(LIMIT);
502+
let head: Vec<_> = head.iter().map(|w| w.to_string()).collect();
503+
format!("`{}` and {} more", head.join("`, `"), tail.len())
504+
}
505+
}
506+
}
507+
504508
fn adt_defined_here(cx: &mut MatchCheckCtxt<'_, '_>, ty: Ty<'_>, err: &mut DiagnosticBuilder<'_>) {
505509
if let ty::Adt(def, _) = ty.sty {
506510
if let Some(sp) = cx.tcx.hir().span_if_local(def.did) {

0 commit comments

Comments
 (0)