Skip to content

Commit 7a3e4fc

Browse files
ytmimicalebcartwright
authored andcommitted
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 8be748d commit 7a3e4fc

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/expr.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,16 @@ fn rewrite_block_inner(
609609
result
610610
}
611611

612+
/// Rewrite the divergent block of a `let-else` statement.
613+
pub(crate) fn rewrite_let_else_block(
614+
block: &ast::Block,
615+
allow_single_line: bool,
616+
context: &RewriteContext<'_>,
617+
shape: Shape,
618+
) -> Option<String> {
619+
rewrite_block_inner(block, None, None, allow_single_line, context, shape)
620+
}
621+
612622
// Rewrite condition if the given expression has one.
613623
pub(crate) fn rewrite_cond(
614624
context: &RewriteContext<'_>,

src/items.rs

Lines changed: 9 additions & 2 deletions
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)