From c889cb627d46ae6cf43231f5311c3319b059e4e3 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Wed, 16 Oct 2019 14:15:34 -0500 Subject: [PATCH 1/5] feat: support closure block indent_style --- src/closures.rs | 39 +++++++++++++++++++++++++--- tests/source/closure_block_style.rs | 17 ++++++++++++ tests/source/closure_visual_style.rs | 17 ++++++++++++ tests/target/closure_block_style.rs | 25 ++++++++++++++++++ tests/target/closure_visual_style.rs | 23 ++++++++++++++++ 5 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 tests/source/closure_block_style.rs create mode 100644 tests/source/closure_visual_style.rs create mode 100644 tests/target/closure_block_style.rs create mode 100644 tests/target/closure_visual_style.rs diff --git a/src/closures.rs b/src/closures.rs index 69ffd888957..fa36c9c69d4 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -3,7 +3,7 @@ use syntax::{ast, ptr}; use crate::attr::get_attrs_from_stmt; use crate::config::lists::*; -use crate::config::Version; +use crate::config::{IndentStyle, Version}; use crate::expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond}; use crate::items::{span_hi_for_param, span_lo_for_param}; use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator}; @@ -238,9 +238,21 @@ fn rewrite_closure_fn_decl( .shrink_left(is_async.len() + mover.len() + immovable.len())? .sub_width(4)?; + let indent_style = context.config.indent_style(); + let version = context.config.version(); + // 1 = | let param_offset = nested_shape.indent + 1; - let param_shape = nested_shape.offset_left(1)?.visual_indent(0); + let param_shape = match indent_style { + IndentStyle::Block => { + if version == Version::Two { + Shape::indented(shape.indent.block_indent(context.config), context.config) + } else { + nested_shape.offset_left(1)?.visual_indent(0) + } + } + IndentStyle::Visual => nested_shape.offset_left(1)?.visual_indent(0), + }; let ret_str = fn_decl.output.rewrite(context, param_shape)?; let param_items = itemize_list( @@ -273,10 +285,29 @@ fn rewrite_closure_fn_decl( .tactic(tactic) .preserve_newline(true); let list_str = write_list(&item_vec, &fmt)?; - let mut prefix = format!("{}{}{}|{}|", is_async, immovable, mover, list_str); + let one_line_budget = context.budget(param_shape.indent.width()); + let (param_str, put_params_in_block) = if match indent_style { + IndentStyle::Block => list_str.contains('\n') || list_str.len() > one_line_budget, + _ => false, + } && !item_vec.is_empty() + && version == Version::Two + { + ( + format!( + "{}{}{}", + param_shape.indent.to_string_with_newline(context.config), + &list_str, + shape.indent.to_string_with_newline(context.config) + ), + true, + ) + } else { + (list_str, false) + }; + let mut prefix = format!("{}{}{}|{}|", is_async, immovable, mover, param_str); if !ret_str.is_empty() { - if prefix.contains('\n') { + if prefix.contains('\n') && !put_params_in_block { prefix.push('\n'); prefix.push_str(¶m_offset.to_string(context.config)); } else { diff --git a/tests/source/closure_block_style.rs b/tests/source/closure_block_style.rs new file mode 100644 index 00000000000..7a5e4f48153 --- /dev/null +++ b/tests/source/closure_block_style.rs @@ -0,0 +1,17 @@ +// rustfmt-indent_style: Block +// rustfmt-version: Two + +fn foo() { + { +let write_status = |status: &mut Vec, + diff: &Diff, + heading: &str, + color: &Style, + show_hints: bool, + hints: &[&str]|-> Option { Some(true) };} +} + +fn bar() { + let write_status = |status: &mut Vec,diff: &Diff,heading: &str,color: &Style,| -> Option { Some(true) }; + let baz = |foo: bool| -> Option { Some(true) }; +} diff --git a/tests/source/closure_visual_style.rs b/tests/source/closure_visual_style.rs new file mode 100644 index 00000000000..e8ae523fe77 --- /dev/null +++ b/tests/source/closure_visual_style.rs @@ -0,0 +1,17 @@ +// rustfmt-indent_style: Visual +// rustfmt-version: Two + +fn foo() { + { +let write_status = |status: &mut Vec, + diff: &Diff, + heading: &str, + color: &Style, + show_hints: bool, + hints: &[&str]|-> Option { Some(true) };} +} + +fn bar() { + let write_status = |status: &mut Vec,diff: &Diff,heading: &str,color: &Style,| -> Option { Some(true) }; + let baz = |foo: bool| -> Option { Some(true) }; +} diff --git a/tests/target/closure_block_style.rs b/tests/target/closure_block_style.rs new file mode 100644 index 00000000000..c9343ec98ab --- /dev/null +++ b/tests/target/closure_block_style.rs @@ -0,0 +1,25 @@ +// rustfmt-indent_style: Block +// rustfmt-version: Two + +fn foo() { + { + let write_status = | + status: &mut Vec, + diff: &Diff, + heading: &str, + color: &Style, + show_hints: bool, + hints: &[&str] + | -> Option { Some(true) }; + } +} + +fn bar() { + let write_status = | + status: &mut Vec, + diff: &Diff, + heading: &str, + color: &Style + | -> Option { Some(true) }; + let baz = |foo: bool| -> Option { Some(true) }; +} diff --git a/tests/target/closure_visual_style.rs b/tests/target/closure_visual_style.rs new file mode 100644 index 00000000000..61c268779e8 --- /dev/null +++ b/tests/target/closure_visual_style.rs @@ -0,0 +1,23 @@ +// rustfmt-indent_style: Visual +// rustfmt-version: Two + +fn foo() { + { + let write_status = |status: &mut Vec, + diff: &Diff, + heading: &str, + color: &Style, + show_hints: bool, + hints: &[&str]| + -> Option { Some(true) }; + } +} + +fn bar() { + let write_status = |status: &mut Vec, + diff: &Diff, + heading: &str, + color: &Style| + -> Option { Some(true) }; + let baz = |foo: bool| -> Option { Some(true) }; +} From 19e5a4b45d1d16ae85960d6f2f4a73b5b7b0edad Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Sat, 19 Oct 2019 23:37:00 -0500 Subject: [PATCH 2/5] refactor: include trailing comma on closure block --- src/closures.rs | 37 +++++++++++-------- ..._style.rs => closure_block_style_comma.rs} | 0 tests/source/closure_block_style_no_comma.rs | 18 +++++++++ tests/target/closure_block_style_comma.rs | 25 +++++++++++++ ...yle.rs => closure_block_style_no_comma.rs} | 1 + 5 files changed, 65 insertions(+), 16 deletions(-) rename tests/source/{closure_block_style.rs => closure_block_style_comma.rs} (100%) create mode 100644 tests/source/closure_block_style_no_comma.rs create mode 100644 tests/target/closure_block_style_comma.rs rename tests/target/{closure_block_style.rs => closure_block_style_no_comma.rs} (94%) diff --git a/src/closures.rs b/src/closures.rs index fa36c9c69d4..08b08564389 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -3,7 +3,7 @@ use syntax::{ast, ptr}; use crate::attr::get_attrs_from_stmt; use crate::config::lists::*; -use crate::config::{IndentStyle, Version}; +use crate::config::{IndentStyle, SeparatorTactic, Version}; use crate::expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond}; use crate::items::{span_hi_for_param, span_lo_for_param}; use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator}; @@ -286,24 +286,29 @@ fn rewrite_closure_fn_decl( .preserve_newline(true); let list_str = write_list(&item_vec, &fmt)?; let one_line_budget = context.budget(param_shape.indent.width()); - let (param_str, put_params_in_block) = if match indent_style { + let multi_line_params = match indent_style { IndentStyle::Block => list_str.contains('\n') || list_str.len() > one_line_budget, _ => false, - } && !item_vec.is_empty() - && version == Version::Two - { - ( - format!( - "{}{}{}", - param_shape.indent.to_string_with_newline(context.config), - &list_str, - shape.indent.to_string_with_newline(context.config) - ), - true, - ) - } else { - (list_str, false) }; + let (param_str, put_params_in_block) = + if multi_line_params && !item_vec.is_empty() && version == Version::Two { + let trailing_comma = match context.config.trailing_comma() { + SeparatorTactic::Never => "", + _ => ",", + }; + ( + format!( + "{}{}{}{}", + param_shape.indent.to_string_with_newline(context.config), + &list_str, + trailing_comma, + shape.indent.to_string_with_newline(context.config) + ), + true, + ) + } else { + (list_str, false) + }; let mut prefix = format!("{}{}{}|{}|", is_async, immovable, mover, param_str); if !ret_str.is_empty() { diff --git a/tests/source/closure_block_style.rs b/tests/source/closure_block_style_comma.rs similarity index 100% rename from tests/source/closure_block_style.rs rename to tests/source/closure_block_style_comma.rs diff --git a/tests/source/closure_block_style_no_comma.rs b/tests/source/closure_block_style_no_comma.rs new file mode 100644 index 00000000000..e428a480c35 --- /dev/null +++ b/tests/source/closure_block_style_no_comma.rs @@ -0,0 +1,18 @@ +// rustfmt-indent_style: Block +// rustfmt-trailing_comma: Never +// rustfmt-version: Two + +fn foo() { + { +let write_status = |status: &mut Vec, + diff: &Diff, + heading: &str, + color: &Style, + show_hints: bool, + hints: &[&str]|-> Option { Some(true) };} +} + +fn bar() { + let write_status = |status: &mut Vec,diff: &Diff,heading: &str,color: &Style,| -> Option { Some(true) }; + let baz = |foo: bool| -> Option { Some(true) }; +} diff --git a/tests/target/closure_block_style_comma.rs b/tests/target/closure_block_style_comma.rs new file mode 100644 index 00000000000..a8f1afaeba8 --- /dev/null +++ b/tests/target/closure_block_style_comma.rs @@ -0,0 +1,25 @@ +// rustfmt-indent_style: Block +// rustfmt-version: Two + +fn foo() { + { + let write_status = | + status: &mut Vec, + diff: &Diff, + heading: &str, + color: &Style, + show_hints: bool, + hints: &[&str], + | -> Option { Some(true) }; + } +} + +fn bar() { + let write_status = | + status: &mut Vec, + diff: &Diff, + heading: &str, + color: &Style, + | -> Option { Some(true) }; + let baz = |foo: bool| -> Option { Some(true) }; +} diff --git a/tests/target/closure_block_style.rs b/tests/target/closure_block_style_no_comma.rs similarity index 94% rename from tests/target/closure_block_style.rs rename to tests/target/closure_block_style_no_comma.rs index c9343ec98ab..e256576192c 100644 --- a/tests/target/closure_block_style.rs +++ b/tests/target/closure_block_style_no_comma.rs @@ -1,4 +1,5 @@ // rustfmt-indent_style: Block +// rustfmt-trailing_comma: Never // rustfmt-version: Two fn foo() { From ae6b0989cd24b97ee2d6c021c7b11c37519f8dc6 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Fri, 25 Oct 2019 15:02:45 -0500 Subject: [PATCH 3/5] refactor: remove version gate on closure style --- src/closures.rs | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/src/closures.rs b/src/closures.rs index 08b08564389..0b9985e855e 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -239,17 +239,12 @@ fn rewrite_closure_fn_decl( .sub_width(4)?; let indent_style = context.config.indent_style(); - let version = context.config.version(); // 1 = | let param_offset = nested_shape.indent + 1; let param_shape = match indent_style { IndentStyle::Block => { - if version == Version::Two { - Shape::indented(shape.indent.block_indent(context.config), context.config) - } else { - nested_shape.offset_left(1)?.visual_indent(0) - } + Shape::indented(shape.indent.block_indent(context.config), context.config) } IndentStyle::Visual => nested_shape.offset_left(1)?.visual_indent(0), }; @@ -290,25 +285,24 @@ fn rewrite_closure_fn_decl( IndentStyle::Block => list_str.contains('\n') || list_str.len() > one_line_budget, _ => false, }; - let (param_str, put_params_in_block) = - if multi_line_params && !item_vec.is_empty() && version == Version::Two { - let trailing_comma = match context.config.trailing_comma() { - SeparatorTactic::Never => "", - _ => ",", - }; - ( - format!( - "{}{}{}{}", - param_shape.indent.to_string_with_newline(context.config), - &list_str, - trailing_comma, - shape.indent.to_string_with_newline(context.config) - ), - true, - ) - } else { - (list_str, false) + let (param_str, put_params_in_block) = if multi_line_params && !item_vec.is_empty() { + let trailing_comma = match context.config.trailing_comma() { + SeparatorTactic::Never => "", + _ => ",", }; + ( + format!( + "{}{}{}{}", + param_shape.indent.to_string_with_newline(context.config), + &list_str, + trailing_comma, + shape.indent.to_string_with_newline(context.config) + ), + true, + ) + } else { + (list_str, false) + }; let mut prefix = format!("{}{}{}|{}|", is_async, immovable, mover, param_str); if !ret_str.is_empty() { From 082ba0523252e8988b3e68db59aecd7eae4e6135 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Fri, 25 Oct 2019 15:03:54 -0500 Subject: [PATCH 4/5] tests: refactor closure tests to account for new indent style --- .../{closure.rs => closure_block_style.rs} | 13 ++++ tests/source/closure_block_style_comma.rs | 17 ----- tests/source/closure_block_style_no_comma.rs | 1 - tests/source/closure_visual_style.rs | 55 ++++++++++++++- .../{closure.rs => closure_block_style.rs} | 43 +++++++++--- tests/target/closure_block_style_comma.rs | 25 ------- tests/target/closure_block_style_no_comma.rs | 1 - tests/target/closure_visual_style.rs | 69 ++++++++++++++++++- tests/target/fn-param-attributes.rs | 12 ++-- tests/target/{mulit-file.rs => multi-file.rs} | 4 +- 10 files changed, 177 insertions(+), 63 deletions(-) rename tests/source/{closure.rs => closure_block_style.rs} (91%) delete mode 100644 tests/source/closure_block_style_comma.rs rename tests/target/{closure.rs => closure_block_style.rs} (86%) delete mode 100644 tests/target/closure_block_style_comma.rs rename tests/target/{mulit-file.rs => multi-file.rs} (68%) diff --git a/tests/source/closure.rs b/tests/source/closure_block_style.rs similarity index 91% rename from tests/source/closure.rs rename to tests/source/closure_block_style.rs index e93cc3fb40f..90cb606afe6 100644 --- a/tests/source/closure.rs +++ b/tests/source/closure_block_style.rs @@ -1,3 +1,4 @@ +// rustfmt-indent_style: Block // rustfmt-normalize_comments: true // Closures @@ -211,3 +212,15 @@ fn issue2262() { slave: None, })?; } + +fn issue_3865() { + { +let write_status = |status: &mut Vec, + diff: &Diff, + heading: &str, + color: &Style, + show_hints: bool, + hints: &[&str]|-> Option { Some(true) };} + let baz = |foo: bool| -> Option { Some(true) }; + let write_status = |status: &mut Vec,diff: &Diff,heading: &str,color: &Style,| -> Option { Some(true) }; +} diff --git a/tests/source/closure_block_style_comma.rs b/tests/source/closure_block_style_comma.rs deleted file mode 100644 index 7a5e4f48153..00000000000 --- a/tests/source/closure_block_style_comma.rs +++ /dev/null @@ -1,17 +0,0 @@ -// rustfmt-indent_style: Block -// rustfmt-version: Two - -fn foo() { - { -let write_status = |status: &mut Vec, - diff: &Diff, - heading: &str, - color: &Style, - show_hints: bool, - hints: &[&str]|-> Option { Some(true) };} -} - -fn bar() { - let write_status = |status: &mut Vec,diff: &Diff,heading: &str,color: &Style,| -> Option { Some(true) }; - let baz = |foo: bool| -> Option { Some(true) }; -} diff --git a/tests/source/closure_block_style_no_comma.rs b/tests/source/closure_block_style_no_comma.rs index e428a480c35..a806f3efc8a 100644 --- a/tests/source/closure_block_style_no_comma.rs +++ b/tests/source/closure_block_style_no_comma.rs @@ -1,6 +1,5 @@ // rustfmt-indent_style: Block // rustfmt-trailing_comma: Never -// rustfmt-version: Two fn foo() { { diff --git a/tests/source/closure_visual_style.rs b/tests/source/closure_visual_style.rs index e8ae523fe77..4fdefc0d7b2 100644 --- a/tests/source/closure_visual_style.rs +++ b/tests/source/closure_visual_style.rs @@ -1,5 +1,5 @@ // rustfmt-indent_style: Visual -// rustfmt-version: Two +// rustfmt-normalize_comments: true fn foo() { { @@ -15,3 +15,56 @@ fn bar() { let write_status = |status: &mut Vec,diff: &Diff,heading: &str,color: &Style,| -> Option { Some(true) }; let baz = |foo: bool| -> Option { Some(true) }; } + +fn main() { + let square = ( |i: i32 | i * i ); + + let commented = |// first + a, // argument + // second + b: WithType, // argument + // ignored + _| { + (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) + }; + + let commented2 = |/* first */ a /*argument*/, /* second*/ b: WithType /* argument*/, /* ignored */ _ | + (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb); + + let block_body = move |xxxxxxxxxxxxxxxxxxxxxxxxxxxxx, ref yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy| { + xxxxxxxxxxxxxxxxxxxxxxxxxxxxx + yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy + }; + + let loooooooooooooong_name = |field| { + // format comments. + if field.node.attrs.len() > 0 { field.node.attrs[0].span.lo() + } else { + field.span.lo() + }}; + + let unblock_me = |trivial| { + closure() + }; + + let empty = |arg| {}; + + let simple = |arg| { /* comment formatting */ foo(arg) }; + + let test = | | { do_something(); do_something_else(); }; + + let arg_test = |big_argument_name, test123| looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame(); + + let arg_test = |big_argument_name, test123| {looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame()}; + + let simple_closure = move || -> () {}; + + let closure = |input: Ty| -> Option { + foo() + }; + + let closure_with_return_type = |aaaaaaaaaaaaaaaaaaaaaaarg1, aaaaaaaaaaaaaaaaaaaaaaarg2| -> Strong { "sup".to_owned() }; + + |arg1, arg2, _, _, arg3, arg4| { let temp = arg4 + arg3; + arg2 * arg1 - temp }; + +} diff --git a/tests/target/closure.rs b/tests/target/closure_block_style.rs similarity index 86% rename from tests/target/closure.rs rename to tests/target/closure_block_style.rs index f3107d19c2f..077b8017a4b 100644 --- a/tests/target/closure.rs +++ b/tests/target/closure_block_style.rs @@ -1,25 +1,28 @@ +// rustfmt-indent_style: Block // rustfmt-normalize_comments: true // Closures fn main() { let square = (|i: i32| i * i); - let commented = |// first - a, // argument - // second - b: WithType, // argument - // ignored - _| { + let commented = | + // first + a, // argument + // second + b: WithType, // argument + // ignored + _, + | { ( aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb, ) }; - let block_body = move |xxxxxxxxxxxxxxxxxxxxxxxxxxxxx, - ref yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy| { - xxxxxxxxxxxxxxxxxxxxxxxxxxxxx + yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy - }; + let block_body = move | + xxxxxxxxxxxxxxxxxxxxxxxxxxxxx, + ref yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy, + | { xxxxxxxxxxxxxxxxxxxxxxxxxxxxx + yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy }; let loooooooooooooong_name = |field| { // format comments. @@ -248,3 +251,23 @@ fn issue2262() { slave: None, })?; } + +fn issue_3865() { + { + let write_status = | + status: &mut Vec, + diff: &Diff, + heading: &str, + color: &Style, + show_hints: bool, + hints: &[&str], + | -> Option { Some(true) }; + } + let baz = |foo: bool| -> Option { Some(true) }; + let write_status = | + status: &mut Vec, + diff: &Diff, + heading: &str, + color: &Style, + | -> Option { Some(true) }; +} diff --git a/tests/target/closure_block_style_comma.rs b/tests/target/closure_block_style_comma.rs deleted file mode 100644 index a8f1afaeba8..00000000000 --- a/tests/target/closure_block_style_comma.rs +++ /dev/null @@ -1,25 +0,0 @@ -// rustfmt-indent_style: Block -// rustfmt-version: Two - -fn foo() { - { - let write_status = | - status: &mut Vec, - diff: &Diff, - heading: &str, - color: &Style, - show_hints: bool, - hints: &[&str], - | -> Option { Some(true) }; - } -} - -fn bar() { - let write_status = | - status: &mut Vec, - diff: &Diff, - heading: &str, - color: &Style, - | -> Option { Some(true) }; - let baz = |foo: bool| -> Option { Some(true) }; -} diff --git a/tests/target/closure_block_style_no_comma.rs b/tests/target/closure_block_style_no_comma.rs index e256576192c..e320e4e8487 100644 --- a/tests/target/closure_block_style_no_comma.rs +++ b/tests/target/closure_block_style_no_comma.rs @@ -1,6 +1,5 @@ // rustfmt-indent_style: Block // rustfmt-trailing_comma: Never -// rustfmt-version: Two fn foo() { { diff --git a/tests/target/closure_visual_style.rs b/tests/target/closure_visual_style.rs index 61c268779e8..a4593b12cfe 100644 --- a/tests/target/closure_visual_style.rs +++ b/tests/target/closure_visual_style.rs @@ -1,5 +1,5 @@ // rustfmt-indent_style: Visual -// rustfmt-version: Two +// rustfmt-normalize_comments: true fn foo() { { @@ -21,3 +21,70 @@ fn bar() { -> Option { Some(true) }; let baz = |foo: bool| -> Option { Some(true) }; } + +fn main() { + let square = (|i: i32| i * i); + + let commented = |// first + a, // argument + // second + b: WithType, // argument + // ignored + _| { + (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) + }; + + let commented2 = + |// first + a, // argument + // second + b: WithType, // argument + // ignored + _| (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb); + + let block_body = move |xxxxxxxxxxxxxxxxxxxxxxxxxxxxx, + ref yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy| { + xxxxxxxxxxxxxxxxxxxxxxxxxxxxx + yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy + }; + + let loooooooooooooong_name = |field| { + // format comments. + if field.node.attrs.len() > 0 { + field.node.attrs[0].span.lo() + } else { + field.span.lo() + } + }; + + let unblock_me = |trivial| closure(); + + let empty = |arg| {}; + + let simple = |arg| { + // comment formatting + foo(arg) + }; + + let test = || { + do_something(); + do_something_else(); + }; + + let arg_test = + |big_argument_name, test123| looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame(); + + let arg_test = + |big_argument_name, test123| looooooooooooooooooong_function_naaaaaaaaaaaaaaaaame(); + + let simple_closure = move || -> () {}; + + let closure = |input: Ty| -> Option { foo() }; + + let closure_with_return_type = + |aaaaaaaaaaaaaaaaaaaaaaarg1, aaaaaaaaaaaaaaaaaaaaaaarg2| -> Strong { "sup".to_owned() }; + + |arg1, arg2, _, _, arg3, arg4| { + let temp = arg4 + arg3; + arg2 * arg1 - temp + }; +} diff --git a/tests/target/fn-param-attributes.rs b/tests/target/fn-param-attributes.rs index 829575518de..f4afe84c797 100644 --- a/tests/target/fn-param-attributes.rs +++ b/tests/target/fn-param-attributes.rs @@ -28,11 +28,13 @@ impl MutStruct { } fn main() { - let c = |#[allow(C)] a: u32, - #[cfg(something)] b: i32, - #[cfg_attr(something, cfg(nothing))] - #[deny(C)] - c: i32| {}; + let c = | + #[allow(C)] a: u32, + #[cfg(something)] b: i32, + #[cfg_attr(something, cfg(nothing))] + #[deny(C)] + c: i32, + | {}; let _ = c(1, 2); } diff --git a/tests/target/mulit-file.rs b/tests/target/multi-file.rs similarity index 68% rename from tests/target/mulit-file.rs rename to tests/target/multi-file.rs index 1f829b36f3f..c24b07397dd 100644 --- a/tests/target/mulit-file.rs +++ b/tests/target/multi-file.rs @@ -2,9 +2,9 @@ // crash. #[cfg(all(foo))] -#[path = "closure.rs"] +#[path = "closure_block_style.rs"] pub mod imp; #[cfg(all(bar))] -#[path = "closure.rs"] +#[path = "closure_block_style.rs"] pub mod imp; From 22fe6a58651256cf98e29f515b89256db261eb13 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Sat, 26 Oct 2019 14:17:59 -0500 Subject: [PATCH 5/5] refactor: stylistic improvements --- src/closures.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/closures.rs b/src/closures.rs index 0b9985e855e..d3cc5a8ffb3 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -285,23 +285,21 @@ fn rewrite_closure_fn_decl( IndentStyle::Block => list_str.contains('\n') || list_str.len() > one_line_budget, _ => false, }; - let (param_str, put_params_in_block) = if multi_line_params && !item_vec.is_empty() { + let put_params_in_block = multi_line_params && !item_vec.is_empty(); + let param_str = if put_params_in_block { let trailing_comma = match context.config.trailing_comma() { SeparatorTactic::Never => "", _ => ",", }; - ( - format!( - "{}{}{}{}", - param_shape.indent.to_string_with_newline(context.config), - &list_str, - trailing_comma, - shape.indent.to_string_with_newline(context.config) - ), - true, + format!( + "{}{}{}{}", + param_shape.indent.to_string_with_newline(context.config), + &list_str, + trailing_comma, + shape.indent.to_string_with_newline(context.config) ) } else { - (list_str, false) + list_str }; let mut prefix = format!("{}{}{}|{}|", is_async, immovable, mover, param_str);