Prevent line breaking at = or in after trivial patterns

This commit is contained in:
sinkuu 2016-09-09 23:20:16 +09:00
parent 4418fab4f2
commit b6bffa6596
5 changed files with 48 additions and 14 deletions

View File

@ -1307,20 +1307,28 @@ fn rewrite_pat_expr(context: &RewriteContext,
offset + extra_offset + spacer.len());
if let Some(expr_string) = expr_rewrite {
let pat_simple =
pat.and_then(|p| p.rewrite(context, context.config.max_width, Indent::empty()))
.map(|s| pat_is_simple(&s));
if pat.is_none() || pat_simple.unwrap_or(false) || !expr_string.contains('\n') {
result.push_str(spacer);
result.push_str(&expr_string);
return Some(result);
}
}
}
let nested = context.nested_context();
// The expression won't fit on the current line, jump to next.
result.push('\n');
result.push_str(&pat_offset.to_string(context.config));
result.push_str(&nested.block_indent.to_string(context.config));
let expr_rewrite =
expr.rewrite(context,
try_opt!(context.config.max_width.checked_sub(pat_offset.width())),
pat_offset);
expr.rewrite(&nested,
try_opt!(context.config.max_width.checked_sub(nested.block_indent.width())),
nested.block_indent);
result.push_str(&try_opt!(expr_rewrite));
Some(result)

View File

@ -285,3 +285,16 @@ fn complex_if_else() {
yo();
}
}
fn issue1106() {
{
if let hir::ItemEnum(ref enum_def, ref generics) = self.ast_map.expect_item(enum_node_id).node {
}
}
for entry in
WalkDir::new(path)
.into_iter()
.filter_entry(|entry| exclusions.filter_entry(entry)) {
}
}

View File

@ -55,8 +55,8 @@ fn foo() -> bool {
}
if let (some_very_large,
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1111 +
2222 {}
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) =
1111 + 2222 {}
if let (some_very_large,
tuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuple) = 1 + 2 + 3 {
@ -290,3 +290,16 @@ fn complex_if_else() {
yo();
}
}
fn issue1106() {
{
if let hir::ItemEnum(ref enum_def, ref generics) =
self.ast_map.expect_item(enum_node_id).node {
}
}
for entry in WalkDir::new(path)
.into_iter()
.filter_entry(|entry| exclusions.filter_entry(entry)) {
}
}

View File

@ -21,8 +21,8 @@ fn main() {
while aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {
}
'b: for xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx in some_iter(arg1,
arg2) {
'b: for xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx in
some_iter(arg1, arg2) {
// do smth
}