Only suggest .. if more than one field is missing

This commit is contained in:
Camelid 2020-12-20 14:43:07 -08:00
parent fe82cc38a0
commit 9959d6deed
9 changed files with 68 additions and 135 deletions

View File

@ -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,
);
}
}
}

View File

@ -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
@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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() => {}
@ -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() => {}

View File

@ -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
@ -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
@ -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