Confusing error messages when a pattern match contains .foo() expressions #107523
Open
Description
Code
#[derive(Debug, PartialEq)]
enum Pet {
Dog(String),
}
fn main() {
let d = Pet::Dog("fido".to_string());
// Intended.
assert_eq!(d, Pet::Dog("fido".to_string()));
// What I wrote by accident.
assert!(matches!(d, Pet::Dog("fido".to_string())));
}
Current output
error: expected one of `)`, `,`, `...`, `..=`, `..`, or `|`, found `.`
--> src/main.rs:14:40
|
14 | assert!(matches!(d, Pet::Dog("fido".to_string())));
| ^
| |
| expected one of `)`, `,`, `...`, `..=`, `..`, or `|`
| help: missing `,`
error[E0531]: cannot find tuple struct or tuple variant `to_string` in this scope
--> src/main.rs:14:41
|
14 | assert!(matches!(d, Pet::Dog("fido".to_string())));
| ^^^^^^^^^ not found in this scope
error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 1 field
--> src/main.rs:14:34
|
3 | Dog(String),
| ------ tuple variant has 1 field
...
14 | assert!(matches!(d, Pet::Dog("fido".to_string())));
| ^^^^^^ ^^^^^^^^^^^ expected 1 field, found 2
Desired output
No response
Rationale and extra context
The third message is really confusing: I think rustc has assumed I intended "fido", to_string()
? It's surprising to see an error talk about a field with two patterns.
Other cases
No response
Anything else?
No response