diff options
| author | Yacin Tmimi <yacintmimi@gmail.com> | 2023-01-16 12:19:47 -0500 |
|---|---|---|
| committer | Caleb Cartwright <calebcartwright@users.noreply.github.com> | 2023-06-20 08:26:11 -0500 |
| commit | 9316df0ca2e709258d0d146fb311b24222116547 (patch) | |
| tree | c62d3f60703457867c43da992b92da150d7697f8 | |
| parent | 75870c55b94b1b927445a35131462fb9db402fd3 (diff) | |
| download | rust-9316df0ca2e709258d0d146fb311b24222116547.tar.gz rust-9316df0ca2e709258d0d146fb311b24222116547.zip | |
Initial pass at implementing let-else
| -rw-r--r-- | src/items.rs | 20 | ||||
| -rw-r--r-- | tests/source/let_else.rs | 10 | ||||
| -rw-r--r-- | tests/target/let_else.rs | 12 |
3 files changed, 37 insertions, 5 deletions
diff --git a/src/items.rs b/src/items.rs index 3ecdb5b4c60..75c42605ccf 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<String> { 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 00000000000..26e0ebf0736 --- /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 00000000000..a910f5389a3 --- /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; + }; +} |
