Skip to content

Commit 2525689

Browse files
Copilotsourcefrog
andcommitted
Fix struct field deletion to include trailing commas
- Updated visit_expr_struct to iterate over field pairs including punctuation - Include trailing comma in the span to generate valid Rust code - This fixes the issue where deleted fields left invalid syntax with dangling commas Co-authored-by: sourcefrog <346355+sourcefrog@users.noreply.github.com>
1 parent 47d105a commit 2525689

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/visit.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,14 +698,29 @@ impl<'ast> Visit<'ast> for DiscoveryVisitor<'_> {
698698
// Check if this struct has a base (default) expression like `..Default::default()`
699699
if let Some(_rest) = &i.rest {
700700
// Generate a mutant for each field by deleting it
701-
for field in &i.fields {
701+
// We need to include the trailing comma in the span
702+
for pair in i.fields.pairs() {
703+
let field = pair.value();
702704
if let syn::Member::Named(field_name) = &field.member {
703705
let field_name_str = field_name.to_string();
704706
let short_replaced = Some(field_name_str);
707+
// Span includes the field and its trailing comma (if present)
708+
let span = if let Some(comma) = pair.punct() {
709+
// Include the comma in the span
710+
let field_span = field.span();
711+
let comma_span = comma.span();
712+
Span {
713+
start: field_span.start().into(),
714+
end: comma_span.end().into(),
715+
}
716+
} else {
717+
// No comma, just the field
718+
field.span().into()
719+
};
705720
let mutant = Mutant {
706721
source_file: self.source_file.clone(),
707722
function: self.fn_stack.last().cloned(),
708-
span: field.span().into(),
723+
span,
709724
short_replaced,
710725
replacement: String::new(),
711726
genre: Genre::StructField,

0 commit comments

Comments
 (0)