Initial pass at implementing let-else

This commit is contained in:
Yacin Tmimi 2023-01-16 12:19:47 -05:00 committed by Caleb Cartwright
parent 75870c55b9
commit 9316df0ca2
3 changed files with 37 additions and 5 deletions

View File

@ -18,7 +18,7 @@
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, RhsAssignKind, RhsTactics,
rewrite_assign_rhs_with_comments, rewrite_else_kw_with_comments, RhsAssignKind, RhsTactics,
};
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
use crate::macros::{rewrite_macro, MacroPosition};
@ -44,7 +44,7 @@ fn type_annotation_separator(config: &Config) -> &str {
}
// Statements of the form
// let pat: ty = init;
// let pat: ty = init; or let pat: ty = init else { .. };
impl Rewrite for ast::Local {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
debug!(
@ -54,7 +54,7 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>
skip_out_of_file_lines_range!(context, self.span);
if contains_skip(&self.attrs) || matches!(self.kind, ast::LocalKind::InitElse(..)) {
if contains_skip(&self.attrs) {
return None;
}
@ -112,7 +112,7 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>
result.push_str(&infix);
if let Some((init, _els)) = self.kind.init_else_opt() {
if let Some((init, else_block)) = self.kind.init_else_opt() {
// 1 = trailing semicolon;
let nested_shape = shape.sub_width(1)?;
@ -123,7 +123,17 @@ fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String>
&RhsAssignKind::Expr(&init.kind, init.span),
nested_shape,
)?;
// todo else
if let Some(block) = else_block {
let else_kw = rewrite_else_kw_with_comments(
true,
context,
init.span.between(block.span),
shape,
);
result.push_str(&else_kw);
result.push_str(&block.rewrite(context, shape)?);
};
}
result.push(';');

10
tests/source/let_else.rs Normal file
View File

@ -0,0 +1,10 @@
fn main() {
let Some(x) = opt else { return };
let Some(x) = opt else { return; };
let Some(x) = opt else {
// nope
return;
};
}

12
tests/target/let_else.rs Normal file
View File

@ -0,0 +1,12 @@
fn main() {
let Some(x) = opt else { return };
let Some(x) = opt else {
return;
};
let Some(x) = opt else {
// nope
return;
};
}