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`.
This commit is contained in:
Yacin Tmimi 2023-02-12 14:46:06 -05:00 committed by Caleb Cartwright
parent 8be748dbb7
commit 7a3e4fca40
2 changed files with 19 additions and 2 deletions

View File

@ -609,6 +609,16 @@ fn rewrite_block_inner(
result
}
/// Rewrite the divergent block of a `let-else` statement.
pub(crate) fn rewrite_let_else_block(
block: &ast::Block,
allow_single_line: bool,
context: &RewriteContext<'_>,
shape: Shape,
) -> Option<String> {
rewrite_block_inner(block, None, None, allow_single_line, context, shape)
}
// Rewrite condition if the given expression has one.
pub(crate) fn rewrite_cond(
context: &RewriteContext<'_>,

View File

@ -18,7 +18,8 @@
use crate::config::{BraceStyle, Config, IndentStyle, Version};
use crate::expr::{
is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
rewrite_assign_rhs_with_comments, rewrite_else_kw_with_comments, RhsAssignKind, RhsTactics,
rewrite_assign_rhs_with_comments, rewrite_else_kw_with_comments, rewrite_let_else_block,
RhsAssignKind, RhsTactics,
};
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
use crate::macros::{rewrite_macro, MacroPosition};
@ -132,7 +133,13 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>
shape,
);
result.push_str(&else_kw);
result.push_str(&block.rewrite(context, shape)?);
let allow_single_line = !result.contains('\n');
result.push_str(&rewrite_let_else_block(
block,
allow_single_line,
context,
shape,
)?);
};
}