Skip to content

Commit

Permalink
Include .. suggestion if fields are all wildcards
Browse files Browse the repository at this point in the history
  • Loading branch information
camelid committed Jan 13, 2021
1 parent d7307a7 commit e8c8793
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 8 deletions.
5 changes: 3 additions & 2 deletions compiler/rustc_typeck/src/check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,8 +1068,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Applicability::MaybeIncorrect,
);

// Only suggest `..` if more than one field is missing.
if fields.len() - subpats.len() > 1 {
// Only suggest `..` if more than one field is missing
// or the pattern consists of all wildcards.
if fields.len() - subpats.len() > 1 || all_wildcards {
if subpats.is_empty() || all_wildcards {
err.span_suggestion_verbose(
all_fields_span,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ help: use `_` to explicitly ignore each field
|
LL | TupleStruct(_, _) = TupleStruct(1, 2);
| ^^^
help: use `..` to ignore all fields
|
LL | TupleStruct(..) = TupleStruct(1, 2);
| ^^

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

error[E0070]: invalid left-hand side of assignment
--> $DIR/tuple_struct_destructure_fail.rs:40:12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ help: use `_` to explicitly ignore each field
|
LL | let P(_) = U {};
| ^
help: use `..` to ignore all fields
|
LL | let P(..) = U {};
| ^^

error: aborting due to 2 previous errors

Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/match/match-pattern-field-mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ help: use `_` to explicitly ignore each field
|
LL | Color::Rgb(_, _, _) => { }
| ^^^
help: use `..` to ignore all fields
|
LL | Color::Rgb(..) => { }
| ^^

error: aborting due to previous error

Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/pattern/pat-tuple-underfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fn main() {
S(_) => {}
//~^ ERROR this pattern has 1 field, but the corresponding tuple struct has 2 fields
//~| HELP use `_` to explicitly ignore each field
//~| HELP use `..` to ignore all fields
}
match S(0, 1.0) {
S() => {}
Expand All @@ -31,6 +32,7 @@ fn main() {
E::S(_) => {}
//~^ ERROR this pattern has 1 field, but the corresponding tuple variant has 2 fields
//~| HELP use `_` to explicitly ignore each field
//~| HELP use `..` to ignore all fields
}
match E::S(0, 1.0) {
E::S() => {}
Expand Down
20 changes: 14 additions & 6 deletions src/test/ui/pattern/pat-tuple-underfield.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0532]: expected unit struct, unit variant or constant, found tuple variant `E::S`
--> $DIR/pat-tuple-underfield.rs:42:9
--> $DIR/pat-tuple-underfield.rs:44:9
|
LL | S(i32, f32),
| ----------- `E::S` defined here
Expand Down Expand Up @@ -34,9 +34,13 @@ help: use `_` to explicitly ignore each field
|
LL | S(_, _) => {}
| ^^^
help: use `..` to ignore all fields
|
LL | S(..) => {}
| ^^

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

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

error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
--> $DIR/pat-tuple-underfield.rs:31:9
--> $DIR/pat-tuple-underfield.rs:32:9
|
LL | S(i32, f32),
| ----------- tuple variant defined here
Expand All @@ -80,9 +84,13 @@ help: use `_` to explicitly ignore each field
|
LL | E::S(_, _) => {}
| ^^^
help: use `..` to ignore all fields
|
LL | E::S(..) => {}
| ^^

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

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

0 comments on commit e8c8793

Please sign in to comment.