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 fb84edc
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 fb84edc

Please sign in to comment.