Skip to content

Commit fa8b854

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

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

src/expr.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -2060,12 +2060,38 @@ 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 lhs_lines: Vec<&str> = lhs_str.split("\n").collect();
2064+
2065+
let mut rhs_shape = shape.clone();
2066+
2067+
for line in lhs_lines.into_iter().rev() {
2068+
let mut indent_width = 0;
2069+
let mut first_char = ' ';
2070+
for char in line.chars() {
2071+
if char != ' ' {
2072+
first_char = char;
2073+
break;
2074+
} else {
2075+
indent_width += 1;
2076+
}
2077+
}
2078+
2079+
if first_char != '/' {
2080+
let indent = Indent::from_width(&context.config, indent_width);
2081+
rhs_shape = Shape::indented(indent, &context.config);
2082+
break;
2083+
}
2084+
}
2085+
2086+
println!("config={:?}", context.config.max_width());
2087+
println!("old shape={shape:?}");
2088+
println!("new shape={rhs_shape:?}");
20632089
rewrite_assign_rhs(
20642090
context,
20652091
lhs_str,
20662092
rhs,
20672093
&RhsAssignKind::Expr(&rhs.kind, rhs.span),
2068-
shape,
2094+
rhs_shape,
20692095
)
20702096
}
20712097

tests/source/issue-189.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}

tests/target/issue-189.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}

0 commit comments

Comments
 (0)