Merge pull request #1156 from sinkuu/break_if_let

Prefer line break at `=`/`in`
This commit is contained in:
Nick Cameron 2016-09-19 18:51:21 +12:00 committed by GitHub
commit 3ff7e83457
5 changed files with 48 additions and 14 deletions

View File

@ -1322,20 +1322,28 @@ fn rewrite_pat_expr(context: &RewriteContext,
offset + extra_offset + spacer.len());
if let Some(expr_string) = expr_rewrite {
result.push_str(spacer);
result.push_str(&expr_string);
return Some(result);
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

@ -568,7 +568,7 @@ fn format_impl_ref_and_type(context: &RewriteContext,
split_at_for: bool)
-> Option<String> {
if let ast::ItemKind::Impl(unsafety, polarity, ref generics, ref trait_ref, ref self_ty, _) =
item.node {
item.node {
let mut result = String::new();
result.push_str(&*format_visibility(&item.vis));
@ -670,7 +670,7 @@ pub fn format_struct(context: &RewriteContext,
pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) -> Option<String> {
if let ast::ItemKind::Trait(unsafety, ref generics, ref type_param_bounds, ref trait_items) =
item.node {
item.node {
let mut result = String::new();
let header = format!("{}{}trait {}",
format_visibility(&item.vis),

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 {
@ -283,10 +283,23 @@ fn complex_if_else() {
} else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + xxxxxxxx {
yo();
} else if let Some(x) =
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {
ha();
} else if xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +
xxxxxxxxx {
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
}