diff options
| author | Jubilee <workingjubilee@gmail.com> | 2025-05-30 13:52:25 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-30 13:52:25 -0700 |
| commit | 5e139db47b29d5a546730ff34eb67916cb462c08 (patch) | |
| tree | 63d81c9688e1849e0b815d0ee8b60101123e4ce3 /compiler/rustc_parse/src/parser/expr.rs | |
| parent | a7e56bff084c22ef915029cc5076acaa2677a081 (diff) | |
| parent | 5f8954bc4185bf825eb84a44d3833e0ce78590da (diff) | |
| download | rust-5e139db47b29d5a546730ff34eb67916cb462c08.tar.gz rust-5e139db47b29d5a546730ff34eb67916cb462c08.zip | |
Rollup merge of #141077 - chenyukang:yukang-fix-140991-comma, r=wesleywiser
Fix the issue of typo of comma in arm parsing Fixes #140991 I also checked is it a '/', since it's near from ',' from keyboard.
Diffstat (limited to 'compiler/rustc_parse/src/parser/expr.rs')
| -rw-r--r-- | compiler/rustc_parse/src/parser/expr.rs | 54 |
1 files changed, 36 insertions, 18 deletions
diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 2a7910a6af4..1a44f4af8a6 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -3311,26 +3311,44 @@ impl<'a> Parser<'a> { let sm = this.psess.source_map(); if let Ok(expr_lines) = sm.span_to_lines(expr_span) && let Ok(arm_start_lines) = sm.span_to_lines(arm_start_span) - && arm_start_lines.lines[0].end_col == expr_lines.lines[0].end_col && expr_lines.lines.len() == 2 { - // We check whether there's any trailing code in the parse span, - // if there isn't, we very likely have the following: - // - // X | &Y => "y" - // | -- - missing comma - // | | - // | arrow_span - // X | &X => "x" - // | - ^^ self.token.span - // | | - // | parsed until here as `"y" & X` - err.span_suggestion_short( - arm_start_span.shrink_to_hi(), - "missing a comma here to end this `match` arm", - ",", - Applicability::MachineApplicable, - ); + if arm_start_lines.lines[0].end_col == expr_lines.lines[0].end_col { + // We check whether there's any trailing code in the parse span, + // if there isn't, we very likely have the following: + // + // X | &Y => "y" + // | -- - missing comma + // | | + // | arrow_span + // X | &X => "x" + // | - ^^ self.token.span + // | | + // | parsed until here as `"y" & X` + err.span_suggestion_short( + arm_start_span.shrink_to_hi(), + "missing a comma here to end this `match` arm", + ",", + Applicability::MachineApplicable, + ); + } else if arm_start_lines.lines[0].end_col + rustc_span::CharPos(1) + == expr_lines.lines[0].end_col + { + // similar to the above, but we may typo a `.` or `/` at the end of the line + let comma_span = arm_start_span + .shrink_to_hi() + .with_hi(arm_start_span.hi() + rustc_span::BytePos(1)); + if let Ok(res) = sm.span_to_snippet(comma_span) + && (res == "." || res == "/") + { + err.span_suggestion_short( + comma_span, + "you might have meant to write a `,` to end this `match` arm", + ",", + Applicability::MachineApplicable, + ); + } + } } } else { err.span_label( |
