Auto merge of #76612 - estebank:pat-missing-fields-suggestion, r=davidtwco

Provide suggestion for missing fields in patterns
This commit is contained in:
bors 2020-09-15 00:17:13 +00:00
commit 255ceeb5ff
7 changed files with 96 additions and 38 deletions

View file

@ -1,15 +0,0 @@
// compile-flags: -Z teach
struct Dog {
name: String,
age: u32,
}
fn main() {
let d = Dog { name: "Rusty".to_string(), age: 8 };
match d {
Dog { age: x } => {}
//~^ ERROR pattern does not mention field `name`
}
}

View file

@ -1,11 +0,0 @@
error[E0027]: pattern does not mention field `name`
--> $DIR/E0027-teach.rs:12:9
|
LL | Dog { age: x } => {}
| ^^^^^^^^^^^^^^ missing field `name`
|
= note: This error indicates that a pattern for a struct fails to specify a sub-pattern for every one of the struct's fields. Ensure that each field from the struct's definition is mentioned in the pattern, or use `..` to ignore unwanted fields.
error: aborting due to previous error
For more information about this error, try `rustc --explain E0027`.

View file

@ -7,7 +7,9 @@ fn main() {
let d = Dog { name: "Rusty".to_string(), age: 8 };
match d {
Dog { age: x } => {}
//~^ ERROR pattern does not mention field `name`
Dog { age: x } => {} //~ ERROR pattern does not mention field `name`
}
match d {
Dog {} => {} //~ ERROR pattern does not mention fields `name`, `age`
}
}

View file

@ -3,7 +3,31 @@ error[E0027]: pattern does not mention field `name`
|
LL | Dog { age: x } => {}
| ^^^^^^^^^^^^^^ missing field `name`
|
help: include the missing field in the pattern
|
LL | Dog { age: x, name } => {}
| ^^^^^^
help: if you don't care about this missing field, you can explicitely ignore it
|
LL | Dog { age: x, .. } => {}
| ^^^^
error: aborting due to previous error
error[E0027]: pattern does not mention fields `name`, `age`
--> $DIR/E0027.rs:13:9
|
LL | Dog {} => {}
| ^^^^^^ missing fields `name`, `age`
|
help: include the missing fields in the pattern
|
LL | Dog { name, age } => {}
| ^^^^^^^^^^^^^
help: if you don't care about these missing fields, you can explicitely ignore them
|
LL | Dog { .. } => {}
| ^^^^^^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0027`.

View file

@ -17,6 +17,15 @@ error[E0027]: pattern does not mention field `present`
|
LL | let Foo { #[cfg(any())] present: () } = foo;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing field `present`
|
help: include the missing field in the pattern
|
LL | let Foo { present } = foo;
| ^^^^^^^^^^^
help: if you don't care about this missing field, you can explicitely ignore it
|
LL | let Foo { .. } = foo;
| ^^^^^^
error[E0026]: struct `Foo` does not have a field named `absent`
--> $DIR/struct-field-cfg.rs:16:42

View file

@ -15,6 +15,15 @@ error[E0027]: pattern does not mention fields `b`, `c`
|
LL | let A { x, y } = self.d;
| ^^^^^^^^^^ missing fields `b`, `c`
|
help: include the missing fields in the pattern
|
LL | let A { x, y, b, c } = self.d;
| ^^^^^^
help: if you don't care about these missing fields, you can explicitely ignore them
|
LL | let A { x, y, .. } = self.d;
| ^^^^
error: aborting due to 3 previous errors