Skip to content

Commit fb84edc

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

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

Diff for: src/expr.rs

+12
Original file line numberDiff line numberDiff line change
@@ -2060,6 +2060,18 @@ 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.rsplitn(2, "\n").next().unwrap_or_default();
2064+
let tab_spaces = context.config.tab_spaces();
2065+
let new_shape = shape
2066+
.block_indent(tab_spaces)
2067+
.saturating_sub_width(tab_spaces);
2068+
let extra_indent_string = new_shape.to_string(&context.config).to_string();
2069+
let shape = if last_line.starts_with(&extra_indent_string) {
2070+
new_shape
2071+
} else {
2072+
shape
2073+
};
2074+
20632075
rewrite_assign_rhs(
20642076
context,
20652077
lhs_str,

Diff for: 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 {

Diff for: tests/source/rust-lang/style-team#189.rs

+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+
}

Diff for: tests/target/rust-lang/style-team#189.rs

+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)