Skip to content

Do not suggest using | in let assignments #61643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
@@ -3257,7 +3257,7 @@ impl<'a> Parser<'a> {
mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
// Parse: `for <src_pat> in <src_expr> <src_loop_block>`

let pat = self.parse_top_level_pat()?;
let pat = self.parse_top_level_pat(false)?;
if !self.eat_keyword(kw::In) {
let in_span = self.prev_span.between(self.span);
let mut err = self.sess.span_diagnostic
@@ -3509,7 +3509,7 @@ impl<'a> Parser<'a> {

let mut pats = Vec::new();
loop {
pats.push(self.parse_top_level_pat()?);
pats.push(self.parse_top_level_pat(true)?);

if self.token == token::OrOr {
let mut err = self.struct_span_err(self.span,
@@ -3860,7 +3860,7 @@ impl<'a> Parser<'a> {
/// A wrapper around `parse_pat` with some special error handling for the
/// "top-level" patterns in a match arm, `for` loop, `let`, &c. (in contrast
/// to subpatterns within such).
fn parse_top_level_pat(&mut self) -> PResult<'a, P<Pat>> {
fn parse_top_level_pat(&mut self, is_match_arg: bool) -> PResult<'a, P<Pat>> {
let pat = self.parse_pat(None)?;
if self.token == token::Comma {
// An unexpected comma after a top-level pattern is a clue that the
@@ -3877,20 +3877,29 @@ impl<'a> Parser<'a> {
err.cancel();
}
let seq_span = pat.span.to(self.prev_span);
let mut err = self.struct_span_err(comma_span,
"unexpected `,` in pattern");
let mut err = self.struct_span_err(
comma_span,
"unexpected `,` in pattern",
);
if let Ok(seq_snippet) = self.sess.source_map().span_to_snippet(seq_span) {
err.span_suggestion(
seq_span,
"try adding parentheses to match on a tuple..",
&format!("try adding parentheses to match on a tuple{}", if is_match_arg {
"..."
} else {
""
}),
format!("({})", seq_snippet),
Applicability::MachineApplicable
).span_suggestion(
seq_span,
"..or a vertical bar to match on multiple alternatives",
format!("{}", seq_snippet.replace(",", " |")),
Applicability::MachineApplicable
);
if is_match_arg {
err.span_suggestion(
seq_span,
"...or a vertical bar to match on multiple alternatives",
format!("{}", seq_snippet.replace(",", " |")),
Applicability::MachineApplicable
);
}
}
return Err(err);
}
@@ -4149,7 +4158,7 @@ impl<'a> Parser<'a> {
/// Parses a local variable declaration.
fn parse_local(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Local>> {
let lo = self.prev_span;
let pat = self.parse_top_level_pat()?;
let pat = self.parse_top_level_pat(false)?;

let (err, ty) = if self.eat(&token::Colon) {
// Save the state of the parser before parsing type normally, in case there is a `:`
Original file line number Diff line number Diff line change
@@ -3,11 +3,11 @@ error: unexpected `,` in pattern
|
LL | while let b1, b2, b3 = reading_frame.next().expect("there should be a start codon") {
| ^
help: try adding parentheses to match on a tuple..
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
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") {
| ^^^^^^^^^^^^
@@ -17,11 +17,11 @@ error: unexpected `,` in pattern
|
LL | if let b1, b2, b3 = reading_frame.next().unwrap() {
| ^
help: try adding parentheses to match on a tuple..
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
help: ...or a vertical bar to match on multiple alternatives
|
LL | if let b1 | b2 | b3 = reading_frame.next().unwrap() {
| ^^^^^^^^^^^^
@@ -31,11 +31,11 @@ error: unexpected `,` in pattern
|
LL | Nucleotide::Adenine, Nucleotide::Cytosine, _ => true
| ^
help: try adding parentheses to match on a tuple..
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
help: ...or a vertical bar to match on multiple alternatives
|
LL | Nucleotide::Adenine | Nucleotide::Cytosine | _ => true
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -44,43 +44,19 @@ 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 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()) {
| ^^^^^^^^^^^^^^
| -^----------- help: try adding parentheses to match on a tuple: `(x, _barr_body)`

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 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()) {
| ^^^^^^^^^^^^^^^^^^^^^^
| -^------------------- help: try adding parentheses to match on a tuple: `(x, y @ Allosome::Y(_))`

error: unexpected `,` in pattern
--> $DIR/issue-48492-tuple-destructure-missing-parens.rs:84:14
|
LL | let women, men: (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
| ^
help: try adding parentheses to match on a tuple..
|
LL | let (women, men): (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
| ^^^^^^^^^^^^
help: ..or a vertical bar to match on multiple alternatives
|
LL | let women | men: (Vec<Genome>, Vec<Genome>) = genomes.iter().cloned()
| ^^^^^^^^^^^
| -----^---- help: try adding parentheses to match on a tuple: `(women, men)`

error: aborting due to 6 previous errors

5 changes: 5 additions & 0 deletions src/test/ui/suggestions/issue-61573.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
let a, b = x;
//~^ ERROR unexpected `,` in pattern
//~| HELP try adding parentheses to match on a tuple
}
8 changes: 8 additions & 0 deletions src/test/ui/suggestions/issue-61573.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: unexpected `,` in pattern
--> $DIR/issue-61573.rs:2:10
|
LL | let a, b = x;
| -^-- help: try adding parentheses to match on a tuple: `(a, b)`

error: aborting due to previous error