Skip to content

Allow overflowing rhs of unit variant #3566

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

Merged
merged 5 commits into from
May 22, 2019
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
9 changes: 8 additions & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,9 @@ pub(crate) enum RhsTactics {
Default,
/// Put the rhs on the next line if it uses multiple line, without extra indentation.
ForceNextLineWithoutIndent,
/// Allow overflowing max width if neither `Default` nor `ForceNextLineWithoutIndent`
/// did not work.
AllowOverflow,
}

// The left hand side must contain everything up to, and including, the
Expand Down Expand Up @@ -1970,6 +1973,10 @@ fn choose_rhs<R: Rewrite>(
Some(format!("{}{}", new_indent_str, new_rhs))
}
(None, Some(ref new_rhs)) => Some(format!("{}{}", new_indent_str, new_rhs)),
(None, None) if rhs_tactics == RhsTactics::AllowOverflow => {
let shape = shape.infinite_width();
expr.rewrite(context, shape).map(|s| format!(" {}", s))
}
(None, None) => None,
(Some(orig_rhs), _) => Some(format!(" {}", orig_rhs)),
}
Expand All @@ -1986,7 +1993,7 @@ fn shape_from_rhs_tactic(
RhsTactics::ForceNextLineWithoutIndent => shape
.with_max_width(context.config)
.sub_width(shape.indent.width()),
RhsTactics::Default => {
RhsTactics::Default | RhsTactics::AllowOverflow => {
Shape::indented(shape.indent.block_indent(context.config), context.config)
.sub_width(shape.rhs_overhead(context.config))
}
Expand Down
8 changes: 7 additions & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,13 @@ impl<'a> FmtVisitor<'a> {
rewrite_ident(&context, field.node.ident),
pad_discrim_ident_to
);
rewrite_assign_rhs(&context, lhs, &*expr.value, shape)?
rewrite_assign_rhs_with(
&context,
lhs,
&*expr.value,
shape,
RhsTactics::AllowOverflow,
)?
} else {
rewrite_ident(&context, field.node.ident).to_owned()
}
Expand Down
11 changes: 11 additions & 0 deletions src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ impl Sub<usize> for Indent {
}
}

// 8096 is close enough to infinite for rustfmt.
const INFINITE_SHAPE_WIDTH: usize = 8096;

#[derive(Copy, Clone, Debug)]
pub(crate) struct Shape {
pub(crate) width: usize,
Expand Down Expand Up @@ -274,6 +277,14 @@ impl Shape {
offset_indent.alignment = self.offset;
offset_indent.to_string_inner(config, 0)
}

/// Creates a `Shape` with a virtually infinite width.
pub(crate) fn infinite_width(&self) -> Shape {
Shape {
width: INFINITE_SHAPE_WIDTH,
..*self
}
}
}

#[cfg(test)]
Expand Down
7 changes: 7 additions & 0 deletions tests/source/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,10 @@ pub enum QlError {
// #2594
enum Foo {}
enum Bar { }

// #3562
enum PublishedFileVisibility {
Public = sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityPublic,
FriendsOnly = sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityFriendsOnly,
Private = sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityPrivate,
}
9 changes: 9 additions & 0 deletions tests/target/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,12 @@ pub enum QlError {
// #2594
enum Foo {}
enum Bar {}

// #3562
enum PublishedFileVisibility {
Public =
sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityPublic,
FriendsOnly = sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityFriendsOnly,
Private =
sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityPrivate,
}