Closed
Description
It'd be nice to smartly hint about missing tuple parentheses in patterns, especially in positions where parentheses nesting is required.
Example of incorrect code:
fn main() {
match Some((1, 2)) {
Some(x, y) => {}
None => {}
}
}
The error:
error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 1 field
--> src/main.rs:3:9
|
3 | Some(x, y) => {}
| ^^^^^^^^^^ expected 1 field, found 2
This error is confusing, especially since Some
is a "tuple pattern" which is confusable with its tuple field. An error that suggests missing tuple parentheses would be helpful.
This issue has been assigned to @sam09 via this comment.
Metadata
Metadata
Assignees
Labels
Area: Messages for errors, warnings, and lintsArea: Suggestions generated by the compiler applied by `cargo fix`Category: An issue proposing an enhancement or a PR with one.Call for participation: Help is requested to fix this issue.Call for participation: Medium difficulty. Experience needed to fix: Intermediate.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
Centril commentedon Aug 27, 2019
I think the right approach here would be to check if the scrutinee type (expected type in this context) has a single field of a tuple type with as many fields as the tuple struct pattern has.
Place to change:
rust/src/librustc_typeck/check/pat.rs
Lines 678 to 688 in 53df91a
If you tackle this, first please also refactor the snippet highlighted above into a diagnostics-only method and then do your changes in there.
sam09 commentedon Aug 31, 2019
Can I pick up this one? Doesn't look like anybody working on this.
zackmdavis commentedon Aug 31, 2019
@sam09 Please do.
@rustbot assign @sam09
sam09 commentedon Sep 1, 2019
variant.fields.len()
gives me the number of fields of expected type, which should be 1 here. I have a condition,varaints.fields.len()==1
. However I can't see how many fieldsvaraints.field[0]
has, when trying to compare to the fields the tuple struct pattern has (subpats.len()
in this case)Maybe I misunderstood it, or am missing something here
sam09 commentedon Sep 7, 2019
This #64161 seems to have changed some things here. Is this issue still relevant?
Centril commentedon Sep 7, 2019
cc @estebank
Ostensibly it is still relevant as we could emit a specific suggestion in this case.
estebank commentedon Sep 7, 2019
Yes, that PR only adds a bit of context that should generally be useful, but this is about adding a structured suggestion for this case in particular. This is more focused and more useful for this case in particular :)
Looking forward to it!
15 remaining items