From 6da81becdebf26c9eca9bf3a1ea1f01bc2d4a973 Mon Sep 17 00:00:00 2001 From: John Chen Date: Sun, 18 Aug 2024 09:11:19 -0400 Subject: [PATCH] rust-lang/style-team#189: rhs-should-use-indent-of-last-line-of-lhs --- src/expr.rs | 19 +++++++++++++++++++ src/shape.rs | 6 ++++++ tests/source/rust-lang/style-team#189.rs | 24 ++++++++++++++++++++++++ tests/target/rust-lang/style-team#189.rs | 24 ++++++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 tests/source/rust-lang/style-team#189.rs create mode 100644 tests/target/rust-lang/style-team#189.rs diff --git a/src/expr.rs b/src/expr.rs index 02372e7be13..c8dca2969b2 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2101,6 +2101,25 @@ pub(crate) fn rewrite_assign_rhs_expr( rhs_kind: &RhsAssignKind<'_>, rhs_tactics: RhsTactics, ) -> Option { + let get_lhs_last_line_shape = || { + let last_line = lhs.rsplitn(2, "\n").next().unwrap_or_default(); + let tab_spaces = context.config.tab_spaces(); + let new_shape = shape + .block_indent(tab_spaces) + .saturating_sub_width(tab_spaces); + let extra_indent_string = new_shape.to_string(&context.config).to_string(); + if last_line.starts_with(&extra_indent_string) { + new_shape + } else { + shape + } + }; + let shape = if context.config.style_edition() >= StyleEdition::Edition2024 { + get_lhs_last_line_shape() + } else { + shape + }; + let last_line_width = last_line_width(lhs).saturating_sub(if lhs.contains('\n') { shape.indent.width() } else { diff --git a/src/shape.rs b/src/shape.rs index 4376fd12b52..d2115befdb4 100644 --- a/src/shape.rs +++ b/src/shape.rs @@ -278,6 +278,12 @@ impl Shape { offset_indent.to_string_inner(config, 0) } + pub(crate) fn to_string(&self, config: &Config) -> Cow<'static, str> { + let mut offset_indent = self.indent; + offset_indent.alignment = self.offset; + offset_indent.to_string_inner(config, 1) + } + /// Creates a `Shape` with a virtually infinite width. pub(crate) fn infinite_width(&self) -> Shape { Shape { diff --git a/tests/source/rust-lang/style-team#189.rs b/tests/source/rust-lang/style-team#189.rs new file mode 100644 index 00000000000..51c794aba14 --- /dev/null +++ b/tests/source/rust-lang/style-team#189.rs @@ -0,0 +1,24 @@ +// rustfmt-style_edition: 2024 + +impl SomeType { + fn method(&mut self) { + self.array[array_index as usize] + .as_mut() + .expect("thing must exist") + .extra_info = Some(ExtraInfo { + parent, + count: count as u16, + children: children.into_boxed_slice(), + }); + } +} + +impl SomeType { + fn method(&mut self) { + self.array[array_index as usize] + .as_mut() + .expect("thing must exist") + .extra_info = + long_long_long_long_long_long_long_long_long_long_long_long_long_long_long; + } +} diff --git a/tests/target/rust-lang/style-team#189.rs b/tests/target/rust-lang/style-team#189.rs new file mode 100644 index 00000000000..bd09d8b3b62 --- /dev/null +++ b/tests/target/rust-lang/style-team#189.rs @@ -0,0 +1,24 @@ +// rustfmt-style_edition: 2024 + +impl SomeType { + fn method(&mut self) { + self.array[array_index as usize] + .as_mut() + .expect("thing must exist") + .extra_info = Some(ExtraInfo { + parent, + count: count as u16, + children: children.into_boxed_slice(), + }); + } +} + +impl SomeType { + fn method(&mut self) { + self.array[array_index as usize] + .as_mut() + .expect("thing must exist") + .extra_info = + long_long_long_long_long_long_long_long_long_long_long_long_long_long_long; + } +}