Skip to content

Commit e221689

Browse files
committed
Initial pass at implementing let-else
1 parent 5c552b9 commit e221689

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

Diff for: src/items.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ 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, RhsAssignKind, RhsTactics,
21+
rewrite_assign_rhs_with_comments, rewrite_else_kw_with_comments, RhsAssignKind, RhsTactics,
2222
};
2323
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
2424
use crate::macros::{rewrite_macro, MacroPosition};
@@ -44,7 +44,7 @@ fn type_annotation_separator(config: &Config) -> &str {
4444
}
4545

4646
// Statements of the form
47-
// let pat: ty = init;
47+
// let pat: ty = init; or let pat: ty = init else { .. };
4848
impl Rewrite for ast::Local {
4949
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
5050
debug!(
@@ -54,7 +54,7 @@ impl Rewrite for ast::Local {
5454

5555
skip_out_of_file_lines_range!(context, self.span);
5656

57-
if contains_skip(&self.attrs) || matches!(self.kind, ast::LocalKind::InitElse(..)) {
57+
if contains_skip(&self.attrs) {
5858
return None;
5959
}
6060

@@ -112,7 +112,7 @@ impl Rewrite for ast::Local {
112112

113113
result.push_str(&infix);
114114

115-
if let Some((init, _els)) = self.kind.init_else_opt() {
115+
if let Some((init, else_block)) = self.kind.init_else_opt() {
116116
// 1 = trailing semicolon;
117117
let nested_shape = shape.sub_width(1)?;
118118

@@ -123,7 +123,17 @@ impl Rewrite for ast::Local {
123123
&RhsAssignKind::Expr(&init.kind, init.span),
124124
nested_shape,
125125
)?;
126-
// todo else
126+
127+
if let Some(block) = else_block {
128+
let else_kw = rewrite_else_kw_with_comments(
129+
true,
130+
context,
131+
init.span.between(block.span),
132+
shape,
133+
);
134+
result.push_str(&else_kw);
135+
result.push_str(&block.rewrite(context, shape)?);
136+
};
127137
}
128138

129139
result.push(';');

Diff for: tests/source/let_else.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn main() {
2+
let Some(x) = opt else { return };
3+
4+
let Some(x) = opt else {
5+
return;
6+
};
7+
8+
let Some(x) = opt else {
9+
// nope
10+
return;
11+
};
12+
}

Diff for: tests/target/let_else.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn main() {
2+
let Some(x) = opt else { return };
3+
4+
let Some(x) = opt else {
5+
return;
6+
};
7+
8+
let Some(x) = opt else {
9+
// nope
10+
return;
11+
};
12+
}

0 commit comments

Comments
 (0)