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 fa8b854
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2060,12 +2060,38 @@ 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 lhs_lines: Vec<&str> = lhs_str.split("\n").collect();

let mut rhs_shape = shape.clone();

for line in lhs_lines.into_iter().rev() {
let mut indent_width = 0;
let mut first_char = ' ';
for char in line.chars() {
if char != ' ' {
first_char = char;
break;
} else {
indent_width += 1;
}
}

if first_char != '/' {
let indent = Indent::from_width(&context.config, indent_width);
rhs_shape = Shape::indented(indent, &context.config);
break;
}
}

println!("config={:?}", context.config.max_width());
println!("old shape={shape:?}");
println!("new shape={rhs_shape:?}");
rewrite_assign_rhs(
context,
lhs_str,
rhs,
&RhsAssignKind::Expr(&rhs.kind, rhs.span),
shape,
rhs_shape,
)
}

Expand Down
14 changes: 14 additions & 0 deletions tests/source/issue-189.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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(),
});
}
}
14 changes: 14 additions & 0 deletions tests/target/issue-189.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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(),
});
}
}

0 comments on commit fa8b854

Please sign in to comment.