Skip to content

Commit 40f434b

Browse files
committed
Reuse adt_defined_here
1 parent d289f55 commit 40f434b

File tree

4 files changed

+47
-18
lines changed

4 files changed

+47
-18
lines changed

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -459,11 +459,9 @@ fn check_exhaustive<'p, 'tcx>(
459459
return;
460460
} else {
461461
// We know the type is inhabited, so this must be wrong
462-
let (def_span, non_empty_enum) = match scrut_ty.kind {
463-
ty::Adt(def, _) if def.is_enum() => {
464-
(cx.tcx.hir().span_if_local(def.did), !def.variants.is_empty())
465-
}
466-
_ => (None, false),
462+
let non_empty_enum = match scrut_ty.kind {
463+
ty::Adt(def, _) => def.is_enum() && !def.variants.is_empty(),
464+
_ => false,
467465
};
468466

469467
if non_empty_enum {
@@ -478,9 +476,7 @@ fn check_exhaustive<'p, 'tcx>(
478476
"ensure that all possible cases are being handled, \
479477
possibly by adding wildcards or more match arms",
480478
);
481-
if let Some(sp) = def_span {
482-
err.span_label(sp, format!("`{}` defined here", scrut_ty));
483-
}
479+
adt_defined_here(cx, &mut err, scrut_ty, &[]);
484480
err.emit();
485481
return;
486482
}

src/test/ui/pattern/usefulness/always-inhabited-union-ref.stderr

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ LL | match uninhab_ref() {
99
error[E0004]: non-exhaustive patterns: type `Foo` is non-empty
1010
--> $DIR/always-inhabited-union-ref.rs:27:11
1111
|
12-
LL | match uninhab_union() {
13-
| ^^^^^^^^^^^^^^^
12+
LL | / pub union Foo {
13+
LL | | foo: !,
14+
LL | | }
15+
| |_- `Foo` defined here
16+
...
17+
LL | match uninhab_union() {
18+
| ^^^^^^^^^^^^^^^
1419
|
1520
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1621

src/test/ui/pattern/usefulness/match-empty-exhaustive_patterns.stderr

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ LL | match_empty!(0u8);
3939
error[E0004]: non-exhaustive patterns: type `NonEmptyStruct` is non-empty
4040
--> $DIR/match-empty-exhaustive_patterns.rs:66:18
4141
|
42+
LL | struct NonEmptyStruct(bool);
43+
| ---------------------------- `NonEmptyStruct` defined here
44+
...
4245
LL | match_empty!(NonEmptyStruct(true));
4346
| ^^^^^^^^^^^^^^^^^^^^
4447
|
@@ -47,16 +50,27 @@ LL | match_empty!(NonEmptyStruct(true));
4750
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion1` is non-empty
4851
--> $DIR/match-empty-exhaustive_patterns.rs:68:18
4952
|
50-
LL | match_empty!((NonEmptyUnion1 { foo: () }));
51-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
53+
LL | / union NonEmptyUnion1 {
54+
LL | | foo: (),
55+
LL | | }
56+
| |_- `NonEmptyUnion1` defined here
57+
...
58+
LL | match_empty!((NonEmptyUnion1 { foo: () }));
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5260
|
5361
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
5462

5563
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion2` is non-empty
5664
--> $DIR/match-empty-exhaustive_patterns.rs:70:18
5765
|
58-
LL | match_empty!((NonEmptyUnion2 { foo: () }));
59-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66+
LL | / union NonEmptyUnion2 {
67+
LL | | foo: (),
68+
LL | | bar: (),
69+
LL | | }
70+
| |_- `NonEmptyUnion2` defined here
71+
...
72+
LL | match_empty!((NonEmptyUnion2 { foo: () }));
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6074
|
6175
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
6276

src/test/ui/pattern/usefulness/match-empty.stderr

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ LL | match_empty!(0u8);
2020
error[E0004]: non-exhaustive patterns: type `NonEmptyStruct` is non-empty
2121
--> $DIR/match-empty.rs:65:18
2222
|
23+
LL | struct NonEmptyStruct(bool);
24+
| ---------------------------- `NonEmptyStruct` defined here
25+
...
2326
LL | match_empty!(NonEmptyStruct(true));
2427
| ^^^^^^^^^^^^^^^^^^^^
2528
|
@@ -28,16 +31,27 @@ LL | match_empty!(NonEmptyStruct(true));
2831
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion1` is non-empty
2932
--> $DIR/match-empty.rs:67:18
3033
|
31-
LL | match_empty!((NonEmptyUnion1 { foo: () }));
32-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
34+
LL | / union NonEmptyUnion1 {
35+
LL | | foo: (),
36+
LL | | }
37+
| |_- `NonEmptyUnion1` defined here
38+
...
39+
LL | match_empty!((NonEmptyUnion1 { foo: () }));
40+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3341
|
3442
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
3543

3644
error[E0004]: non-exhaustive patterns: type `NonEmptyUnion2` is non-empty
3745
--> $DIR/match-empty.rs:69:18
3846
|
39-
LL | match_empty!((NonEmptyUnion2 { foo: () }));
40-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
47+
LL | / union NonEmptyUnion2 {
48+
LL | | foo: (),
49+
LL | | bar: (),
50+
LL | | }
51+
| |_- `NonEmptyUnion2` defined here
52+
...
53+
LL | match_empty!((NonEmptyUnion2 { foo: () }));
54+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4155
|
4256
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
4357

0 commit comments

Comments
 (0)