Skip to content

Commit

Permalink
Handle multiline condition in let if else (fixes rust-lang#1239)
Browse files Browse the repository at this point in the history
  • Loading branch information
tspiteri committed Jan 31, 2017
1 parent 428339f commit 6810c76
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
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,
},
};
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 {
};
}

0 comments on commit 6810c76

Please sign in to comment.