Detect tuple struct incorrectly used as struct pat

This commit is contained in:
Esteban Küber 2020-07-08 19:39:26 -07:00
parent 346aec9b02
commit 5daedea3db
10 changed files with 137 additions and 49 deletions

View file

@ -450,6 +450,7 @@ E0765: include_str!("./error_codes/E0765.md"),
E0766: include_str!("./error_codes/E0766.md"),
E0767: include_str!("./error_codes/E0767.md"),
E0768: include_str!("./error_codes/E0768.md"),
E0769: include_str!("./error_codes/E0769.md"),
;
// E0006, // merged with E0005
// E0008, // cannot bind by-move into a pattern guard

View file

@ -0,0 +1,39 @@
A tuple struct or tuple variant was used in a pattern as if it were a
struct or struct variant.
Erroneous code example:
```compile_fail,E0769
enum E {
A(i32),
}
let e = E::A(42);
match e {
E::A { number } => println!("{}", x),
}
```
To fix this error, you can use the tuple pattern:
```
# enum E {
# A(i32),
# }
# let e = E::A(42);
match e {
E::A(number) => println!("{}", number),
}
```
Alternatively, you can also use the struct pattern by using the correct
field names and binding them to new identifiers:
```
# enum E {
# A(i32),
# }
# let e = E::A(42);
match e {
E::A { 0: number } => println!("{}", number),
}
```