Skip to content

Commit 23bfe0e

Browse files
committed
rust-lang/style-team#189: rhs-should-use-indent-of-last-line-of-lhs
1 parent 46cb7d3 commit 23bfe0e

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

src/expr.rs

+18
Original file line numberDiff line numberDiff line change
@@ -2060,6 +2060,24 @@ fn rewrite_assignment(
20602060
let lhs_shape = shape.sub_width(operator_str.len() + 1)?;
20612061
let lhs_str = format!("{} {}", lhs.rewrite(context, lhs_shape)?, operator_str);
20622062

2063+
let last_line = lhs_str.rsplit_once("\n");
2064+
let shape = match last_line {
2065+
Some((_, line)) => {
2066+
let tab_spaces = context.config.tab_spaces();
2067+
let new_shape = shape
2068+
.block_indent(tab_spaces)
2069+
.saturating_sub_width(tab_spaces);
2070+
2071+
let extra_indent_string = new_shape.to_string(&context.config).to_string();
2072+
2073+
if line.starts_with(&extra_indent_string) {
2074+
new_shape
2075+
} else {
2076+
shape
2077+
}
2078+
}
2079+
_ => shape,
2080+
};
20632081
rewrite_assign_rhs(
20642082
context,
20652083
lhs_str,

src/shape.rs

+6
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,12 @@ impl Shape {
278278
offset_indent.to_string_inner(config, 0)
279279
}
280280

281+
pub(crate) fn to_string(&self, config: &Config) -> Cow<'static, str> {
282+
let mut offset_indent = self.indent;
283+
offset_indent.alignment = self.offset;
284+
offset_indent.to_string_inner(config, 1)
285+
}
286+
281287
/// Creates a `Shape` with a virtually infinite width.
282288
pub(crate) fn infinite_width(&self) -> Shape {
283289
Shape {
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// rustfmt-style_edition: 2024
2+
3+
impl SomeType {
4+
fn method(&mut self) {
5+
self.array[array_index as usize]
6+
.as_mut()
7+
.expect("thing must exist")
8+
.extra_info = Some(ExtraInfo {
9+
parent,
10+
count: count as u16,
11+
children: children.into_boxed_slice(),
12+
});
13+
}
14+
}
15+
16+
impl SomeType {
17+
fn method(&mut self) {
18+
self.array[array_index as usize]
19+
.as_mut()
20+
.expect("thing must exist")
21+
.extra_info =
22+
long_long_long_long_long_long_long_long_long_long_long_long_long_long_long;
23+
}
24+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// rustfmt-style_edition: 2024
2+
3+
impl SomeType {
4+
fn method(&mut self) {
5+
self.array[array_index as usize]
6+
.as_mut()
7+
.expect("thing must exist")
8+
.extra_info = Some(ExtraInfo {
9+
parent,
10+
count: count as u16,
11+
children: children.into_boxed_slice(),
12+
});
13+
}
14+
}
15+
16+
impl SomeType {
17+
fn method(&mut self) {
18+
self.array[array_index as usize]
19+
.as_mut()
20+
.expect("thing must exist")
21+
.extra_info =
22+
long_long_long_long_long_long_long_long_long_long_long_long_long_long_long;
23+
}
24+
}

0 commit comments

Comments
 (0)