Skip to content
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

Adjust enum variant spans to exclude any explicit discriminant #5687

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ impl<'a> FmtVisitor<'a> {
let variant_body = match field.data {
ast::VariantData::Tuple(..) | ast::VariantData::Struct(..) => format_struct(
&context,
&StructParts::from_variant(field),
&StructParts::from_variant(field, &context),
self.block_indent,
Some(one_line_width),
)?,
Expand Down Expand Up @@ -951,14 +951,14 @@ impl<'a> StructParts<'a> {
format_header(context, self.prefix, self.ident, self.vis, offset)
}

fn from_variant(variant: &'a ast::Variant) -> Self {
fn from_variant(variant: &'a ast::Variant, context: &RewriteContext<'_>) -> Self {
StructParts {
prefix: "",
ident: variant.ident,
vis: &DEFAULT_VISIBILITY,
def: &variant.data,
generics: None,
span: variant.span,
span: enum_variant_span(variant, context),
}
}

Expand All @@ -979,6 +979,25 @@ impl<'a> StructParts<'a> {
}
}

fn enum_variant_span(variant: &ast::Variant, context: &RewriteContext<'_>) -> Span {
use ast::VariantData::*;
if let Some(ref anon_const) = variant.disr_expr {
let span_before_consts = variant.span.until(anon_const.value.span);
let hi = match &variant.data {
Struct(..) => context
.snippet_provider
.span_after_last(span_before_consts, "}"),
Tuple(..) => context
.snippet_provider
.span_after_last(span_before_consts, ")"),
Unit(..) => variant.ident.span.hi(),
};
mk_sp(span_before_consts.lo(), hi)
} else {
variant.span
}
}

fn format_struct(
context: &RewriteContext<'_>,
struct_parts: &StructParts<'_>,
Expand Down
40 changes: 40 additions & 0 deletions tests/source/issue_5686.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#[repr(u8)]
enum MyEnum {
UnitWithExplicitDiscriminant = 0,
EmptyStructSingleLineBlockComment {
/* Comment */
} = 1,
EmptyStructMultiLineBlockComment {
/*
* Comment
*/
} = 2,
EmptyStructLineComment {
// comment
} = 3,
EmptyTupleSingleLineBlockComment(
/* Comment */
) = 4,
EmptyTupleMultiLineBlockComment(
/*
* Comment
*/
) = 5,
EmptyTupleLineComment(
// comment
) = 6,
}

enum Animal {
Dog(/* tuple variant closer in comment -> ) */) = 1,
#[hello(world)]
Cat(/* tuple variant close in leading attribute */) = 2,
Bee(/* tuple variant closer on associated field attribute */ #[hello(world)] usize) = 3,
Fox(/* tuple variant closer on const fn call */) = some_const_fn(),
Ant(/* tuple variant closer on macro call */) = some_macro!(),
Snake {/* stuct variant closer in comment -> } */} = 6,
#[hell{world}]
Cobra {/* struct variant close in leading attribute */} = 6,
Eagle {/* struct variant closer on associated field attribute */ #[hell{world}]value: Sting} = 7,
Koala {/* struct variant closer on macro call */} = some_macro!{}
}
42 changes: 42 additions & 0 deletions tests/target/issue_5686.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#[repr(u8)]
enum MyEnum {
UnitWithExplicitDiscriminant = 0,
EmptyStructSingleLineBlockComment {/* Comment */} = 1,
EmptyStructMultiLineBlockComment {
/*
* Comment
*/
} = 2,
EmptyStructLineComment {
// comment
} = 3,
EmptyTupleSingleLineBlockComment(/* Comment */) = 4,
EmptyTupleMultiLineBlockComment(
/*
* Comment
*/
) = 5,
EmptyTupleLineComment(
// comment
) = 6,
}

enum Animal {
Dog(/* tuple variant closer in comment -> ) */) = 1,
#[hello(world)]
Cat(/* tuple variant close in leading attribute */) = 2,
Bee(
/* tuple variant closer on associated field attribute */ #[hello(world)] usize,
) = 3,
Fox(/* tuple variant closer on const fn call */) = some_const_fn(),
Ant(/* tuple variant closer on macro call */) = some_macro!(),
Snake {/* stuct variant closer in comment -> } */} = 6,
#[hell{world}]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol 🔥

Cobra {/* struct variant close in leading attribute */} = 6,
Eagle {
/* struct variant closer on associated field attribute */
#[hell{world}]
value: Sting,
} = 7,
Koala {/* struct variant closer on macro call */} = some_macro! {},
}