Skip to content

Commit 93cf44a

Browse files
committed
Implement let-else rewriting in terms of rewrite_let_else_block
`rewrite_let_else_block` gives us more control over allowing the `else` block to be formatted on a single line than `<ast::Block as Rewrite>::rewrite`.
1 parent b91ed2e commit 93cf44a

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

Diff for: src/expr.rs

+10
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,16 @@ fn rewrite_block_inner(
605605
result
606606
}
607607

608+
/// Rewrite the divergent block of a `let-else` statement.
609+
pub(crate) fn rewrite_let_else_block(
610+
block: &ast::Block,
611+
allow_single_line: bool,
612+
context: &RewriteContext<'_>,
613+
shape: Shape,
614+
) -> Option<String> {
615+
rewrite_block_inner(block, None, None, allow_single_line, context, shape)
616+
}
617+
608618
// Rewrite condition if the given expression has one.
609619
pub(crate) fn rewrite_cond(
610620
context: &RewriteContext<'_>,

Diff for: src/items.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use crate::config::lists::*;
1818
use crate::config::{BraceStyle, Config, IndentStyle, Version};
1919
use crate::expr::{
2020
is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
21-
rewrite_assign_rhs_with_comments, rewrite_else_kw_with_comments, RhsAssignKind, RhsTactics,
21+
rewrite_assign_rhs_with_comments, rewrite_else_kw_with_comments, rewrite_let_else_block,
22+
RhsAssignKind, RhsTactics,
2223
};
2324
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
2425
use crate::macros::{rewrite_macro, MacroPosition};
@@ -132,7 +133,13 @@ impl Rewrite for ast::Local {
132133
shape,
133134
);
134135
result.push_str(&else_kw);
135-
result.push_str(&block.rewrite(context, shape)?);
136+
let allow_single_line = !result.contains('\n');
137+
result.push_str(&rewrite_let_else_block(
138+
block,
139+
allow_single_line,
140+
context,
141+
shape,
142+
)?);
136143
};
137144
}
138145

0 commit comments

Comments
 (0)