Skip to content

Commit

Permalink
rust-lang/style-team#189: rhs-should-use-indent-of-last-line-of-lhs
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhuichen committed Sep 1, 2024
1 parent 46cb7d3 commit 23bfe0e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2060,6 +2060,24 @@ 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.rsplit_once("\n");
let shape = match last_line {
Some((_, line)) => {
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 line.starts_with(&extra_indent_string) {
new_shape
} else {
shape
}
}
_ => shape,
};
rewrite_assign_rhs(
context,
lhs_str,
Expand Down
6 changes: 6 additions & 0 deletions src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions tests/source/rust-lang/style-team#189.rs
Original file line number Diff line number Diff line change
@@ -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;
}
}
24 changes: 24 additions & 0 deletions tests/target/rust-lang/style-team#189.rs
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 23bfe0e

Please sign in to comment.