check_match: extract joined_uncovered_patterns.

This commit is contained in:
Mazdak Farrokhzad 2019-09-09 01:59:22 +02:00
parent e2640a51ee
commit 5435b38456

View File

@ -463,21 +463,7 @@ fn check_exhaustive<'tcx>(
pats.iter().map(|w| w.single_pattern()).collect()
};
const LIMIT: usize = 3;
let joined_patterns = match witnesses.len() {
0 => bug!(),
1 => format!("`{}`", witnesses[0]),
2..=LIMIT => {
let (tail, head) = witnesses.split_last().unwrap();
let head: Vec<_> = head.iter().map(|w| w.to_string()).collect();
format!("`{}` and `{}`", head.join("`, `"), tail)
}
_ => {
let (head, tail) = witnesses.split_at(LIMIT);
let head: Vec<_> = head.iter().map(|w| w.to_string()).collect();
format!("`{}` and {} more", head.join("`, `"), tail.len())
}
};
let joined_patterns = joined_uncovered_patterns(&witnesses);
let mut err = create_e0004(cx.tcx.sess, sp, format!(
"non-exhaustive patterns: {} not covered",
@ -501,6 +487,24 @@ fn check_exhaustive<'tcx>(
err.emit();
}
fn joined_uncovered_patterns(witnesses: &[&Pattern<'_>]) -> String {
const LIMIT: usize = 3;
match witnesses.len() {
0 => bug!(),
1 => format!("`{}`", witnesses[0]),
2..=LIMIT => {
let (tail, head) = witnesses.split_last().unwrap();
let head: Vec<_> = head.iter().map(|w| w.to_string()).collect();
format!("`{}` and `{}`", head.join("`, `"), tail)
}
_ => {
let (head, tail) = witnesses.split_at(LIMIT);
let head: Vec<_> = head.iter().map(|w| w.to_string()).collect();
format!("`{}` and {} more", head.join("`, `"), tail.len())
}
}
}
fn adt_defined_here(cx: &mut MatchCheckCtxt<'_, '_>, ty: Ty<'_>, err: &mut DiagnosticBuilder<'_>) {
if let ty::Adt(def, _) = ty.sty {
if let Some(sp) = cx.tcx.hir().span_if_local(def.did) {