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:
commit
2e06dcdbeb
8 changed files with 79 additions and 30 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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(_) |
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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 `_`
|
||||
|
|
|
|||
|
|
@ -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!(),
|
||||
}
|
||||
}
|
||||
|
|
@ -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!(),
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue