Skip to content

Commit

Permalink
Reuse adt_defined_here
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Dec 4, 2019
1 parent d289f55 commit 40f434b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
12 changes: 4 additions & 8 deletions src/librustc_mir/hair/pattern/check_match.rs
Expand Up @@ -459,11 +459,9 @@ fn check_exhaustive<'p, 'tcx>(
return;
} else {
// We know the type is inhabited, so this must be wrong
let (def_span, non_empty_enum) = match scrut_ty.kind {
ty::Adt(def, _) if def.is_enum() => {
(cx.tcx.hir().span_if_local(def.did), !def.variants.is_empty())
}
_ => (None, false),
let non_empty_enum = match scrut_ty.kind {
ty::Adt(def, _) => def.is_enum() && !def.variants.is_empty(),
_ => false,
};

if non_empty_enum {
Expand All @@ -478,9 +476,7 @@ fn check_exhaustive<'p, 'tcx>(
"ensure that all possible cases are being handled, \
possibly by adding wildcards or more match arms",
);
if let Some(sp) = def_span {
err.span_label(sp, format!("`{}` defined here", scrut_ty));
}
adt_defined_here(cx, &mut err, scrut_ty, &[]);
err.emit();
return;
}
Expand Down
Expand Up @@ -9,8 +9,13 @@ LL | match uninhab_ref() {
error[E0004]: non-exhaustive patterns: type `Foo` is non-empty
--> $DIR/always-inhabited-union-ref.rs:27:11
|
LL | match uninhab_union() {
| ^^^^^^^^^^^^^^^
LL | / pub union Foo {
LL | | foo: !,
LL | | }
| |_- `Foo` defined here
...
LL | match uninhab_union() {
| ^^^^^^^^^^^^^^^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

Expand Down
Expand Up @@ -39,6 +39,9 @@ LL | match_empty!(0u8);
error[E0004]: non-exhaustive patterns: type `NonEmptyStruct` is non-empty
--> $DIR/match-empty-exhaustive_patterns.rs:66:18
|
LL | struct NonEmptyStruct(bool);
| ---------------------------- `NonEmptyStruct` defined here
...
LL | match_empty!(NonEmptyStruct(true));
| ^^^^^^^^^^^^^^^^^^^^
|
Expand All @@ -47,16 +50,27 @@ LL | match_empty!(NonEmptyStruct(true));
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion1` is non-empty
--> $DIR/match-empty-exhaustive_patterns.rs:68:18
|
LL | match_empty!((NonEmptyUnion1 { foo: () }));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | / union NonEmptyUnion1 {
LL | | foo: (),
LL | | }
| |_- `NonEmptyUnion1` defined here
...
LL | match_empty!((NonEmptyUnion1 { foo: () }));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error[E0004]: non-exhaustive patterns: type `NonEmptyUnion2` is non-empty
--> $DIR/match-empty-exhaustive_patterns.rs:70:18
|
LL | match_empty!((NonEmptyUnion2 { foo: () }));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | / union NonEmptyUnion2 {
LL | | foo: (),
LL | | bar: (),
LL | | }
| |_- `NonEmptyUnion2` defined here
...
LL | match_empty!((NonEmptyUnion2 { foo: () }));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

Expand Down
22 changes: 18 additions & 4 deletions src/test/ui/pattern/usefulness/match-empty.stderr
Expand Up @@ -20,6 +20,9 @@ LL | match_empty!(0u8);
error[E0004]: non-exhaustive patterns: type `NonEmptyStruct` is non-empty
--> $DIR/match-empty.rs:65:18
|
LL | struct NonEmptyStruct(bool);
| ---------------------------- `NonEmptyStruct` defined here
...
LL | match_empty!(NonEmptyStruct(true));
| ^^^^^^^^^^^^^^^^^^^^
|
Expand All @@ -28,16 +31,27 @@ LL | match_empty!(NonEmptyStruct(true));
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion1` is non-empty
--> $DIR/match-empty.rs:67:18
|
LL | match_empty!((NonEmptyUnion1 { foo: () }));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | / union NonEmptyUnion1 {
LL | | foo: (),
LL | | }
| |_- `NonEmptyUnion1` defined here
...
LL | match_empty!((NonEmptyUnion1 { foo: () }));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

error[E0004]: non-exhaustive patterns: type `NonEmptyUnion2` is non-empty
--> $DIR/match-empty.rs:69:18
|
LL | match_empty!((NonEmptyUnion2 { foo: () }));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | / union NonEmptyUnion2 {
LL | | foo: (),
LL | | bar: (),
LL | | }
| |_- `NonEmptyUnion2` defined here
...
LL | match_empty!((NonEmptyUnion2 { foo: () }));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms

Expand Down

0 comments on commit 40f434b

Please sign in to comment.