Skip to content

Commit

Permalink
Merge pull request rust-lang#3078 from YaLTeR/fix-closure-indentation
Browse files Browse the repository at this point in the history
Fix inconsistent overflow behavior in Visual style
  • Loading branch information
nrc authored Oct 8, 2018
2 parents 832a912 + dee6843 commit 1b2e727
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 43 deletions.
10 changes: 9 additions & 1 deletion src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,15 @@ impl<'a> ChainFormatterShared<'a> {
if all_in_one_line || extendable {
// First we try to 'overflow' the last child and see if it looks better than using
// vertical layout.
if let Some(one_line_shape) = last_shape.offset_left(almost_total) {
let one_line_shape = if context.use_block_indent() {
last_shape.offset_left(almost_total)
} else {
last_shape
.visual_indent(almost_total)
.sub_width(almost_total)
};

if let Some(one_line_shape) = one_line_shape {
if let Some(rw) = last.rewrite(context, one_line_shape) {
// We allow overflowing here only if both of the following conditions match:
// 1. The entire chain fits in a single line except the last child.
Expand Down
3 changes: 1 addition & 2 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1326,8 +1326,7 @@ pub fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_l
context.config.combine_control_expr() && context.use_block_indent() && args_len == 1
}
ast::ExprKind::Block(..) | ast::ExprKind::Closure(..) => {
context.use_block_indent()
|| context.config.indent_style() == IndentStyle::Visual && args_len > 1
context.use_block_indent() || context.config.indent_style() == IndentStyle::Visual
}
ast::ExprKind::Array(..)
| ast::ExprKind::Call(..)
Expand Down
45 changes: 45 additions & 0 deletions tests/source/issue-3049.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// rustfmt-indent_style: Visual
fn main() {
something.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.bench_function(|| {
let x = hello();
});

something.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.bench_function(arg, || {
let x = hello();
});

something.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.bench_function(arg,
|| {
let x = hello();
},
arg);

AAAAAAAAAAA.function(|| {
let _ = ();
});

AAAAAAAAAAA.chain().function(|| {
let _ = ();
})
}
40 changes: 20 additions & 20 deletions tests/target/chains-visual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ fn main() {

fffffffffffffffffffffffffffffffffff(a, {
SCRIPT_TASK_ROOT.with(|root| {
*root.borrow_mut() = Some(&script_task);
});
*root.borrow_mut() = Some(&script_task);
});
});

let suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuum =
Expand All @@ -47,9 +47,9 @@ fn main() {
.fold(0, |acc, x| acc + x);

aaaaaaaaaaaaaaaa.map(|x| {
x += 1;
x
})
x += 1;
x
})
.filter(some_mod::some_filter)
}

Expand Down Expand Up @@ -83,16 +83,16 @@ fn floaters() {
.baz();

Foo { x: val }.baz(|| {
force();
multiline();
})
force();
multiline();
})
.quux();

Foo { y: i_am_multi_line,
z: ok }.baz(|| {
force();
multiline();
})
force();
multiline();
})
.quux();

a + match x {
Expand Down Expand Up @@ -137,22 +137,22 @@ fn issue1434() {
for _ in 0..100 {
let prototype_id =
PrototypeIdData::from_reader::<_, B>(&mut self.file_cursor).chain_err(|| {
format!("could not read prototype ID at offset {:#010x}",
current_offset)
})?;
format!("could not read prototype ID at offset {:#010x}",
current_offset)
})?;
}
}

fn issue2264() {
{
something.function()
.map(|| {
if let a_very_very_very_very_very_very_very_very_long_variable =
compute_this_variable()
{
println!("Hello");
}
})
if let a_very_very_very_very_very_very_very_very_long_variable =
compute_this_variable()
{
println!("Hello");
}
})
.collect();
}
}
40 changes: 20 additions & 20 deletions tests/target/issue-2985.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ fn foo() {
{
let extra_encoder_settings = extra_encoder_settings.iter()
.filter_map(|&(name, value)| {
value.split()
.next()
.something()
.something2()
.something3()
.something4()
});
value.split()
.next()
.something()
.something2()
.something3()
.something4()
});
let extra_encoder_settings = extra_encoder_settings.iter()
.filter_map(|&(name, value)| {
value.split()
.next()
.something()
.something2()
.something3()
.something4()
})
value.split()
.next()
.something()
.something2()
.something3()
.something4()
})
.something();
if let Some(subpod) = pod.subpods.iter().find(|s| {
!s.plaintext
.as_ref()
.map(String::as_ref)
.unwrap_or("")
.is_empty()
}) {
!s.plaintext
.as_ref()
.map(String::as_ref)
.unwrap_or("")
.is_empty()
}) {
do_something();
}
}
Expand Down
45 changes: 45 additions & 0 deletions tests/target/issue-3049.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// rustfmt-indent_style: Visual
fn main() {
something.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.bench_function(|| {
let x = hello();
});

something.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.bench_function(arg, || {
let x = hello();
});

something.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.aaaaaaaaaaaa()
.bench_function(arg,
|| {
let x = hello();
},
arg);

AAAAAAAAAAA.function(|| {
let _ = ();
});

AAAAAAAAAAA.chain().function(|| {
let _ = ();
})
}

0 comments on commit 1b2e727

Please sign in to comment.