diff --git a/src/expr.rs b/src/expr.rs index 5bd87d00b1d..bf946ff6c26 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2124,6 +2124,24 @@ pub(crate) fn rewrite_assign_rhs_expr( rhs_kind: &RhsAssignKind<'_>, rhs_tactics: RhsTactics, ) -> RewriteResult { + let get_rhs_shape = || { + let last_line = lhs.lines().last().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); + if last_line.starts_with(extra_indent_string.as_ref()) { + new_shape + } else { + shape + } + }; + let shape = if context.config.style_edition() >= StyleEdition::Edition2024 { + get_rhs_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..beede122064 100644 --- a/src/shape.rs +++ b/src/shape.rs @@ -278,6 +278,13 @@ impl Shape { offset_indent.to_string_inner(config, 0) } + /// similar to to_string_with_newline, except the result does not start with a new line + 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; + } +}