Skip to content

Commit e8c8793

Browse files
committed
Include .. suggestion if fields are all wildcards
1 parent d7307a7 commit e8c8793

File tree

6 files changed

+35
-8
lines changed

6 files changed

+35
-8
lines changed

compiler/rustc_typeck/src/check/pat.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1068,8 +1068,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10681068
Applicability::MaybeIncorrect,
10691069
);
10701070

1071-
// Only suggest `..` if more than one field is missing.
1072-
if fields.len() - subpats.len() > 1 {
1071+
// Only suggest `..` if more than one field is missing
1072+
// or the pattern consists of all wildcards.
1073+
if fields.len() - subpats.len() > 1 || all_wildcards {
10731074
if subpats.is_empty() || all_wildcards {
10741075
err.span_suggestion_verbose(
10751076
all_fields_span,

src/test/ui/destructuring-assignment/tuple_struct_destructure_fail.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ help: use `_` to explicitly ignore each field
3636
|
3737
LL | TupleStruct(_, _) = TupleStruct(1, 2);
3838
| ^^^
39+
help: use `..` to ignore all fields
40+
|
41+
LL | TupleStruct(..) = TupleStruct(1, 2);
42+
| ^^
3943

4044
error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
4145
--> $DIR/tuple_struct_destructure_fail.rs:34:5
@@ -59,6 +63,10 @@ help: use `_` to explicitly ignore each field
5963
|
6064
LL | Enum::SingleVariant(_, _) = Enum::SingleVariant(1, 2);
6165
| ^^^
66+
help: use `..` to ignore all fields
67+
|
68+
LL | Enum::SingleVariant(..) = Enum::SingleVariant(1, 2);
69+
| ^^
6270

6371
error[E0070]: invalid left-hand side of assignment
6472
--> $DIR/tuple_struct_destructure_fail.rs:40:12

src/test/ui/issues/issue-67037-pat-tup-scrut-ty-diff-less-fields.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ help: use `_` to explicitly ignore each field
2222
|
2323
LL | let P(_) = U {};
2424
| ^
25+
help: use `..` to ignore all fields
26+
|
27+
LL | let P(..) = U {};
28+
| ^^
2529

2630
error: aborting due to 2 previous errors
2731

src/test/ui/match/match-pattern-field-mismatch.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ help: use `_` to explicitly ignore each field
1111
|
1212
LL | Color::Rgb(_, _, _) => { }
1313
| ^^^
14+
help: use `..` to ignore all fields
15+
|
16+
LL | Color::Rgb(..) => { }
17+
| ^^
1418

1519
error: aborting due to previous error
1620

src/test/ui/pattern/pat-tuple-underfield.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fn main() {
1414
S(_) => {}
1515
//~^ ERROR this pattern has 1 field, but the corresponding tuple struct has 2 fields
1616
//~| HELP use `_` to explicitly ignore each field
17+
//~| HELP use `..` to ignore all fields
1718
}
1819
match S(0, 1.0) {
1920
S() => {}
@@ -31,6 +32,7 @@ fn main() {
3132
E::S(_) => {}
3233
//~^ ERROR this pattern has 1 field, but the corresponding tuple variant has 2 fields
3334
//~| HELP use `_` to explicitly ignore each field
35+
//~| HELP use `..` to ignore all fields
3436
}
3537
match E::S(0, 1.0) {
3638
E::S() => {}

src/test/ui/pattern/pat-tuple-underfield.stderr

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0532]: expected unit struct, unit variant or constant, found tuple variant `E::S`
2-
--> $DIR/pat-tuple-underfield.rs:42:9
2+
--> $DIR/pat-tuple-underfield.rs:44:9
33
|
44
LL | S(i32, f32),
55
| ----------- `E::S` defined here
@@ -34,9 +34,13 @@ help: use `_` to explicitly ignore each field
3434
|
3535
LL | S(_, _) => {}
3636
| ^^^
37+
help: use `..` to ignore all fields
38+
|
39+
LL | S(..) => {}
40+
| ^^
3741

3842
error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has 2 fields
39-
--> $DIR/pat-tuple-underfield.rs:19:9
43+
--> $DIR/pat-tuple-underfield.rs:20:9
4044
|
4145
LL | struct S(i32, f32);
4246
| ------------------- tuple struct defined here
@@ -54,7 +58,7 @@ LL | S(..) => {}
5458
| ^^
5559

5660
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
57-
--> $DIR/pat-tuple-underfield.rs:26:9
61+
--> $DIR/pat-tuple-underfield.rs:27:9
5862
|
5963
LL | S(i32, f32),
6064
| ----------- tuple variant defined here
@@ -68,7 +72,7 @@ LL | E::S(x, _) => {}
6872
| ^^^
6973

7074
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
71-
--> $DIR/pat-tuple-underfield.rs:31:9
75+
--> $DIR/pat-tuple-underfield.rs:32:9
7276
|
7377
LL | S(i32, f32),
7478
| ----------- tuple variant defined here
@@ -80,9 +84,13 @@ help: use `_` to explicitly ignore each field
8084
|
8185
LL | E::S(_, _) => {}
8286
| ^^^
87+
help: use `..` to ignore all fields
88+
|
89+
LL | E::S(..) => {}
90+
| ^^
8391

8492
error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 2 fields
85-
--> $DIR/pat-tuple-underfield.rs:36:9
93+
--> $DIR/pat-tuple-underfield.rs:38:9
8694
|
8795
LL | S(i32, f32),
8896
| ----------- tuple variant defined here
@@ -100,7 +108,7 @@ LL | E::S(..) => {}
100108
| ^^
101109

102110
error[E0023]: this pattern has 2 fields, but the corresponding tuple struct has 4 fields
103-
--> $DIR/pat-tuple-underfield.rs:48:9
111+
--> $DIR/pat-tuple-underfield.rs:50:9
104112
|
105113
LL | struct Point4(i32, i32, i32, i32);
106114
| ---------------------------------- tuple struct defined here

0 commit comments

Comments
 (0)