Add config option to more aggressively allow overflow
This commit is contained in:
parent
16d5f507b6
commit
25b828d35f
5 changed files with 222 additions and 8 deletions
|
|
@ -88,8 +88,10 @@ create_config! {
|
|||
// Misc.
|
||||
remove_nested_parens: bool, true, true, "Remove nested parens";
|
||||
combine_control_expr: bool, true, false, "Combine control expressions with function calls";
|
||||
struct_field_align_threshold: usize, 0, false, "Align struct fields if their diffs fits within \
|
||||
threshold";
|
||||
overflow_delimited_expr: bool, false, false,
|
||||
"Allow trailing bracket/brace delimited expressions to overflow";
|
||||
struct_field_align_threshold: usize, 0, false,
|
||||
"Align struct fields if their diffs fits within threshold";
|
||||
enum_discrim_align_threshold: usize, 0, false,
|
||||
"Align enum variants discrims, if their diffs fit within threshold";
|
||||
match_arm_blocks: bool, true, false, "Wrap the body of arms in blocks when it does not fit on \
|
||||
|
|
|
|||
31
src/expr.rs
31
src/expr.rs
|
|
@ -1343,12 +1343,31 @@ pub fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_l
|
|||
ast::ExprKind::Block(..) | ast::ExprKind::Closure(..) => {
|
||||
context.use_block_indent() || context.config.indent_style() == IndentStyle::Visual
|
||||
}
|
||||
ast::ExprKind::Array(..)
|
||||
| ast::ExprKind::Call(..)
|
||||
| ast::ExprKind::Mac(..)
|
||||
| ast::ExprKind::MethodCall(..)
|
||||
| ast::ExprKind::Struct(..)
|
||||
| ast::ExprKind::Tup(..) => context.use_block_indent() && args_len == 1,
|
||||
|
||||
// Handle `[]` and `{}`-like expressions
|
||||
ast::ExprKind::Array(..) | ast::ExprKind::Struct(..) => {
|
||||
if context.config.overflow_delimited_expr() {
|
||||
context.use_block_indent() || context.config.indent_style() == IndentStyle::Visual
|
||||
} else {
|
||||
context.use_block_indent() && args_len == 1
|
||||
}
|
||||
}
|
||||
ast::ExprKind::Mac(ref macro_) => {
|
||||
match (macro_.node.delim, context.config.overflow_delimited_expr()) {
|
||||
(ast::MacDelimiter::Bracket, true) | (ast::MacDelimiter::Brace, true) => {
|
||||
context.use_block_indent()
|
||||
|| context.config.indent_style() == IndentStyle::Visual
|
||||
}
|
||||
_ => context.use_block_indent() && args_len == 1,
|
||||
}
|
||||
}
|
||||
|
||||
// Handle parenthetical expressions
|
||||
ast::ExprKind::Call(..) | ast::ExprKind::MethodCall(..) | ast::ExprKind::Tup(..) => {
|
||||
context.use_block_indent() && args_len == 1
|
||||
}
|
||||
|
||||
// Handle unary-like expressions
|
||||
ast::ExprKind::AddrOf(_, ref expr)
|
||||
| ast::ExprKind::Box(ref expr)
|
||||
| ast::ExprKind::Try(ref expr)
|
||||
|
|
|
|||
|
|
@ -404,6 +404,7 @@ impl<'a> Context<'a> {
|
|||
closures::rewrite_last_closure(self.context, expr, shape)
|
||||
}
|
||||
}
|
||||
|
||||
// When overflowing the expressions which consists of a control flow
|
||||
// expression, avoid condition to use multi line.
|
||||
ast::ExprKind::If(..)
|
||||
|
|
@ -422,6 +423,7 @@ impl<'a> Context<'a> {
|
|||
expr.rewrite(self.context, shape)
|
||||
}
|
||||
}
|
||||
|
||||
_ => expr.rewrite(self.context, shape),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
111
tests/source/expr-overflow-delimited.rs
Normal file
111
tests/source/expr-overflow-delimited.rs
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
// rustfmt-overflow_delimited_expr: true
|
||||
|
||||
fn combine_blocklike() {
|
||||
do_thing(
|
||||
|param| {
|
||||
action();
|
||||
foo(param)
|
||||
},
|
||||
);
|
||||
|
||||
do_thing(
|
||||
x,
|
||||
|param| {
|
||||
action();
|
||||
foo(param)
|
||||
},
|
||||
);
|
||||
|
||||
do_thing(
|
||||
Bar {
|
||||
x: value,
|
||||
y: value2,
|
||||
},
|
||||
);
|
||||
|
||||
do_thing(
|
||||
x,
|
||||
Bar {
|
||||
x: value,
|
||||
y: value2,
|
||||
},
|
||||
);
|
||||
|
||||
do_thing(
|
||||
&[
|
||||
value_with_longer_name,
|
||||
value2_with_longer_name,
|
||||
value3_with_longer_name,
|
||||
value4_with_longer_name,
|
||||
],
|
||||
);
|
||||
|
||||
do_thing(
|
||||
x,
|
||||
&[
|
||||
value_with_longer_name,
|
||||
value2_with_longer_name,
|
||||
value3_with_longer_name,
|
||||
value4_with_longer_name,
|
||||
],
|
||||
);
|
||||
|
||||
do_thing(
|
||||
vec![
|
||||
value_with_longer_name,
|
||||
value2_with_longer_name,
|
||||
value3_with_longer_name,
|
||||
value4_with_longer_name,
|
||||
],
|
||||
);
|
||||
|
||||
do_thing(
|
||||
x,
|
||||
vec![
|
||||
value_with_longer_name,
|
||||
value2_with_longer_name,
|
||||
value3_with_longer_name,
|
||||
value4_with_longer_name,
|
||||
],
|
||||
);
|
||||
|
||||
do_thing(
|
||||
x,
|
||||
(
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
|param| {
|
||||
action();
|
||||
foo(param)
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
fn combine_struct_sample() {
|
||||
let identity = verify(
|
||||
&ctx,
|
||||
VerifyLogin {
|
||||
type_: LoginType::Username,
|
||||
username: args.username.clone(),
|
||||
password: Some(args.password.clone()),
|
||||
domain: None,
|
||||
},
|
||||
)?;
|
||||
}
|
||||
|
||||
fn combine_macro_sample() {
|
||||
rocket::ignite()
|
||||
.mount(
|
||||
"/",
|
||||
routes![
|
||||
http::auth::login,
|
||||
http::auth::logout,
|
||||
http::cors::options,
|
||||
http::action::dance,
|
||||
http::action::sleep,
|
||||
],
|
||||
)
|
||||
.launch();
|
||||
}
|
||||
80
tests/target/expr-overflow-delimited.rs
Normal file
80
tests/target/expr-overflow-delimited.rs
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
// rustfmt-overflow_delimited_expr: true
|
||||
|
||||
fn combine_blocklike() {
|
||||
do_thing(|param| {
|
||||
action();
|
||||
foo(param)
|
||||
});
|
||||
|
||||
do_thing(x, |param| {
|
||||
action();
|
||||
foo(param)
|
||||
});
|
||||
|
||||
do_thing(Bar {
|
||||
x: value,
|
||||
y: value2,
|
||||
});
|
||||
|
||||
do_thing(x, Bar {
|
||||
x: value,
|
||||
y: value2,
|
||||
});
|
||||
|
||||
do_thing(&[
|
||||
value_with_longer_name,
|
||||
value2_with_longer_name,
|
||||
value3_with_longer_name,
|
||||
value4_with_longer_name,
|
||||
]);
|
||||
|
||||
do_thing(x, &[
|
||||
value_with_longer_name,
|
||||
value2_with_longer_name,
|
||||
value3_with_longer_name,
|
||||
value4_with_longer_name,
|
||||
]);
|
||||
|
||||
do_thing(vec![
|
||||
value_with_longer_name,
|
||||
value2_with_longer_name,
|
||||
value3_with_longer_name,
|
||||
value4_with_longer_name,
|
||||
]);
|
||||
|
||||
do_thing(x, vec![
|
||||
value_with_longer_name,
|
||||
value2_with_longer_name,
|
||||
value3_with_longer_name,
|
||||
value4_with_longer_name,
|
||||
]);
|
||||
|
||||
do_thing(
|
||||
x,
|
||||
(1, 2, 3, |param| {
|
||||
action();
|
||||
foo(param)
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
fn combine_struct_sample() {
|
||||
let identity = verify(&ctx, VerifyLogin {
|
||||
type_: LoginType::Username,
|
||||
username: args.username.clone(),
|
||||
password: Some(args.password.clone()),
|
||||
domain: None,
|
||||
})?;
|
||||
}
|
||||
|
||||
fn combine_macro_sample() {
|
||||
rocket::ignite()
|
||||
.mount("/", routes![
|
||||
http::auth::login,
|
||||
http::auth::logout,
|
||||
http::cors::options,
|
||||
http::action::dance,
|
||||
http::action::sleep,
|
||||
])
|
||||
.launch();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue