Skip to content

Commit 4dc01f9

Browse files
committed
Prevent single-line let-else if it would exceed max_width
1 parent a44fe50 commit 4dc01f9

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/items.rs

+27-7
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,21 @@ impl Rewrite for ast::Local {
137137
shape,
138138
);
139139
result.push_str(&else_kw);
140-
let allow_single_line = !result.contains('\n');
141-
result.push_str(&rewrite_let_else_block(
142-
block,
143-
allow_single_line,
144-
context,
145-
shape,
146-
)?);
140+
141+
let allow_single_line = allow_single_line_let_else_block(&result, block);
142+
143+
let mut rw_else_block =
144+
rewrite_let_else_block(block, allow_single_line, context, shape)?;
145+
146+
if allow_single_line && !rw_else_block.contains('\n') {
147+
let available_space = shape.width.saturating_sub(result.len());
148+
if available_space <= rw_else_block.len() {
149+
// writing this on one line would exceed the available width
150+
rw_else_block = rewrite_let_else_block(block, false, context, shape)?;
151+
}
152+
}
153+
154+
result.push_str(&rw_else_block);
147155
};
148156
}
149157

@@ -193,6 +201,18 @@ fn same_line_else_kw_and_brace(
193201
.map_or(false, |l| !l.starts_with(char::is_whitespace))
194202
}
195203

204+
fn allow_single_line_let_else_block(result: &str, block: &ast::Block) -> bool {
205+
if result.contains('\n') {
206+
return false;
207+
}
208+
209+
if block.stmts.len() <= 1 {
210+
return true;
211+
}
212+
213+
false
214+
}
215+
196216
// FIXME convert to using rewrite style rather than visitor
197217
// FIXME format modules in this style
198218
#[allow(dead_code)]

0 commit comments

Comments
 (0)