Skip to content

Commit e41329c

Browse files
ytmimicalebcartwright
authored andcommitted
Search for struct body span after any generic arguments
Fixes 5273 Previously, rustfmt searched for the start of a struct body after the opening `{`. In most cases this works just fine, but const values can also be defined between `{ }`, which lead to issues when rewriting the struct body. Now, rustfmt will search for the `{` after the generic argument list to guarantee that the `{` it finds is the start of the struct body.
1 parent a36dc36 commit e41329c

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/items.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,13 @@ pub(crate) fn format_struct_struct(
12731273
result.push_str(&header_str);
12741274

12751275
let header_hi = struct_parts.ident.span.hi();
1276-
let body_lo = context.snippet_provider.span_after(span, "{");
1276+
let body_lo = if let Some(generics) = struct_parts.generics {
1277+
// Adjust the span to start at the end of the generic arguments before searching for the '{'
1278+
let span = span.with_lo(generics.span.hi());
1279+
context.snippet_provider.span_after(span, "{")
1280+
} else {
1281+
context.snippet_provider.span_after(span, "{")
1282+
};
12771283

12781284
let generics_str = match struct_parts.generics {
12791285
Some(g) => format_generics(

tests/target/issue_5273.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
struct Example<const N: usize = { 1048576 }> {
2+
//
3+
}

0 commit comments

Comments
 (0)