diff --git a/src/items.rs b/src/items.rs index 3ecdb5b4c607..75c42605ccf0 100644 --- a/src/items.rs +++ b/src/items.rs @@ -18,7 +18,7 @@ use crate::config::lists::*; 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 { debug!( @@ -54,7 +54,7 @@ impl Rewrite for ast::Local { 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 @@ impl Rewrite for ast::Local { 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 @@ impl Rewrite for ast::Local { &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(';'); diff --git a/tests/source/let_else.rs b/tests/source/let_else.rs new file mode 100644 index 000000000000..26e0ebf07364 --- /dev/null +++ b/tests/source/let_else.rs @@ -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; + }; +} diff --git a/tests/target/let_else.rs b/tests/target/let_else.rs new file mode 100644 index 000000000000..a910f5389a3a --- /dev/null +++ b/tests/target/let_else.rs @@ -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; + }; +}