Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ impl<'a> Rewrite for ControlFlow<'a> {
let block_sep = if self.cond.is_none() && between_kwd_cond_comment.is_some() {
""
} else if context.config.control_brace_style ==
ControlBraceStyle::AlwaysNextLine {
ControlBraceStyle::AlwaysNextLine {
alt_block_sep.as_str()
} else {
" "
Expand All @@ -912,6 +912,15 @@ impl<'a> Rewrite for ControlFlow<'a> {
block_str);

if let Some(else_block) = self.else_block {
// Since this is an else block, we should not indent for the assignment preceding
// the original if, so set shape.indent.alignment to 0.
let shape = Shape {
width: shape.width,
indent: Indent {
block_indent: shape.indent.block_indent,
alignment: 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is quite right, because the if/else could be nested inside an expression where the alignment should not be reset

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have an example in mind? Because as I understand it, the } else if ... { part will always move back to the block indent, which is retained.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think you're right, and you're passing the test suite, so it seems good.

},
};
let mut last_in_chain = false;
let rewrite = match else_block.node {
// If the else expression is another if-else expression, prevent it
Expand Down
9 changes: 9 additions & 0 deletions tests/source/issue-1239.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn foo() {
let with_alignment = if condition__uses_alignment_for_first_if__0 ||
condition__uses_alignment_for_first_if__1 ||
condition__uses_alignment_for_first_if__2 {
} else if condition__no_alignment_for_later_else__0 ||
condition__no_alignment_for_later_else__1 ||
condition__no_alignment_for_later_else__2 {
};
}
9 changes: 9 additions & 0 deletions tests/target/issue-1239.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn foo() {
let with_alignment = if condition__uses_alignment_for_first_if__0 ||
condition__uses_alignment_for_first_if__1 ||
condition__uses_alignment_for_first_if__2 {
} else if condition__no_alignment_for_later_else__0 ||
condition__no_alignment_for_later_else__1 ||
condition__no_alignment_for_later_else__2 {
};
}