Skip to content

Commit 99efd73

Browse files
authored
Permit ranges on multiple lines in closures, without an enclosing block (#4309)
The problem here was that if an expression in a closure fails to be formatted without an enclosing block, as in the case of a multiline range prior to this commit, we try to put the expression in the block. But then we try to check what expressions should be "vetoed" from being in a block, which includes ranges. The non-breaking fix is to let multiline ranges be formatted without a block. On the big picture, though, it may be the case that there is no need for a "veto" check when formatting expressions in a closure block, or that there is a better way to structure the work being done here. I added a todo for this, since it's relatively low priority (IMO). Closes #4308
1 parent 8e8db1f commit 99efd73

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/formatting/closures.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ fn needs_block(block: &ast::Block, prefix: &str, context: &RewriteContext<'_>) -
121121
|| prefix.contains('\n')
122122
}
123123

124+
// TODO: The expressions "veto"ed here should align with expressions that are permitted on multiple
125+
// lines in rewrite_closure_expr#allow_multi_line. Consider refactoring to avoid this disparity.
124126
fn veto_block(e: &ast::Expr) -> bool {
125127
match e.kind {
126128
ast::ExprKind::Call(..)
@@ -179,7 +181,8 @@ fn rewrite_closure_expr(
179181
| ast::ExprKind::Block(..)
180182
| ast::ExprKind::TryBlock(..)
181183
| ast::ExprKind::Loop(..)
182-
| ast::ExprKind::Struct(..) => true,
184+
| ast::ExprKind::Struct(..)
185+
| ast::ExprKind::Range(..) => true,
183186

184187
ast::ExprKind::AddrOf(_, _, ref expr)
185188
| ast::ExprKind::Box(ref expr)

tests/source/issue-4308.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn main() {
2+
let requires_multiline = 7;
3+
4+
let _ = {
5+
|| if true {
6+
requires_multiline
7+
} else {
8+
requires_multiline
9+
}
10+
..19;
11+
};
12+
}

tests/target/issue-4308.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn main() {
2+
let requires_multiline = 7;
3+
4+
let _ = {
5+
|| if true {
6+
requires_multiline
7+
} else {
8+
requires_multiline
9+
}..19;
10+
};
11+
}

0 commit comments

Comments
 (0)