From fb84edc0a9d30163b75e35b54cc4bf726022e3f2 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 | 12 ++++++++++++ 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, 66 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..f5741b2e72a 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2060,6 +2060,18 @@ fn rewrite_assignment( let lhs_shape = shape.sub_width(operator_str.len() + 1)?; let lhs_str = format!("{} {}", lhs.rewrite(context, lhs_shape)?, operator_str); + let last_line = lhs_str.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(); + let shape = if last_line.starts_with(&extra_indent_string) { + new_shape + } else { + shape + }; + rewrite_assign_rhs( context, lhs_str, 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; + } +}