Skip to content

Commit 62867b4

Browse files
committed
Suggest to add each of | and () when unexpected , is found in pattern
1 parent d8a0dd7 commit 62867b4

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

Diff for: src/libsyntax/parse/parser.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -4381,9 +4381,14 @@ impl<'a> Parser<'a> {
43814381
if let Ok(seq_snippet) = self.sess.source_map().span_to_snippet(seq_span) {
43824382
err.span_suggestion(
43834383
seq_span,
4384-
"try adding parentheses",
4384+
"try adding parentheses to match on a tuple..",
43854385
format!("({})", seq_snippet),
43864386
Applicability::MachineApplicable
4387+
).span_suggestion(
4388+
seq_span,
4389+
"..or a vertical bar to match on multiple alternatives",
4390+
format!("{}", seq_snippet.replace(",", " |")),
4391+
Applicability::MachineApplicable
43874392
);
43884393
}
43894394
return Err(err);

Diff for: src/test/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.stderr

+54-6
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,85 @@ error: unexpected `,` in pattern
22
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:38:17
33
|
44
LL | while let b1, b2, b3 = reading_frame.next().expect("there should be a start codon") {
5-
| --^------- help: try adding parentheses: `(b1, b2, b3)`
5+
| ^
6+
help: try adding parentheses to match on a tuple..
7+
|
8+
LL | while let (b1, b2, b3) = reading_frame.next().expect("there should be a start codon") {
9+
| ^^^^^^^^^^^^
10+
help: ..or a vertical bar to match on multiple alternatives
11+
|
12+
LL | while let b1 | b2 | b3 = reading_frame.next().expect("there should be a start codon") {
13+
| ^^^^^^^^^^^^
614

715
error: unexpected `,` in pattern
816
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:49:14
917
|
1018
LL | if let b1, b2, b3 = reading_frame.next().unwrap() {
11-
| --^------- help: try adding parentheses: `(b1, b2, b3)`
19+
| ^
20+
help: try adding parentheses to match on a tuple..
21+
|
22+
LL | if let (b1, b2, b3) = reading_frame.next().unwrap() {
23+
| ^^^^^^^^^^^^
24+
help: ..or a vertical bar to match on multiple alternatives
25+
|
26+
LL | if let b1 | b2 | b3 = reading_frame.next().unwrap() {
27+
| ^^^^^^^^^^^^
1228

1329
error: unexpected `,` in pattern
1430
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:59:28
1531
|
1632
LL | Nucleotide::Adenine, Nucleotide::Cytosine, _ => true
17-
| -------------------^------------------------ help: try adding parentheses: `(Nucleotide::Adenine, Nucleotide::Cytosine, _)`
33+
| ^
34+
help: try adding parentheses to match on a tuple..
35+
|
36+
LL | (Nucleotide::Adenine, Nucleotide::Cytosine, _) => true
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38+
help: ..or a vertical bar to match on multiple alternatives
39+
|
40+
LL | Nucleotide::Adenine | Nucleotide::Cytosine | _ => true
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1842

1943
error: unexpected `,` in pattern
2044
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:67:10
2145
|
2246
LL | for x, _barr_body in women.iter().map(|woman| woman.allosomes.clone()) {
23-
| -^----------- help: try adding parentheses: `(x, _barr_body)`
47+
| ^
48+
help: try adding parentheses to match on a tuple..
49+
|
50+
LL | for (x, _barr_body) in women.iter().map(|woman| woman.allosomes.clone()) {
51+
| ^^^^^^^^^^^^^^^
52+
help: ..or a vertical bar to match on multiple alternatives
53+
|
54+
LL | for x | _barr_body in women.iter().map(|woman| woman.allosomes.clone()) {
55+
| ^^^^^^^^^^^^^^
2456

2557
error: unexpected `,` in pattern
2658
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:75:10
2759
|
2860
LL | for x, y @ Allosome::Y(_) in men.iter().map(|man| man.allosomes.clone()) {
29-
| -^------------------- help: try adding parentheses: `(x, y @ Allosome::Y(_))`
61+
| ^
62+
help: try adding parentheses to match on a tuple..
63+
|
64+
LL | for (x, y @ Allosome::Y(_)) in men.iter().map(|man| man.allosomes.clone()) {
65+
| ^^^^^^^^^^^^^^^^^^^^^^^
66+
help: ..or a vertical bar to match on multiple alternatives
67+
|
68+
LL | for x | y @ Allosome::Y(_) in men.iter().map(|man| man.allosomes.clone()) {
69+
| ^^^^^^^^^^^^^^^^^^^^^^
3070

3171
error: unexpected `,` in pattern
3272
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:84:14
3373
|
3474
LL | let women, men: (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
35-
| -----^---- help: try adding parentheses: `(women, men)`
75+
| ^
76+
help: try adding parentheses to match on a tuple..
77+
|
78+
LL | let (women, men): (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
79+
| ^^^^^^^^^^^^
80+
help: ..or a vertical bar to match on multiple alternatives
81+
|
82+
LL | let women | men: (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
83+
| ^^^^^^^^^^^
3684

3785
error: aborting due to 6 previous errors
3886

0 commit comments

Comments
 (0)