Rollup merge of #147245 - karolzwolak:only-replace-intended-bar-not-all-in-pattern, r=lcnr

only replace the intended comma in pattern suggestions

Only suggest to replace the intended comma, not all bars in the pattern.
Fixes rust-lang/rust#143330.
This continues rust-lang/rust#143331, the credit for making the fix goes to `@A4-Tacks.` I just blessed tests and added a regression test.
This commit is contained in:
Matthias Krüger 2025-10-03 21:10:31 +02:00 committed by GitHub
commit 2e06dcdbeb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 79 additions and 30 deletions

View file

@ -2939,26 +2939,24 @@ impl<'a> Parser<'a> {
}
let seq_span = lo.to(self.prev_token.span);
let mut err = self.dcx().struct_span_err(comma_span, "unexpected `,` in pattern");
if let Ok(seq_snippet) = self.span_to_snippet(seq_span) {
err.multipart_suggestion(
format!(
"try adding parentheses to match on a tuple{}",
if let CommaRecoveryMode::LikelyTuple = rt { "" } else { "..." },
),
vec![
(seq_span.shrink_to_lo(), "(".to_string()),
(seq_span.shrink_to_hi(), ")".to_string()),
],
err.multipart_suggestion(
format!(
"try adding parentheses to match on a tuple{}",
if let CommaRecoveryMode::LikelyTuple = rt { "" } else { "..." },
),
vec![
(seq_span.shrink_to_lo(), "(".to_string()),
(seq_span.shrink_to_hi(), ")".to_string()),
],
Applicability::MachineApplicable,
);
if let CommaRecoveryMode::EitherTupleOrPipe = rt {
err.span_suggestion(
comma_span,
"...or a vertical bar to match on alternatives",
" |",
Applicability::MachineApplicable,
);
if let CommaRecoveryMode::EitherTupleOrPipe = rt {
err.span_suggestion(
seq_span,
"...or a vertical bar to match on multiple alternatives",
seq_snippet.replace(',', " |"),
Applicability::MachineApplicable,
);
}
}
Err(err)
}

View file

@ -32,10 +32,10 @@ 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 alternatives
|
LL - Nucleotide::Adenine, Nucleotide::Cytosine, _ => true
LL + Nucleotide::Adenine | Nucleotide::Cytosine | _ => true
LL + Nucleotide::Adenine | Nucleotide::Cytosine, _ => true
|
error: unexpected `,` in pattern

View file

@ -8,7 +8,7 @@ help: try adding parentheses to match on a tuple...
|
LL | (Some(_),)
| + +
help: ...or a vertical bar to match on multiple alternatives
help: ...or a vertical bar to match on alternatives
|
LL - Some(_),
LL + Some(_) |

View file

@ -17,13 +17,13 @@ fn main() {
Some(_),
//~^ ERROR unexpected `,` in pattern
//~| HELP try adding parentheses to match on a tuple
//~| HELP or a vertical bar to match on multiple alternatives
//~| HELP or a vertical bar to match on alternative
}
match Some(false) {
Some(_),
//~^ ERROR unexpected `,` in pattern
//~| HELP try adding parentheses to match on a tuple
//~| HELP or a vertical bar to match on multiple alternatives
//~| HELP or a vertical bar to match on alternative
_ => {}
}
match Some(false) {

View file

@ -16,7 +16,7 @@ help: try adding parentheses to match on a tuple...
|
LL | (Some(_),)
| + +
help: ...or a vertical bar to match on multiple alternatives
help: ...or a vertical bar to match on alternatives
|
LL - Some(_),
LL + Some(_) |
@ -36,13 +36,10 @@ LL |
LL |
LL ~ _) => {}
|
help: ...or a vertical bar to match on multiple alternatives
help: ...or a vertical bar to match on alternatives
|
LL ~ Some(_) |
LL +
LL +
LL +
LL ~ _ => {}
LL - Some(_),
LL + Some(_) |
|
error: expected one of `.`, `=>`, `?`, or an operator, found reserved identifier `_`

View file

@ -0,0 +1,18 @@
//@ run-rustfix
// Regression test for issue #143330.
// Ensure we suggest to replace only the intended coma with a bar, not all commas in the pattern.
fn main() {
struct Foo { x: i32, ch: char }
let pos = Foo { x: 2, ch: 'x' };
match pos {
// All commas here were replaced with bars.
// Foo { x: 2 | ch: ' |' } | Foo { x: 3 | ch: '@' } => (),
(Foo { x: 2, ch: ',' } | Foo { x: 3, ch: '@' }) => (),
//~^ ERROR unexpected `,` in pattern
//~| HELP try adding parentheses to match on a tuple...
//~| HELP ...or a vertical bar to match on alternative
_ => todo!(),
}
}

View file

@ -0,0 +1,18 @@
//@ run-rustfix
// Regression test for issue #143330.
// Ensure we suggest to replace only the intended coma with a bar, not all commas in the pattern.
fn main() {
struct Foo { x: i32, ch: char }
let pos = Foo { x: 2, ch: 'x' };
match pos {
// All commas here were replaced with bars.
// Foo { x: 2 | ch: ' |' } | Foo { x: 3 | ch: '@' } => (),
Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (),
//~^ ERROR unexpected `,` in pattern
//~| HELP try adding parentheses to match on a tuple...
//~| HELP ...or a vertical bar to match on alternative
_ => todo!(),
}
}

View file

@ -0,0 +1,18 @@
error: unexpected `,` in pattern
--> $DIR/only-replace-intended-coma-not-all-in-pattern.rs:12:30
|
LL | Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (),
| ^
|
help: try adding parentheses to match on a tuple...
|
LL | (Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' }) => (),
| + +
help: ...or a vertical bar to match on alternatives
|
LL - Foo { x: 2, ch: ',' }, Foo { x: 3, ch: '@' } => (),
LL + Foo { x: 2, ch: ',' } | Foo { x: 3, ch: '@' } => (),
|
error: aborting due to 1 previous error