Skip to content

Commit 6da81be

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

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

src/expr.rs

+19
Original file line numberDiff line numberDiff line change
@@ -2101,6 +2101,25 @@ pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
21012101
rhs_kind: &RhsAssignKind<'_>,
21022102
rhs_tactics: RhsTactics,
21032103
) -> Option<String> {
2104+
let get_lhs_last_line_shape = || {
2105+
let last_line = lhs.rsplitn(2, "\n").next().unwrap_or_default();
2106+
let tab_spaces = context.config.tab_spaces();
2107+
let new_shape = shape
2108+
.block_indent(tab_spaces)
2109+
.saturating_sub_width(tab_spaces);
2110+
let extra_indent_string = new_shape.to_string(&context.config).to_string();
2111+
if last_line.starts_with(&extra_indent_string) {
2112+
new_shape
2113+
} else {
2114+
shape
2115+
}
2116+
};
2117+
let shape = if context.config.style_edition() >= StyleEdition::Edition2024 {
2118+
get_lhs_last_line_shape()
2119+
} else {
2120+
shape
2121+
};
2122+
21042123
let last_line_width = last_line_width(lhs).saturating_sub(if lhs.contains('\n') {
21052124
shape.indent.width()
21062125
} else {

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)