Skip to content

Commit

Permalink
Only suggest .. if more than one field is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
camelid committed Jan 13, 2021
1 parent fe82cc3 commit 9959d6d
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 135 deletions.
33 changes: 18 additions & 15 deletions compiler/rustc_typeck/src/check/pat.rs
Expand Up @@ -1061,27 +1061,30 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
wildcard_sugg = String::from(", ") + &wildcard_sugg;
}

err.span_suggestion(
err.span_suggestion_short(
after_fields_span,
"use `_` to explicitly ignore each field",
wildcard_sugg,
Applicability::MaybeIncorrect,
);

if subpats.is_empty() || all_wildcards {
err.span_suggestion(
all_fields_span,
"use `..` to ignore all fields",
String::from(".."),
Applicability::MaybeIncorrect,
);
} else {
err.span_suggestion(
after_fields_span,
"use `..` to ignore the rest of the fields",
String::from(", .."),
Applicability::MaybeIncorrect,
);
// Only suggest `..` if more than one field is missing.
if fields.len() - subpats.len() > 1 {
if subpats.is_empty() || all_wildcards {
err.span_suggestion_short(
all_fields_span,
"use `..` to ignore all fields",
String::from(".."),
Applicability::MaybeIncorrect,
);
} else {
err.span_suggestion_short(
after_fields_span,
"use `..` to ignore the rest of the fields",
String::from(", .."),
Applicability::MaybeIncorrect,
);
}
}
}

Expand Down
Expand Up @@ -30,16 +30,10 @@ LL | struct TupleStruct<S, T>(S, T);
| ------------------------------- tuple struct defined here
...
LL | TupleStruct(_) = TupleStruct(1, 2);
| ^^^^^^^^^^^^^^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
LL | TupleStruct(_, _) = TupleStruct(1, 2);
| ^^^
help: use `..` to ignore all fields
|
LL | TupleStruct(..) = TupleStruct(1, 2);
| ^^
| ^^^^^^^^^^^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 2 fields, found 1

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 @@ -57,16 +51,10 @@ LL | SingleVariant(S, T)
| ------------------- tuple variant defined here
...
LL | Enum::SingleVariant(_) = Enum::SingleVariant(1, 2);
| ^^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 1
|
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);
| ^^
| ^^^^^^^^^^^^^^^^^^^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 2 fields, found 1

error[E0070]: invalid left-hand side of assignment
--> $DIR/tuple_struct_destructure_fail.rs:40:12
Expand Down
14 changes: 4 additions & 10 deletions src/test/ui/error-codes/E0023.stderr
Expand Up @@ -5,16 +5,10 @@ LL | Apple(String, String),
| --------------------- tuple variant defined here
...
LL | Fruit::Apple(a) => {},
| ^^^^^^^^^^^^^^^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
LL | Fruit::Apple(a, _) => {},
| ^^^
help: use `..` to ignore the rest of the fields
|
LL | Fruit::Apple(a, ..) => {},
| ^^^^
| ^^^^^^^^^^^^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 2 fields, found 1

error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
--> $DIR/E0023.rs:12:9
Expand Down
Expand Up @@ -16,16 +16,10 @@ LL | struct P<T>(T); // 1 type parameter wanted
| --------------- tuple struct defined here
...
LL | let P() = U {};
| ^^^ expected 1 field, found 0
|
help: use `_` to explicitly ignore each field
|
LL | let P(_) = U {};
| ^
help: use `..` to ignore all fields
|
LL | let P(..) = U {};
| ^^
| ^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 1 field, found 0

error: aborting due to 2 previous errors

Expand Down
14 changes: 4 additions & 10 deletions src/test/ui/issues/issue-72574-2.stderr
Expand Up @@ -25,16 +25,10 @@ LL | struct Binder(i32, i32, i32);
| ----------------------------- tuple struct defined here
...
LL | Binder(_a, _x @ ..) => {}
| ^^^^^^^^^^^^^^^^^^^ expected 3 fields, found 2
|
help: use `_` to explicitly ignore each field
|
LL | Binder(_a, _x @ .., _) => {}
| ^^^
help: use `..` to ignore the rest of the fields
|
LL | Binder(_a, _x @ .., ..) => {}
| ^^^^
| ^^^^^^^^^^^^^^^^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 3 fields, found 2

error: aborting due to 3 previous errors

Expand Down
14 changes: 4 additions & 10 deletions src/test/ui/match/match-pattern-field-mismatch.stderr
Expand Up @@ -5,16 +5,10 @@ LL | Rgb(usize, usize, usize),
| ------------------------ tuple variant defined here
...
LL | Color::Rgb(_, _) => { }
| ^^^^^^^^^^^^^^^^ expected 3 fields, found 2
|
help: use `_` to explicitly ignore each field
|
LL | Color::Rgb(_, _, _) => { }
| ^^^
help: use `..` to ignore all fields
|
LL | Color::Rgb(..) => { }
| ^^
| ^^^^^^^^^^^^^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 3 fields, found 2

error: aborting due to previous error

Expand Down
14 changes: 4 additions & 10 deletions src/test/ui/pattern/issue-74539.stderr
Expand Up @@ -25,16 +25,10 @@ LL | A(u8, u8),
| --------- tuple variant defined here
...
LL | E::A(x @ ..) => {
| ^^^^^^^^^^^^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
LL | E::A(x @ .., _) => {
| ^^^
help: use `..` to ignore the rest of the fields
|
LL | E::A(x @ .., ..) => {
| ^^^^
| ^^^^^^^^^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 2 fields, found 1

error: aborting due to 3 previous errors

Expand Down
4 changes: 0 additions & 4 deletions src/test/ui/pattern/pat-tuple-underfield.rs
Expand Up @@ -8,13 +8,11 @@ fn main() {
S(x) => {}
//~^ 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 the rest of the fields
}
match S(0, 1.0) {
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 @@ -27,13 +25,11 @@ fn main() {
E::S(x) => {}
//~^ 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 the rest of the fields
}
match E::S(0, 1.0) {
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
68 changes: 22 additions & 46 deletions src/test/ui/pattern/pat-tuple-underfield.stderr
@@ -1,5 +1,5 @@
error[E0532]: expected unit struct, unit variant or constant, found tuple variant `E::S`
--> $DIR/pat-tuple-underfield.rs:45:9
--> $DIR/pat-tuple-underfield.rs:41:9
|
LL | S(i32, f32),
| ----------- `E::S` defined here
Expand All @@ -14,37 +14,25 @@ LL | struct S(i32, f32);
| ------------------- tuple struct defined here
...
LL | S(x) => {}
| ^^^^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
LL | S(x, _) => {}
| ^^^
help: use `..` to ignore the rest of the fields
|
LL | S(x, ..) => {}
| ^^^^
| ^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 2 fields, found 1

error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 2 fields
--> $DIR/pat-tuple-underfield.rs:14:9
--> $DIR/pat-tuple-underfield.rs:13:9
|
LL | struct S(i32, f32);
| ------------------- tuple struct defined here
...
LL | S(_) => {}
| ^^^^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
LL | S(_, _) => {}
| ^^^
help: use `..` to ignore all fields
|
LL | S(..) => {}
| ^^
| ^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 2 fields, found 1

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

error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
--> $DIR/pat-tuple-underfield.rs:27:9
--> $DIR/pat-tuple-underfield.rs:25:9
|
LL | S(i32, f32),
| ----------- tuple variant defined here
...
LL | E::S(x) => {}
| ^^^^^^^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
LL | E::S(x, _) => {}
| ^^^
help: use `..` to ignore the rest of the fields
|
LL | E::S(x, ..) => {}
| ^^^^
| ^^^^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 2 fields, found 1

error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
--> $DIR/pat-tuple-underfield.rs:33:9
--> $DIR/pat-tuple-underfield.rs:30:9
|
LL | S(i32, f32),
| ----------- tuple variant defined here
...
LL | E::S(_) => {}
| ^^^^^^^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
LL | E::S(_, _) => {}
| ^^^
help: use `..` to ignore all fields
|
LL | E::S(..) => {}
| ^^
| ^^^^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 2 fields, found 1

error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 2 fields
--> $DIR/pat-tuple-underfield.rs:39:9
--> $DIR/pat-tuple-underfield.rs:35:9
|
LL | S(i32, f32),
| ----------- tuple variant defined here
Expand Down

0 comments on commit 9959d6d

Please sign in to comment.