Skip to content

Commit b5449ba

Browse files
authored
Allow overflowing rhs of unit variant (#3566)
1 parent 72ca0e5 commit b5449ba

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

src/expr.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1894,6 +1894,9 @@ pub(crate) enum RhsTactics {
18941894
Default,
18951895
/// Put the rhs on the next line if it uses multiple line, without extra indentation.
18961896
ForceNextLineWithoutIndent,
1897+
/// Allow overflowing max width if neither `Default` nor `ForceNextLineWithoutIndent`
1898+
/// did not work.
1899+
AllowOverflow,
18971900
}
18981901

18991902
// The left hand side must contain everything up to, and including, the
@@ -1970,6 +1973,10 @@ fn choose_rhs<R: Rewrite>(
19701973
Some(format!("{}{}", new_indent_str, new_rhs))
19711974
}
19721975
(None, Some(ref new_rhs)) => Some(format!("{}{}", new_indent_str, new_rhs)),
1976+
(None, None) if rhs_tactics == RhsTactics::AllowOverflow => {
1977+
let shape = shape.infinite_width();
1978+
expr.rewrite(context, shape).map(|s| format!(" {}", s))
1979+
}
19731980
(None, None) => None,
19741981
(Some(orig_rhs), _) => Some(format!(" {}", orig_rhs)),
19751982
}
@@ -1986,7 +1993,7 @@ fn shape_from_rhs_tactic(
19861993
RhsTactics::ForceNextLineWithoutIndent => shape
19871994
.with_max_width(context.config)
19881995
.sub_width(shape.indent.width()),
1989-
RhsTactics::Default => {
1996+
RhsTactics::Default | RhsTactics::AllowOverflow => {
19901997
Shape::indented(shape.indent.block_indent(context.config), context.config)
19911998
.sub_width(shape.rhs_overhead(context.config))
19921999
}

src/items.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,13 @@ impl<'a> FmtVisitor<'a> {
592592
rewrite_ident(&context, field.node.ident),
593593
pad_discrim_ident_to
594594
);
595-
rewrite_assign_rhs(&context, lhs, &*expr.value, shape)?
595+
rewrite_assign_rhs_with(
596+
&context,
597+
lhs,
598+
&*expr.value,
599+
shape,
600+
RhsTactics::AllowOverflow,
601+
)?
596602
} else {
597603
rewrite_ident(&context, field.node.ident).to_owned()
598604
}

src/shape.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ impl Sub<usize> for Indent {
136136
}
137137
}
138138

139+
// 8096 is close enough to infinite for rustfmt.
140+
const INFINITE_SHAPE_WIDTH: usize = 8096;
141+
139142
#[derive(Copy, Clone, Debug)]
140143
pub(crate) struct Shape {
141144
pub(crate) width: usize,
@@ -274,6 +277,14 @@ impl Shape {
274277
offset_indent.alignment = self.offset;
275278
offset_indent.to_string_inner(config, 0)
276279
}
280+
281+
/// Creates a `Shape` with a virtually infinite width.
282+
pub(crate) fn infinite_width(&self) -> Shape {
283+
Shape {
284+
width: INFINITE_SHAPE_WIDTH,
285+
..*self
286+
}
287+
}
277288
}
278289

279290
#[cfg(test)]

tests/source/enum.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,10 @@ pub enum QlError {
195195
// #2594
196196
enum Foo {}
197197
enum Bar { }
198+
199+
// #3562
200+
enum PublishedFileVisibility {
201+
Public = sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityPublic,
202+
FriendsOnly = sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityFriendsOnly,
203+
Private = sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityPrivate,
204+
}

tests/target/enum.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,12 @@ pub enum QlError {
264264
// #2594
265265
enum Foo {}
266266
enum Bar {}
267+
268+
// #3562
269+
enum PublishedFileVisibility {
270+
Public =
271+
sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityPublic,
272+
FriendsOnly = sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityFriendsOnly,
273+
Private =
274+
sys::ERemoteStoragePublishedFileVisibility_k_ERemoteStoragePublishedFileVisibilityPrivate,
275+
}

0 commit comments

Comments
 (0)