From 62867b499291a69f5df16a60228c6bcedc5e4225 Mon Sep 17 00:00:00 2001 From: Knium_ Date: Wed, 30 Jan 2019 13:50:44 +0900 Subject: [PATCH] Suggest to add each of `|` and `()` when unexpected `,` is found in pattern --- src/libsyntax/parse/parser.rs | 7 ++- ...92-tuple-destructure-missing-parens.stderr | 60 +++++++++++++++++-- 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 65572102c5981..514b2952c5036 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -4381,9 +4381,14 @@ impl<'a> Parser<'a> { if let Ok(seq_snippet) = self.sess.source_map().span_to_snippet(seq_span) { err.span_suggestion( seq_span, - "try adding parentheses", + "try adding parentheses to match on a tuple..", format!("({})", seq_snippet), Applicability::MachineApplicable + ).span_suggestion( + seq_span, + "..or a vertical bar to match on multiple alternatives", + format!("{}", seq_snippet.replace(",", " |")), + Applicability::MachineApplicable ); } return Err(err); diff --git a/src/test/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.stderr b/src/test/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.stderr index 9df881e8e26d9..8099c3c0584fc 100644 --- a/src/test/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.stderr +++ b/src/test/ui/did_you_mean/issue-48492-tuple-destructure-missing-parens.stderr @@ -2,37 +2,85 @@ error: unexpected `,` in pattern --> $DIR/issue-48492-tuple-destructure-missing-parens.rs:38:17 | LL | while let b1, b2, b3 = reading_frame.next().expect("there should be a start codon") { - | --^------- help: try adding parentheses: `(b1, b2, b3)` + | ^ +help: try adding parentheses to match on a tuple.. + | +LL | while let (b1, b2, b3) = reading_frame.next().expect("there should be a start codon") { + | ^^^^^^^^^^^^ +help: ..or a vertical bar to match on multiple alternatives + | +LL | while let b1 | b2 | b3 = reading_frame.next().expect("there should be a start codon") { + | ^^^^^^^^^^^^ error: unexpected `,` in pattern --> $DIR/issue-48492-tuple-destructure-missing-parens.rs:49:14 | LL | if let b1, b2, b3 = reading_frame.next().unwrap() { - | --^------- help: try adding parentheses: `(b1, b2, b3)` + | ^ +help: try adding parentheses to match on a tuple.. + | +LL | if let (b1, b2, b3) = reading_frame.next().unwrap() { + | ^^^^^^^^^^^^ +help: ..or a vertical bar to match on multiple alternatives + | +LL | if let b1 | b2 | b3 = reading_frame.next().unwrap() { + | ^^^^^^^^^^^^ error: unexpected `,` in pattern --> $DIR/issue-48492-tuple-destructure-missing-parens.rs:59:28 | LL | Nucleotide::Adenine, Nucleotide::Cytosine, _ => true - | -------------------^------------------------ help: try adding parentheses: `(Nucleotide::Adenine, Nucleotide::Cytosine, _)` + | ^ +help: try adding parentheses to match on a tuple.. + | +LL | (Nucleotide::Adenine, Nucleotide::Cytosine, _) => true + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: ..or a vertical bar to match on multiple alternatives + | +LL | Nucleotide::Adenine | Nucleotide::Cytosine | _ => true + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: unexpected `,` in pattern --> $DIR/issue-48492-tuple-destructure-missing-parens.rs:67:10 | LL | for x, _barr_body in women.iter().map(|woman| woman.allosomes.clone()) { - | -^----------- help: try adding parentheses: `(x, _barr_body)` + | ^ +help: try adding parentheses to match on a tuple.. + | +LL | for (x, _barr_body) in women.iter().map(|woman| woman.allosomes.clone()) { + | ^^^^^^^^^^^^^^^ +help: ..or a vertical bar to match on multiple alternatives + | +LL | for x | _barr_body in women.iter().map(|woman| woman.allosomes.clone()) { + | ^^^^^^^^^^^^^^ error: unexpected `,` in pattern --> $DIR/issue-48492-tuple-destructure-missing-parens.rs:75:10 | LL | for x, y @ Allosome::Y(_) in men.iter().map(|man| man.allosomes.clone()) { - | -^------------------- help: try adding parentheses: `(x, y @ Allosome::Y(_))` + | ^ +help: try adding parentheses to match on a tuple.. + | +LL | for (x, y @ Allosome::Y(_)) in men.iter().map(|man| man.allosomes.clone()) { + | ^^^^^^^^^^^^^^^^^^^^^^^ +help: ..or a vertical bar to match on multiple alternatives + | +LL | for x | y @ Allosome::Y(_) in men.iter().map(|man| man.allosomes.clone()) { + | ^^^^^^^^^^^^^^^^^^^^^^ error: unexpected `,` in pattern --> $DIR/issue-48492-tuple-destructure-missing-parens.rs:84:14 | LL | let women, men: (Vec, Vec) = genomes.iter().cloned() - | -----^---- help: try adding parentheses: `(women, men)` + | ^ +help: try adding parentheses to match on a tuple.. + | +LL | let (women, men): (Vec, Vec) = genomes.iter().cloned() + | ^^^^^^^^^^^^ +help: ..or a vertical bar to match on multiple alternatives + | +LL | let women | men: (Vec, Vec) = genomes.iter().cloned() + | ^^^^^^^^^^^ error: aborting due to 6 previous errors