-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
fix: don't add trailing comma after .. in struct pattern suggestion
#153501
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2436,10 +2436,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
| .struct_span_err(pat.span, "pattern requires `..` due to inaccessible fields"); | ||
|
|
||
| if let Some(field) = fields.last() { | ||
| // `..` does not allow a trailing comma, so if the last field already | ||
| // ends with one, insert `" .."` right after that comma rather than | ||
| // appending `", .."` after the field (which would produce `field, ..,`). | ||
| let sm = self.tcx.sess.source_map(); | ||
| let after_field = field.span.shrink_to_hi(); | ||
| let tail_span = after_field.with_hi(pat.span.hi()); | ||
| // `span_through_char` scans past whitespace and comments, so this | ||
| // correctly handles cases like `field /*comment*/,`. | ||
| let comma_span = sm.span_through_char(tail_span, ','); | ||
| let (span, insertion) = if comma_span != tail_span { | ||
| // A comma was found; insert ` ..` right after it. | ||
| (comma_span.shrink_to_hi(), " ..") | ||
| } else { | ||
| (after_field, ", ..") | ||
| }; | ||
| err.span_suggestion_verbose( | ||
| field.span.shrink_to_hi(), | ||
| span, | ||
| "ignore the inaccessible and unused fields", | ||
| ", ..", | ||
| insertion, | ||
| Applicability::MachineApplicable, | ||
| ); | ||
|
Comment on lines
2454
to
2459
|
||
| } else { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
has_trailing_commaonly skips whitespace (trim_start()), so cases likeFoo { field /*comment*/, }won’t be detected as having a trailing comma. In that situation this code will take theelsebranch and insert", .."immediately after the field, leaving the original comma later and still producing an invalid suggestion (field, .. /*comment*/,). Consider skipping over line/block comments (and whitespace) when checking what comes after the field, or scanning the tail snippet for the first non-trivia token and checking whether it’s a comma before deciding the insertion point.