Skip to content

Commit e41f32a

Browse files
refactor: include trailing comma on closure block
1 parent 3e94db6 commit e41f32a

5 files changed

+65
-16
lines changed

src/closures.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use syntax::{ast, ptr};
33

44
use crate::attr::get_attrs_from_stmt;
55
use crate::config::lists::*;
6-
use crate::config::{IndentStyle, Version};
6+
use crate::config::{IndentStyle, SeparatorTactic, Version};
77
use crate::expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond};
88
use crate::items::{span_hi_for_param, span_lo_for_param};
99
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
@@ -286,24 +286,29 @@ fn rewrite_closure_fn_decl(
286286
.preserve_newline(true);
287287
let list_str = write_list(&item_vec, &fmt)?;
288288
let one_line_budget = context.budget(param_shape.indent.width());
289-
let (param_str, put_params_in_block) = if match indent_style {
289+
let multi_line_params = match indent_style {
290290
IndentStyle::Block => list_str.contains('\n') || list_str.len() > one_line_budget,
291291
_ => false,
292-
} && !item_vec.is_empty()
293-
&& version == Version::Two
294-
{
295-
(
296-
format!(
297-
"{}{}{}",
298-
param_shape.indent.to_string_with_newline(context.config),
299-
&list_str,
300-
shape.indent.to_string_with_newline(context.config)
301-
),
302-
true,
303-
)
304-
} else {
305-
(list_str, false)
306292
};
293+
let (param_str, put_params_in_block) =
294+
if multi_line_params && !item_vec.is_empty() && version == Version::Two {
295+
let trailing_comma = match context.config.trailing_comma() {
296+
SeparatorTactic::Never => "",
297+
_ => ",",
298+
};
299+
(
300+
format!(
301+
"{}{}{}{}",
302+
param_shape.indent.to_string_with_newline(context.config),
303+
&list_str,
304+
trailing_comma,
305+
shape.indent.to_string_with_newline(context.config)
306+
),
307+
true,
308+
)
309+
} else {
310+
(list_str, false)
311+
};
307312
let mut prefix = format!("{}{}{}|{}|", is_async, immovable, mover, param_str);
308313

309314
if !ret_str.is_empty() {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// rustfmt-indent_style: Block
2+
// rustfmt-trailing_comma: Never
3+
// rustfmt-version: Two
4+
5+
fn foo() {
6+
{
7+
let write_status = |status: &mut Vec<ansi_term::ANSIString>,
8+
diff: &Diff,
9+
heading: &str,
10+
color: &Style,
11+
show_hints: bool,
12+
hints: &[&str]|-> Option<bool> { Some(true) };}
13+
}
14+
15+
fn bar() {
16+
let write_status = |status: &mut Vec<ansi_term::ANSIString>,diff: &Diff,heading: &str,color: &Style,| -> Option<bool> { Some(true) };
17+
let baz = |foo: bool| -> Option<bool> { Some(true) };
18+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// rustfmt-indent_style: Block
2+
// rustfmt-version: Two
3+
4+
fn foo() {
5+
{
6+
let write_status = |
7+
status: &mut Vec<ansi_term::ANSIString>,
8+
diff: &Diff,
9+
heading: &str,
10+
color: &Style,
11+
show_hints: bool,
12+
hints: &[&str],
13+
| -> Option<bool> { Some(true) };
14+
}
15+
}
16+
17+
fn bar() {
18+
let write_status = |
19+
status: &mut Vec<ansi_term::ANSIString>,
20+
diff: &Diff,
21+
heading: &str,
22+
color: &Style,
23+
| -> Option<bool> { Some(true) };
24+
let baz = |foo: bool| -> Option<bool> { Some(true) };
25+
}

tests/target/closure_block_style.rs renamed to tests/target/closure_block_style_no_comma.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// rustfmt-indent_style: Block
2+
// rustfmt-trailing_comma: Never
23
// rustfmt-version: Two
34

45
fn foo() {

0 commit comments

Comments
 (0)