From b5449ba7850406821d1b05ca4f55c5eb3b7e8ae8 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Wed, 22 May 2019 10:51:19 +0900 Subject: [PATCH] Allow overflowing rhs of unit variant (#3566) --- src/expr.rs | 9 ++++++++- src/items.rs | 8 +++++++- src/shape.rs | 11 +++++++++++ tests/source/enum.rs | 7 +++++++ tests/target/enum.rs | 9 +++++++++ 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index c4c41fb6957..afd9465c73c 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -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 @@ -1970,6 +1973,10 @@ fn choose_rhs( 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)), } @@ -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)) } diff --git a/src/items.rs b/src/items.rs index 202eb2cf117..d642718412f 100644 --- a/src/items.rs +++ b/src/items.rs @@ -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() } diff --git a/src/shape.rs b/src/shape.rs index d8971e05ef9..4376fd12b52 100644 --- a/src/shape.rs +++ b/src/shape.rs @@ -136,6 +136,9 @@ impl Sub 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, @@ -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)] diff --git a/tests/source/enum.rs b/tests/source/enum.rs index c348b5752e2..fd563206ae5 100644 --- a/tests/source/enum.rs +++ b/tests/source/enum.rs @@ -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, +} diff --git a/tests/target/enum.rs b/tests/target/enum.rs index dda58c4b5b0..6af05cbed90 100644 --- a/tests/target/enum.rs +++ b/tests/target/enum.rs @@ -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, +}