Merge pull request #1295 from tspiteri/let-if-else

Handle multiline condition in let if else (fixes #1239)
This commit is contained in:
Nick Cameron 2017-02-01 13:14:59 +13:00 committed by GitHub
commit 5925a1a6d1
3 changed files with 28 additions and 1 deletions

View File

@ -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 {
" "
@ -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

View File

@ -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 {
};
}

View File

@ -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 {
};
}