Skip to content

Commit 5925a1a

Browse files
authored
Merge pull request #1295 from tspiteri/let-if-else
Handle multiline condition in let if else (fixes #1239)
2 parents 428339f + 6810c76 commit 5925a1a

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/expr.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ impl<'a> Rewrite for ControlFlow<'a> {
895895
let block_sep = if self.cond.is_none() && between_kwd_cond_comment.is_some() {
896896
""
897897
} else if context.config.control_brace_style ==
898-
ControlBraceStyle::AlwaysNextLine {
898+
ControlBraceStyle::AlwaysNextLine {
899899
alt_block_sep.as_str()
900900
} else {
901901
" "
@@ -912,6 +912,15 @@ impl<'a> Rewrite for ControlFlow<'a> {
912912
block_str);
913913

914914
if let Some(else_block) = self.else_block {
915+
// Since this is an else block, we should not indent for the assignment preceding
916+
// the original if, so set shape.indent.alignment to 0.
917+
let shape = Shape {
918+
width: shape.width,
919+
indent: Indent {
920+
block_indent: shape.indent.block_indent,
921+
alignment: 0,
922+
},
923+
};
915924
let mut last_in_chain = false;
916925
let rewrite = match else_block.node {
917926
// If the else expression is another if-else expression, prevent it

tests/source/issue-1239.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn foo() {
2+
let with_alignment = if condition__uses_alignment_for_first_if__0 ||
3+
condition__uses_alignment_for_first_if__1 ||
4+
condition__uses_alignment_for_first_if__2 {
5+
} else if condition__no_alignment_for_later_else__0 ||
6+
condition__no_alignment_for_later_else__1 ||
7+
condition__no_alignment_for_later_else__2 {
8+
};
9+
}

tests/target/issue-1239.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn foo() {
2+
let with_alignment = if condition__uses_alignment_for_first_if__0 ||
3+
condition__uses_alignment_for_first_if__1 ||
4+
condition__uses_alignment_for_first_if__2 {
5+
} else if condition__no_alignment_for_later_else__0 ||
6+
condition__no_alignment_for_later_else__1 ||
7+
condition__no_alignment_for_later_else__2 {
8+
};
9+
}

0 commit comments

Comments
 (0)