Skip to content

Commit 2d42c97

Browse files
committed
refactor visit_enum
- introduce format_enum that returns Rewrite - early return when it fails to format the generics in enum
1 parent e0e8238 commit 2d42c97

File tree

6 files changed

+33
-20
lines changed

6 files changed

+33
-20
lines changed

src/items.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -524,21 +524,22 @@ impl<'a> FmtVisitor<'a> {
524524
self.push_rewrite(struct_parts.span, rewrite);
525525
}
526526

527-
pub(crate) fn visit_enum(
527+
// TODO(ding-young) do I need to make it a separate function instead of a method of FmtVisitor?
528+
fn format_enum(
528529
&mut self,
529530
ident: symbol::Ident,
530531
vis: &ast::Visibility,
531532
enum_def: &ast::EnumDef,
532533
generics: &ast::Generics,
533534
span: Span,
534-
) {
535+
) -> Option<String> {
535536
let enum_header =
536537
format_header(&self.get_context(), "enum ", ident, vis, self.block_indent);
537-
self.push_str(&enum_header);
538538

539539
let enum_snippet = self.snippet(span);
540540
let brace_pos = enum_snippet.find_uncommented("{").unwrap();
541541
let body_start = span.lo() + BytePos(brace_pos as u32 + 1);
542+
// TODO(ding-young) what if there is no generic?
542543
let generics_str = format_generics(
543544
&self.get_context(),
544545
generics,
@@ -552,25 +553,36 @@ impl<'a> FmtVisitor<'a> {
552553
// make a span that starts right after `enum Foo`
553554
mk_sp(ident.span.hi(), body_start),
554555
last_line_width(&enum_header),
555-
);
556-
557-
if let Some(generics_str) = generics_str {
558-
self.push_str(&generics_str);
559-
} else {
560-
self.push_str(self.snippet(mk_sp(ident.span.hi(), body_start)));
561-
}
562-
563-
self.last_pos = body_start;
556+
)?;
564557

565558
match self.format_variant_list(enum_def, body_start, span.hi()) {
566-
Some(ref s) if enum_def.variants.is_empty() => self.push_str(s),
567-
rw => {
568-
self.push_rewrite(mk_sp(body_start, span.hi()), rw);
559+
Some(ref s) if enum_def.variants.is_empty() => {
560+
Some(format!("{enum_header}{generics_str}{s}"))
561+
}
562+
Some(rw) => {
563+
let indent = self.block_indent.to_string(self.config);
564+
self.block_indent = self.block_indent.block_unindent(self.config);
565+
Some(format!("{enum_header}{generics_str}\n{indent}{rw}"))
566+
}
567+
None => {
569568
self.block_indent = self.block_indent.block_unindent(self.config);
569+
None
570570
}
571571
}
572572
}
573573

574+
pub(crate) fn visit_enum(
575+
&mut self,
576+
ident: symbol::Ident,
577+
vis: &ast::Visibility,
578+
enum_def: &ast::EnumDef,
579+
generics: &ast::Generics,
580+
span: Span,
581+
) {
582+
let rewrite = self.format_enum(ident, vis, enum_def, generics, span);
583+
self.push_rewrite(span, rewrite);
584+
}
585+
574586
// Format the body of an enum definition
575587
fn format_variant_list(
576588
&mut self,

src/visitor.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
523523
self.visit_struct(&StructParts::from_item(item));
524524
}
525525
ast::ItemKind::Enum(ident, ref def, ref generics) => {
526-
self.format_missing_with_indent(source!(self, item.span).lo());
527526
self.visit_enum(ident, &item.vis, def, generics, item.span);
528-
self.last_pos = source!(self, item.span).hi();
529527
}
530528
ast::ItemKind::Mod(safety, ident, ref mod_kind) => {
531529
self.format_missing_with_indent(source!(self, item.span).lo());

tests/source/crash-on-enum-generics/issue_5738.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ where
88
{
99
V0,
1010
V1,
11-
V2,
11+
V2, // left unformatted since formatting where clause fails
1212
V3,
1313
}

tests/source/crash-on-enum-generics/issue_6318.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ fn my_fn() {
88
Option<[u8; 4]>,
99
>>::Archived,
1010
>,
11+
// left unformatted since formatting where clause fails
1112
{
1213
}
1314
}

tests/target/crash-on-enum-generics/issue_5738.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ where
88
{
99
V0,
1010
V1,
11-
V2,
11+
V2, // left unformatted since formatting where clause fails
1212
V3,
1313
}

tests/target/crash-on-enum-generics/issue_6318.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ fn my_fn() {
88
Option<[u8; 4]>,
99
>>::Archived,
1010
>,
11-
{}
11+
// left unformatted since formatting where clause fails
12+
{
13+
}
1214
}

0 commit comments

Comments
 (0)