Prevent single-line let-else
if it would exceed max_width
This commit is contained in:
parent
00fef2d51d
commit
521f86bae5
34
src/items.rs
34
src/items.rs
@ -137,13 +137,21 @@ impl Rewrite for ast::Local {
|
|||||||
shape,
|
shape,
|
||||||
);
|
);
|
||||||
result.push_str(&else_kw);
|
result.push_str(&else_kw);
|
||||||
let allow_single_line = !result.contains('\n');
|
|
||||||
result.push_str(&rewrite_let_else_block(
|
let allow_single_line = allow_single_line_let_else_block(&result, block);
|
||||||
block,
|
|
||||||
allow_single_line,
|
let mut rw_else_block =
|
||||||
context,
|
rewrite_let_else_block(block, allow_single_line, context, shape)?;
|
||||||
shape,
|
|
||||||
)?);
|
if allow_single_line && !rw_else_block.contains('\n') {
|
||||||
|
let available_space = shape.width.saturating_sub(result.len());
|
||||||
|
if available_space <= rw_else_block.len() {
|
||||||
|
// writing this on one line would exceed the available width
|
||||||
|
rw_else_block = rewrite_let_else_block(block, false, context, shape)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result.push_str(&rw_else_block);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -193,6 +201,18 @@ fn same_line_else_kw_and_brace(
|
|||||||
.map_or(false, |l| !l.starts_with(char::is_whitespace))
|
.map_or(false, |l| !l.starts_with(char::is_whitespace))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn allow_single_line_let_else_block(result: &str, block: &ast::Block) -> bool {
|
||||||
|
if result.contains('\n') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if block.stmts.len() <= 1 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME convert to using rewrite style rather than visitor
|
// FIXME convert to using rewrite style rather than visitor
|
||||||
// FIXME format modules in this style
|
// FIXME format modules in this style
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user