From fa8b85459c7de05767521168373049731d464072 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 | 28 +++++++++++++++++++++++++++- tests/source/issue-189.rs | 14 ++++++++++++++ tests/target/issue-189.rs | 14 ++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 tests/source/issue-189.rs create mode 100644 tests/target/issue-189.rs diff --git a/src/expr.rs b/src/expr.rs index 02372e7be13..00aa1ab51fb 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2060,12 +2060,38 @@ 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 lhs_lines: Vec<&str> = lhs_str.split("\n").collect(); + + let mut rhs_shape = shape.clone(); + + for line in lhs_lines.into_iter().rev() { + let mut indent_width = 0; + let mut first_char = ' '; + for char in line.chars() { + if char != ' ' { + first_char = char; + break; + } else { + indent_width += 1; + } + } + + if first_char != '/' { + let indent = Indent::from_width(&context.config, indent_width); + rhs_shape = Shape::indented(indent, &context.config); + break; + } + } + + println!("config={:?}", context.config.max_width()); + println!("old shape={shape:?}"); + println!("new shape={rhs_shape:?}"); rewrite_assign_rhs( context, lhs_str, rhs, &RhsAssignKind::Expr(&rhs.kind, rhs.span), - shape, + rhs_shape, ) } diff --git a/tests/source/issue-189.rs b/tests/source/issue-189.rs new file mode 100644 index 00000000000..484119084ed --- /dev/null +++ b/tests/source/issue-189.rs @@ -0,0 +1,14 @@ +// 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(), + }); + } +} diff --git a/tests/target/issue-189.rs b/tests/target/issue-189.rs new file mode 100644 index 00000000000..607464aff2b --- /dev/null +++ b/tests/target/issue-189.rs @@ -0,0 +1,14 @@ +// 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(), + }); + } +}