Skip to content

Commit 1b2e727

Browse files
authored
Merge pull request rust-lang#3078 from YaLTeR/fix-closure-indentation
Fix inconsistent overflow behavior in Visual style
2 parents 832a912 + dee6843 commit 1b2e727

File tree

6 files changed

+140
-43
lines changed

6 files changed

+140
-43
lines changed

src/chains.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,15 @@ impl<'a> ChainFormatterShared<'a> {
576576
if all_in_one_line || extendable {
577577
// First we try to 'overflow' the last child and see if it looks better than using
578578
// vertical layout.
579-
if let Some(one_line_shape) = last_shape.offset_left(almost_total) {
579+
let one_line_shape = if context.use_block_indent() {
580+
last_shape.offset_left(almost_total)
581+
} else {
582+
last_shape
583+
.visual_indent(almost_total)
584+
.sub_width(almost_total)
585+
};
586+
587+
if let Some(one_line_shape) = one_line_shape {
580588
if let Some(rw) = last.rewrite(context, one_line_shape) {
581589
// We allow overflowing here only if both of the following conditions match:
582590
// 1. The entire chain fits in a single line except the last child.

src/expr.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,8 +1326,7 @@ pub fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_l
13261326
context.config.combine_control_expr() && context.use_block_indent() && args_len == 1
13271327
}
13281328
ast::ExprKind::Block(..) | ast::ExprKind::Closure(..) => {
1329-
context.use_block_indent()
1330-
|| context.config.indent_style() == IndentStyle::Visual && args_len > 1
1329+
context.use_block_indent() || context.config.indent_style() == IndentStyle::Visual
13311330
}
13321331
ast::ExprKind::Array(..)
13331332
| ast::ExprKind::Call(..)

tests/source/issue-3049.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// rustfmt-indent_style: Visual
2+
fn main() {
3+
something.aaaaaaaaaaaa()
4+
.aaaaaaaaaaaa()
5+
.aaaaaaaaaaaa()
6+
.aaaaaaaaaaaa()
7+
.aaaaaaaaaaaa()
8+
.aaaaaaaaaaaa()
9+
.aaaaaaaaaaaa()
10+
.bench_function(|| {
11+
let x = hello();
12+
});
13+
14+
something.aaaaaaaaaaaa()
15+
.aaaaaaaaaaaa()
16+
.aaaaaaaaaaaa()
17+
.aaaaaaaaaaaa()
18+
.aaaaaaaaaaaa()
19+
.aaaaaaaaaaaa()
20+
.aaaaaaaaaaaa()
21+
.bench_function(arg, || {
22+
let x = hello();
23+
});
24+
25+
something.aaaaaaaaaaaa()
26+
.aaaaaaaaaaaa()
27+
.aaaaaaaaaaaa()
28+
.aaaaaaaaaaaa()
29+
.aaaaaaaaaaaa()
30+
.aaaaaaaaaaaa()
31+
.aaaaaaaaaaaa()
32+
.bench_function(arg,
33+
|| {
34+
let x = hello();
35+
},
36+
arg);
37+
38+
AAAAAAAAAAA.function(|| {
39+
let _ = ();
40+
});
41+
42+
AAAAAAAAAAA.chain().function(|| {
43+
let _ = ();
44+
})
45+
}

tests/target/chains-visual.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ fn main() {
3737

3838
fffffffffffffffffffffffffffffffffff(a, {
3939
SCRIPT_TASK_ROOT.with(|root| {
40-
*root.borrow_mut() = Some(&script_task);
41-
});
40+
*root.borrow_mut() = Some(&script_task);
41+
});
4242
});
4343

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

4949
aaaaaaaaaaaaaaaa.map(|x| {
50-
x += 1;
51-
x
52-
})
50+
x += 1;
51+
x
52+
})
5353
.filter(some_mod::some_filter)
5454
}
5555

@@ -83,16 +83,16 @@ fn floaters() {
8383
.baz();
8484

8585
Foo { x: val }.baz(|| {
86-
force();
87-
multiline();
88-
})
86+
force();
87+
multiline();
88+
})
8989
.quux();
9090

9191
Foo { y: i_am_multi_line,
9292
z: ok }.baz(|| {
93-
force();
94-
multiline();
95-
})
93+
force();
94+
multiline();
95+
})
9696
.quux();
9797

9898
a + match x {
@@ -137,22 +137,22 @@ fn issue1434() {
137137
for _ in 0..100 {
138138
let prototype_id =
139139
PrototypeIdData::from_reader::<_, B>(&mut self.file_cursor).chain_err(|| {
140-
format!("could not read prototype ID at offset {:#010x}",
141-
current_offset)
142-
})?;
140+
format!("could not read prototype ID at offset {:#010x}",
141+
current_offset)
142+
})?;
143143
}
144144
}
145145

146146
fn issue2264() {
147147
{
148148
something.function()
149149
.map(|| {
150-
if let a_very_very_very_very_very_very_very_very_long_variable =
151-
compute_this_variable()
152-
{
153-
println!("Hello");
154-
}
155-
})
150+
if let a_very_very_very_very_very_very_very_very_long_variable =
151+
compute_this_variable()
152+
{
153+
println!("Hello");
154+
}
155+
})
156156
.collect();
157157
}
158158
}

tests/target/issue-2985.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,30 @@ fn foo() {
44
{
55
let extra_encoder_settings = extra_encoder_settings.iter()
66
.filter_map(|&(name, value)| {
7-
value.split()
8-
.next()
9-
.something()
10-
.something2()
11-
.something3()
12-
.something4()
13-
});
7+
value.split()
8+
.next()
9+
.something()
10+
.something2()
11+
.something3()
12+
.something4()
13+
});
1414
let extra_encoder_settings = extra_encoder_settings.iter()
1515
.filter_map(|&(name, value)| {
16-
value.split()
17-
.next()
18-
.something()
19-
.something2()
20-
.something3()
21-
.something4()
22-
})
16+
value.split()
17+
.next()
18+
.something()
19+
.something2()
20+
.something3()
21+
.something4()
22+
})
2323
.something();
2424
if let Some(subpod) = pod.subpods.iter().find(|s| {
25-
!s.plaintext
26-
.as_ref()
27-
.map(String::as_ref)
28-
.unwrap_or("")
29-
.is_empty()
30-
}) {
25+
!s.plaintext
26+
.as_ref()
27+
.map(String::as_ref)
28+
.unwrap_or("")
29+
.is_empty()
30+
}) {
3131
do_something();
3232
}
3333
}

tests/target/issue-3049.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// rustfmt-indent_style: Visual
2+
fn main() {
3+
something.aaaaaaaaaaaa()
4+
.aaaaaaaaaaaa()
5+
.aaaaaaaaaaaa()
6+
.aaaaaaaaaaaa()
7+
.aaaaaaaaaaaa()
8+
.aaaaaaaaaaaa()
9+
.aaaaaaaaaaaa()
10+
.bench_function(|| {
11+
let x = hello();
12+
});
13+
14+
something.aaaaaaaaaaaa()
15+
.aaaaaaaaaaaa()
16+
.aaaaaaaaaaaa()
17+
.aaaaaaaaaaaa()
18+
.aaaaaaaaaaaa()
19+
.aaaaaaaaaaaa()
20+
.aaaaaaaaaaaa()
21+
.bench_function(arg, || {
22+
let x = hello();
23+
});
24+
25+
something.aaaaaaaaaaaa()
26+
.aaaaaaaaaaaa()
27+
.aaaaaaaaaaaa()
28+
.aaaaaaaaaaaa()
29+
.aaaaaaaaaaaa()
30+
.aaaaaaaaaaaa()
31+
.aaaaaaaaaaaa()
32+
.bench_function(arg,
33+
|| {
34+
let x = hello();
35+
},
36+
arg);
37+
38+
AAAAAAAAAAA.function(|| {
39+
let _ = ();
40+
});
41+
42+
AAAAAAAAAAA.chain().function(|| {
43+
let _ = ();
44+
})
45+
}

0 commit comments

Comments
 (0)