Reword error message

This commit is contained in:
Esteban Küber 2019-03-02 16:44:00 -08:00
parent 509bb0acba
commit d651281a71
2 changed files with 19 additions and 9 deletions

View file

@ -217,7 +217,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
if def.variants.len() < 4 && !def.variants.is_empty() { if def.variants.len() < 4 && !def.variants.is_empty() {
// keep around to point at the definition of non-covered variants // keep around to point at the definition of non-covered variants
missing_variants = def.variants.iter() missing_variants = def.variants.iter()
.map(|variant| variant.ident.span) .map(|variant| variant.ident)
.collect(); .collect();
} }
def.variants.is_empty() def.variants.is_empty()
@ -227,10 +227,19 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
}; };
if !scrutinee_is_uninhabited { if !scrutinee_is_uninhabited {
// We know the type is inhabited, so this must be wrong // We know the type is inhabited, so this must be wrong
let mut err = create_e0004(self.tcx.sess, scrut.span, format!( let mut err = create_e0004(
"non-exhaustive patterns: type `{}` is non-empty", self.tcx.sess,
pat_ty, scrut.span,
)); format!("non-exhaustive patterns: {}", match missing_variants.len() {
0 => format!("type `{}` is non-empty", pat_ty),
1 => format!(
"pattern `{}` of type `{}` is not handled",
missing_variants[0].name,
pat_ty,
),
_ => format!("multiple patterns of type `{}` are not handled", pat_ty),
}),
);
err.help("ensure that all possible cases are being handled, \ err.help("ensure that all possible cases are being handled, \
possibly by adding wildcards or more match arms"); possibly by adding wildcards or more match arms");
if let Some(sp) = def_span { if let Some(sp) = def_span {
@ -238,7 +247,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
} }
// point at the definition of non-covered enum variants // point at the definition of non-covered enum variants
for variant in &missing_variants { for variant in &missing_variants {
err.span_label(*variant, "variant not covered"); err.span_label(variant.span, "variant not covered");
} }
err.emit(); err.emit();
} }
@ -508,7 +517,8 @@ fn maybe_point_at_variant(
) -> Vec<Span> { ) -> Vec<Span> {
let mut covered = vec![]; let mut covered = vec![];
if let ty::Adt(def, _) = sty { if let ty::Adt(def, _) = sty {
// Don't point at the variants if they are too many to avoid visual clutter // Don't point at variants that have already been covered due to other patterns to avoid
// visual clutter
for pattern in patterns { for pattern in patterns {
let pk: &PatternKind<'_> = &pattern.kind; let pk: &PatternKind<'_> = &pattern.kind;
if let PatternKind::Variant { adt_def, variant_index, subpatterns, .. } = pk { if let PatternKind::Variant { adt_def, variant_index, subpatterns, .. } = pk {
@ -526,7 +536,7 @@ fn maybe_point_at_variant(
); );
} }
} }
if let PatternKind::Leaf{ subpatterns } = pk { if let PatternKind::Leaf { subpatterns } = pk {
let subpatterns = subpatterns.iter() let subpatterns = subpatterns.iter()
.map(|field_pattern| field_pattern.pattern.clone()) .map(|field_pattern| field_pattern.pattern.clone())
.collect::<Vec<_>>(); .collect::<Vec<_>>();

View file

@ -1,4 +1,4 @@
error[E0004]: non-exhaustive patterns: type `std::option::Option<i32>` is non-empty error[E0004]: non-exhaustive patterns: multiple patterns of type `std::option::Option<i32>` are not handled
--> $DIR/E0004-2.rs:4:11 --> $DIR/E0004-2.rs:4:11
| |
LL | match x { } //~ ERROR E0004 LL | match x { } //~ ERROR E0004