Skip to content

Commit 4e2f741

Browse files
authored
Merge pull request rust-lang#3175 from kestred/kstenerson/delimited-overflow
Add config option to more aggressively allow overflow
2 parents 6efc963 + 3330e67 commit 4e2f741

File tree

6 files changed

+384
-10
lines changed

6 files changed

+384
-10
lines changed

Configurations.md

+82
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,88 @@ fn main() {
20892089

20902090
See also: [`match_block_trailing_comma`](#match_block_trailing_comma).
20912091

2092+
## `overflow_delimited_expr`
2093+
2094+
When structs, slices, arrays, and block/array-like macros are used as the last
2095+
argument in an expression list, allow them to overflow (like blocks/closures)
2096+
instead of being indented on a new line.
2097+
2098+
- **Default value**: `false`
2099+
- **Possible values**: `true`, `false`
2100+
- **Stable**: No
2101+
2102+
#### `false` (default):
2103+
2104+
```rust
2105+
fn example() {
2106+
foo(ctx, |param| {
2107+
action();
2108+
foo(param)
2109+
});
2110+
2111+
foo(
2112+
ctx,
2113+
Bar {
2114+
x: value,
2115+
y: value2,
2116+
},
2117+
);
2118+
2119+
foo(
2120+
ctx,
2121+
&[
2122+
MAROON_TOMATOES,
2123+
PURPLE_POTATOES,
2124+
ORGANE_ORANGES,
2125+
GREEN_PEARS,
2126+
RED_APPLES,
2127+
],
2128+
);
2129+
2130+
foo(
2131+
ctx,
2132+
vec![
2133+
MAROON_TOMATOES,
2134+
PURPLE_POTATOES,
2135+
ORGANE_ORANGES,
2136+
GREEN_PEARS,
2137+
RED_APPLES,
2138+
],
2139+
);
2140+
}
2141+
```
2142+
2143+
#### `true`:
2144+
2145+
```rust
2146+
fn example() {
2147+
foo(ctx, |param| {
2148+
action();
2149+
foo(param)
2150+
});
2151+
2152+
foo(ctx, Bar {
2153+
x: value,
2154+
y: value2,
2155+
});
2156+
2157+
foo(ctx, &[
2158+
MAROON_TOMATOES,
2159+
PURPLE_POTATOES,
2160+
ORGANE_ORANGES,
2161+
GREEN_PEARS,
2162+
RED_APPLES,
2163+
]);
2164+
2165+
foo(ctx, vec![
2166+
MAROON_TOMATOES,
2167+
PURPLE_POTATOES,
2168+
ORGANE_ORANGES,
2169+
GREEN_PEARS,
2170+
RED_APPLES,
2171+
]);
2172+
}
2173+
```
20922174

20932175
## `blank_lines_upper_bound`
20942176

src/config/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ create_config! {
8888
// Misc.
8989
remove_nested_parens: bool, true, true, "Remove nested parens";
9090
combine_control_expr: bool, true, false, "Combine control expressions with function calls";
91-
struct_field_align_threshold: usize, 0, false, "Align struct fields if their diffs fits within \
92-
threshold";
91+
overflow_delimited_expr: bool, false, false,
92+
"Allow trailing bracket/brace delimited expressions to overflow";
93+
struct_field_align_threshold: usize, 0, false,
94+
"Align struct fields if their diffs fits within threshold";
9395
enum_discrim_align_threshold: usize, 0, false,
9496
"Align enum variants discrims, if their diffs fit within threshold";
9597
match_arm_blocks: bool, true, false, "Wrap the body of arms in blocks when it does not fit on \

src/expr.rs

+21-8
Original file line numberDiff line numberDiff line change
@@ -1340,15 +1340,28 @@ pub fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_l
13401340
| ast::ExprKind::WhileLet(..) => {
13411341
context.config.combine_control_expr() && context.use_block_indent() && args_len == 1
13421342
}
1343-
ast::ExprKind::Block(..) | ast::ExprKind::Closure(..) => {
1344-
context.use_block_indent() || context.config.indent_style() == IndentStyle::Visual
1343+
1344+
// Handle always block-like expressions
1345+
ast::ExprKind::Block(..) | ast::ExprKind::Closure(..) => true,
1346+
1347+
// Handle `[]` and `{}`-like expressions
1348+
ast::ExprKind::Array(..) | ast::ExprKind::Struct(..) => {
1349+
context.config.overflow_delimited_expr()
1350+
|| (context.use_block_indent() && args_len == 1)
1351+
}
1352+
ast::ExprKind::Mac(ref macro_) => {
1353+
match (macro_.node.delim, context.config.overflow_delimited_expr()) {
1354+
(ast::MacDelimiter::Bracket, true) | (ast::MacDelimiter::Brace, true) => true,
1355+
_ => context.use_block_indent() && args_len == 1,
1356+
}
13451357
}
1346-
ast::ExprKind::Array(..)
1347-
| ast::ExprKind::Call(..)
1348-
| ast::ExprKind::Mac(..)
1349-
| ast::ExprKind::MethodCall(..)
1350-
| ast::ExprKind::Struct(..)
1351-
| ast::ExprKind::Tup(..) => context.use_block_indent() && args_len == 1,
1358+
1359+
// Handle parenthetical expressions
1360+
ast::ExprKind::Call(..) | ast::ExprKind::MethodCall(..) | ast::ExprKind::Tup(..) => {
1361+
context.use_block_indent() && args_len == 1
1362+
}
1363+
1364+
// Handle unary-like expressions
13521365
ast::ExprKind::AddrOf(_, ref expr)
13531366
| ast::ExprKind::Box(ref expr)
13541367
| ast::ExprKind::Try(ref expr)

src/overflow.rs

+2
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ impl<'a> Context<'a> {
404404
closures::rewrite_last_closure(self.context, expr, shape)
405405
}
406406
}
407+
407408
// When overflowing the expressions which consists of a control flow
408409
// expression, avoid condition to use multi line.
409410
ast::ExprKind::If(..)
@@ -422,6 +423,7 @@ impl<'a> Context<'a> {
422423
expr.rewrite(self.context, shape)
423424
}
424425
}
426+
425427
_ => expr.rewrite(self.context, shape),
426428
}
427429
}
+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// rustfmt-overflow_delimited_expr: true
2+
3+
fn combine_blocklike() {
4+
do_thing(
5+
|param| {
6+
action();
7+
foo(param)
8+
},
9+
);
10+
11+
do_thing(
12+
x,
13+
|param| {
14+
action();
15+
foo(param)
16+
},
17+
);
18+
19+
do_thing(
20+
x,
21+
22+
// I'll be discussing the `action` with your para(m)legal counsel
23+
|param| {
24+
action();
25+
foo(param)
26+
},
27+
);
28+
29+
do_thing(
30+
Bar {
31+
x: value,
32+
y: value2,
33+
},
34+
);
35+
36+
do_thing(
37+
x,
38+
Bar {
39+
x: value,
40+
y: value2,
41+
},
42+
);
43+
44+
do_thing(
45+
x,
46+
47+
// Let me tell you about that one time at the `Bar`
48+
Bar {
49+
x: value,
50+
y: value2,
51+
},
52+
);
53+
54+
do_thing(
55+
&[
56+
value_with_longer_name,
57+
value2_with_longer_name,
58+
value3_with_longer_name,
59+
value4_with_longer_name,
60+
],
61+
);
62+
63+
do_thing(
64+
x,
65+
&[
66+
value_with_longer_name,
67+
value2_with_longer_name,
68+
value3_with_longer_name,
69+
value4_with_longer_name,
70+
],
71+
);
72+
73+
do_thing(
74+
x,
75+
76+
// Just admit it; my list is longer than can be folded on to one line
77+
&[
78+
value_with_longer_name,
79+
value2_with_longer_name,
80+
value3_with_longer_name,
81+
value4_with_longer_name,
82+
],
83+
);
84+
85+
do_thing(
86+
vec![
87+
value_with_longer_name,
88+
value2_with_longer_name,
89+
value3_with_longer_name,
90+
value4_with_longer_name,
91+
],
92+
);
93+
94+
do_thing(
95+
x,
96+
vec![
97+
value_with_longer_name,
98+
value2_with_longer_name,
99+
value3_with_longer_name,
100+
value4_with_longer_name,
101+
],
102+
);
103+
104+
do_thing(
105+
x,
106+
107+
// Just admit it; my list is longer than can be folded on to one line
108+
vec![
109+
value_with_longer_name,
110+
value2_with_longer_name,
111+
value3_with_longer_name,
112+
value4_with_longer_name,
113+
],
114+
);
115+
116+
do_thing(
117+
x,
118+
(
119+
1,
120+
2,
121+
3,
122+
|param| {
123+
action();
124+
foo(param)
125+
},
126+
),
127+
);
128+
}
129+
130+
fn combine_struct_sample() {
131+
let identity = verify(
132+
&ctx,
133+
VerifyLogin {
134+
type_: LoginType::Username,
135+
username: args.username.clone(),
136+
password: Some(args.password.clone()),
137+
domain: None,
138+
},
139+
)?;
140+
}
141+
142+
fn combine_macro_sample() {
143+
rocket::ignite()
144+
.mount(
145+
"/",
146+
routes![
147+
http::auth::login,
148+
http::auth::logout,
149+
http::cors::options,
150+
http::action::dance,
151+
http::action::sleep,
152+
],
153+
)
154+
.launch();
155+
}

0 commit comments

Comments
 (0)