Fix incorrect trailing comma suggested in no_accessible_fields

This commit is contained in:
yukang
2026-01-18 11:14:13 +08:00
parent 9f6cd6defb
commit 73e35ee5ee
4 changed files with 75 additions and 2 deletions
+12 -2
View File
@@ -2434,10 +2434,20 @@ fn error_no_accessible_fields(
.struct_span_err(pat.span, "pattern requires `..` due to inaccessible fields");
if let Some(field) = fields.last() {
let tail_span = field.span.shrink_to_hi().to(pat.span.shrink_to_hi());
let comma_hi_offset =
self.tcx.sess.source_map().span_to_snippet(tail_span).ok().and_then(|snippet| {
let trimmed = snippet.trim_start();
trimmed.starts_with(',').then(|| (snippet.len() - trimmed.len() + 1) as u32)
});
err.span_suggestion_verbose(
field.span.shrink_to_hi(),
if let Some(comma_hi_offset) = comma_hi_offset {
tail_span.with_hi(tail_span.lo() + BytePos(comma_hi_offset)).shrink_to_hi()
} else {
field.span.shrink_to_hi()
},
"ignore the inaccessible and unused fields",
", ..",
if comma_hi_offset.is_some() { " .." } else { ", .." },
Applicability::MachineApplicable,
);
} else {
@@ -0,0 +1,23 @@
#![allow(dead_code)]
#![allow(unused)]
//@ run-rustfix
mod m {
pub(crate) struct S {
pub(crate) visible: u64,
hidden: u64,
}
impl S {
pub(crate) fn new() -> Self {
loop {}
}
}
}
fn main() {
let m::S {
//~^ ERROR pattern requires `..` due to inaccessible fields
visible, ..
} = m::S::new();
}
@@ -0,0 +1,23 @@
#![allow(dead_code)]
#![allow(unused)]
//@ run-rustfix
mod m {
pub(crate) struct S {
pub(crate) visible: u64,
hidden: u64,
}
impl S {
pub(crate) fn new() -> Self {
loop {}
}
}
}
fn main() {
let m::S {
//~^ ERROR pattern requires `..` due to inaccessible fields
visible,
} = m::S::new();
}
@@ -0,0 +1,17 @@
error: pattern requires `..` due to inaccessible fields
--> $DIR/inaccessible-private-fields-trailing-comma-149787.rs:19:9
|
LL | let m::S {
| _________^
LL | |
LL | | visible,
LL | | } = m::S::new();
| |_____^
|
help: ignore the inaccessible and unused fields
|
LL | visible, ..
| ++
error: aborting due to 1 previous error