diff --git a/Configurations.md b/Configurations.md index e066553dc63..b9ec113eded 100644 --- a/Configurations.md +++ b/Configurations.md @@ -756,6 +756,97 @@ trait Lorem { See also [`fn_params_layout`](#fn_params_layout) +## `fn_call_layout` + +Control the layout of arguments in function calls + +- **Default value**: `"Foo"` +- **Possible values**: `"Foo"` `"Compressed"`, `"Tall"`, `"Vertical"` +- **Stable**: No (tracking issue: N/A) + +#### `"Foo"` (default): + +```rust +fn main() { + lorem(ipsum, dolor, sit, amet); + ipsum( + dolor, + sit, + amet, + consectetur, + adipiscing, + elit, + vivamus, + ipsum, + orci, + rhoncus, + vel, + imperdiet, + ); +} +``` + +#### `"Tall"`: + +```rust +fn main() { + lorem(ipsum, dolor, sit, amet); + ipsum( + dolor, + sit, + amet, + consectetur, + adipiscing, + elit, + vivamus, + ipsum, + orci, + rhoncus, + vel, + imperdiet, + ); +} +``` + +#### `"Compressed"`: + +```rust +fn main() { + lorem(ipsum, dolor, sit, amet); + ipsum( + dolor, sit, amet, consectetur, adipiscing, elit, vivamus, ipsum, orci, rhoncus, vel, + imperdiet, + ); +} +``` + +#### `"Vertical"`: + +```rust +fn main() { + lorem( + ipsum, + dolor, + sit, + amet, + ); + ipsum( + dolor, + sit, + amet, + consectetur, + adipiscing, + elit, + vivamus, + ipsum, + orci, + rhoncus, + vel, + imperdiet, + ); +} +``` + ## `fn_call_width` Maximum width of the args of a function call before falling back to vertical formatting. diff --git a/src/attr.rs b/src/attr.rs index 41ba9a847e6..4d20acfcb18 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -309,6 +309,7 @@ impl Rewrite for ast::MetaItem { } else { SeparatorTactic::Never }), + None, )? } ast::MetaItemKind::NameValue(ref literal) => { diff --git a/src/chains.rs b/src/chains.rs index 5bccb22db4c..7ba8e097f0a 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -253,7 +253,7 @@ impl ChainItem { format!("::<{}>", type_list.join(", ")) }; let callee_str = format!(".{}{}", rewrite_ident(context, method_name), type_str); - rewrite_call(context, &callee_str, &args[1..], span, shape) + rewrite_call(context, &callee_str, &args[1..], None, span, shape) } } diff --git a/src/config/mod.rs b/src/config/mod.rs index 5492519f389..fceb0f2b0c6 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -127,6 +127,8 @@ create_config! { "(deprecated: use fn_params_layout instead)"; fn_params_layout: Density, Density::Tall, true, "Control the layout of parameters in function signatures."; + fn_call_layout: OverflowDensity, OverflowDensity::Foo, false, + "Control the layout of arguments in a function call"; brace_style: BraceStyle, BraceStyle::SameLineWhere, false, "Brace style for items"; control_brace_style: ControlBraceStyle, ControlBraceStyle::AlwaysSameLine, false, "Brace style for control flow constructs"; @@ -654,6 +656,7 @@ match_arm_blocks = true match_arm_leading_pipes = "Never" force_multiline_blocks = false fn_params_layout = "Tall" +fn_call_layout = "Foo" brace_style = "SameLineWhere" control_brace_style = "AlwaysSameLine" trailing_semicolon = true diff --git a/src/config/options.rs b/src/config/options.rs index 257a17b2703..33967010252 100644 --- a/src/config/options.rs +++ b/src/config/options.rs @@ -69,6 +69,22 @@ pub enum Density { Vertical, } +#[config_type] +/// allows users to specify a density for list-like items. +/// Currently only supports function arguments. +pub enum OverflowDensity { + /// To prevent breaking formatting changes, this option follows the default behavior for + /// list-like items that can overflow. + Foo, + /// Fit as much on one line as possible before wrapping to the next line. + Compressed, + /// Items are placed horizontally as long as there is sufficient space and there aren't + /// any line comments that would force a Vertical layout. + Tall, + /// Place every item on a separate line. There must be at least two items in the list. + Vertical, +} + #[config_type] /// Spacing around type combinators. pub enum TypeDensity { diff --git a/src/expr.rs b/src/expr.rs index 13d068d0c2d..7972476f37a 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -12,7 +12,7 @@ use crate::comment::{ combine_strs_with_missing_comments, contains_comment, recover_comment_removed, rewrite_comment, rewrite_missing_comment, CharClasses, FindUncommented, }; -use crate::config::lists::*; +use crate::config::{lists::*, OverflowDensity}; use crate::config::{Config, ControlBraceStyle, HexLiteralCase, IndentStyle, Version}; use crate::lists::{ definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting, struct_lit_shape, @@ -89,7 +89,15 @@ pub(crate) fn format_expr( ast::ExprKind::Call(ref callee, ref args) => { let inner_span = mk_sp(callee.span.hi(), expr.span.hi()); let callee_str = callee.rewrite(context, shape)?; - rewrite_call(context, &callee_str, args, inner_span, shape) + let force_list_tactic = Some(context.config.fn_call_layout()); + rewrite_call( + context, + &callee_str, + args, + force_list_tactic, + inner_span, + shape, + ) } ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, shape, expr.span), ast::ExprKind::Binary(op, ref lhs, ref rhs) => { @@ -1264,6 +1272,7 @@ pub(crate) fn rewrite_call( context: &RewriteContext<'_>, callee: &str, args: &[ptr::P], + force_list_tactic: Option, span: Span, shape: Shape, ) -> Option { @@ -1275,6 +1284,7 @@ pub(crate) fn rewrite_call( span, context.config.fn_call_width(), choose_separator_tactic(context, span), + force_list_tactic, ) } @@ -1812,6 +1822,7 @@ pub(crate) fn rewrite_tuple<'a, T: 'a + IntoOverflowableItem<'a>>( span, context.config.fn_call_width(), force_tactic, + None, ) } else { rewrite_tuple_in_visual_indent_style(context, items, span, shape, is_singleton_tuple) diff --git a/src/items.rs b/src/items.rs index a76e88b77d4..921adf3f5e2 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1488,6 +1488,7 @@ fn format_tuple_struct( mk_sp(lo, span.hi()), context.config.fn_call_width(), None, + None, )?; } diff --git a/src/macros.rs b/src/macros.rs index e78ef1f8066..81377663fb8 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -279,6 +279,7 @@ fn rewrite_macro_inner( } else { Some(SeparatorTactic::Never) }, + None, ) .map(|rw| match position { MacroPosition::Item => format!("{};", rw), diff --git a/src/overflow.rs b/src/overflow.rs index 6bf8cd0c70b..3f383af02aa 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -8,8 +8,8 @@ use rustc_ast::{ast, ptr}; use rustc_span::Span; use crate::closures; -use crate::config::lists::*; use crate::config::Version; +use crate::config::{lists::*, OverflowDensity}; use crate::expr::{ can_be_overflowed_expr, is_every_expr_simple, is_method_call, is_nested_call, is_simple_expr, rewrite_cond, @@ -252,6 +252,7 @@ pub(crate) fn rewrite_with_parens<'a, T: 'a + IntoOverflowableItem<'a>>( span: Span, item_max_width: usize, force_separator_tactic: Option, + force_list_tactic: Option, ) -> Option { Context::new( context, @@ -263,6 +264,7 @@ pub(crate) fn rewrite_with_parens<'a, T: 'a + IntoOverflowableItem<'a>>( ")", item_max_width, force_separator_tactic, + force_list_tactic, None, ) .rewrite(shape) @@ -286,6 +288,7 @@ pub(crate) fn rewrite_with_angle_brackets<'a, T: 'a + IntoOverflowableItem<'a>>( context.config.max_width(), None, None, + None, ) .rewrite(shape) } @@ -314,6 +317,7 @@ pub(crate) fn rewrite_with_square_brackets<'a, T: 'a + IntoOverflowableItem<'a>> rhs, context.config.array_width(), force_separator_tactic, + None, Some(("[", "]")), ) .rewrite(shape) @@ -331,6 +335,7 @@ struct Context<'a> { item_max_width: usize, one_line_width: usize, force_separator_tactic: Option, + force_list_tactic: Option, custom_delims: Option<(&'a str, &'a str)>, } @@ -345,6 +350,7 @@ impl<'a> Context<'a> { suffix: &'static str, item_max_width: usize, force_separator_tactic: Option, + force_list_tactic: Option, custom_delims: Option<(&'a str, &'a str)>, ) -> Context<'a> { let used_width = extra_offset(ident, shape); @@ -369,6 +375,7 @@ impl<'a> Context<'a> { item_max_width, one_line_width, force_separator_tactic, + force_list_tactic, custom_delims, } } @@ -584,6 +591,36 @@ impl<'a> Context<'a> { _ => (), } + // we only care if the any element but the last has a sigle line comment + let any_but_last_contains_line_comment = list_items + .iter() + .rev() + .skip(1) + .any(|item| item.has_single_line_comment()); + + match self.force_list_tactic { + Some(OverflowDensity::Tall) + if tactic == DefinitiveListTactic::Mixed && any_but_last_contains_line_comment => + { + // If we determined a `Mixed` layout, but we configured tall then force + // the tactic to be vertical only if any of the items contain single line comments. + // Otherwise, the tacitc was properly set above. + tactic = DefinitiveListTactic::Vertical + } + Some(OverflowDensity::Compressed) if tactic != DefinitiveListTactic::Horizontal => { + // Only force a mixed layout if we haven't already decided on going horizontal + tactic = DefinitiveListTactic::Mixed + } + // If we need to force a `Vertical` layout, we should only do so if there are + // at least 2 items for us to format. Otherwise, use the tactic already determined. + Some(OverflowDensity::Vertical) if self.items.len() > 1 => { + tactic = DefinitiveListTactic::Vertical; + } + // Default behavior for calls to match rustfmts pre `fn_call_layout` formatting. + Some(OverflowDensity::Foo) => {} + _ => {} + }; + tactic } diff --git a/src/patterns.rs b/src/patterns.rs index 9b74b35f314..f323646c336 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -494,6 +494,7 @@ fn rewrite_tuple_pat( } else { None }, + None, ) } diff --git a/src/types.rs b/src/types.rs index 64a201e45dd..0f980323373 100644 --- a/src/types.rs +++ b/src/types.rs @@ -844,6 +844,7 @@ impl Rewrite for ast::Ty { context, "typeof", &[anon_const.value.clone()], + None, self.span, shape, ), diff --git a/tests/source/configs/fn_call_layout/compressed.rs b/tests/source/configs/fn_call_layout/compressed.rs new file mode 100644 index 00000000000..f461cf0cb3d --- /dev/null +++ b/tests/source/configs/fn_call_layout/compressed.rs @@ -0,0 +1,73 @@ +// rustfmt-fn_call_layout:Compressed + +fn main() { + empty_args(); + single_arg(ipsum); + two_args(ipsum, dolor); + + lorem(ipsum, dolor, sit, amet); + lorem(ipsum, // some inine comment + dolor, sit, amet); + lorem(ipsum, /* some inine comment */ + dolor, sit, amet); + ipsum(dolor, sit, amet, consectetur, adipiscing, elit, vivamus, ipsum, orci, rhoncus, vel, imperdiet); + + // issue 2010 + let a = i8x32::new( + 0, 1, -1, 2, + -2, 3, -3, 4, + -4, 5, -5, std::i8::MAX, + std::i8::MIN + 1, 100, -100, -32, + 0, 1, -1, 2, + -2, 3, -3, 4, + -4, 5, -5, std::i8::MAX, + std::i8::MIN + 1, 100, -100, -32); + + // issue 4146 + return_monitor_err( + e, + channel_state, + chan, + order, + commitment_update.is_some(), + revoke_and_ack.is_some(), + ); + + // line comment in the middle of the args + CastCheck::new( + &fn_ctxt, e, from_ty, to_ty, + // We won't show any error to the user, so we don't care what the span is here. + DUMMY_SP, DUMMY_SP, + ); + + // other examples with more complex args + more_complex_args( + |a, b, c| {if a == 998765390 {- b * 3 } else {c} }, + std::ops::Range { start: 3, end: 5 }, + std::i8::MAX, String::from(" hello world!!").as_bytes(), + thread::Builder::new() + .name("thread1".to_string()) + .spawn(move || { + use std::sync::Arc; + + let mut values = Arc::<[u32]>::new_uninit_slice(3); + + // Deferred initialization: + let data = Arc::get_mut(&mut values).unwrap(); + data[0].write(1); + data[1].write(2); + data[2].write(3); + + let values = unsafe { values.assume_init() }; + }), "another argument" + ); + + // nested calls + ipsum(dolor(sit::amet(consectetur(aaaaaaaaaaaaaa, bbbbbbbbbbbb, ccccccccccccc, ddddddddddddd, eeeeeeeee)))); + + ipsum(dolor(sit::amet(consectetur(aaaaaaaaaaaaaa, bbbbbbbbbbbb, ccccccccccccc, ddddddddddddd, adipiscing(elit(|| ipsum(dolor(sit::amet(consectetur()))))))))); + + aaaaaaaaaaaaaaaaaa::bbbbbbbbbbbbbb::cccccccccc(ipsum(), dolor(sit::amet(consectetur, adipiscing), elit(vivamus::ipsum::orci(rhoncus())))); + + ipsum(dolor(sit::amet(consectetur(adipiscing(elit(vivamus::ipsum::orci(rhoncus()))))))); +} diff --git a/tests/source/configs/fn_call_layout/foo.rs b/tests/source/configs/fn_call_layout/foo.rs new file mode 100644 index 00000000000..5ed6bc62b5a --- /dev/null +++ b/tests/source/configs/fn_call_layout/foo.rs @@ -0,0 +1,73 @@ +// rustfmt-fn_call_layout: Foo + +fn main() { + empty_args(); + single_arg(ipsum); + two_args(ipsum, dolor); + + lorem(ipsum, dolor, sit, amet); + lorem(ipsum, // some inine comment + dolor, sit, amet); + lorem(ipsum, /* some inine comment */ + dolor, sit, amet); + ipsum(dolor, sit, amet, consectetur, adipiscing, elit, vivamus, ipsum, orci, rhoncus, vel, imperdiet); + + // issue 2010 + let a = i8x32::new( + 0, 1, -1, 2, + -2, 3, -3, 4, + -4, 5, -5, std::i8::MAX, + std::i8::MIN + 1, 100, -100, -32, + 0, 1, -1, 2, + -2, 3, -3, 4, + -4, 5, -5, std::i8::MAX, + std::i8::MIN + 1, 100, -100, -32); + + // issue 4146 + return_monitor_err( + e, + channel_state, + chan, + order, + commitment_update.is_some(), + revoke_and_ack.is_some(), + ); + + // line comment in the middle of the args + CastCheck::new( + &fn_ctxt, e, from_ty, to_ty, + // We won't show any error to the user, so we don't care what the span is here. + DUMMY_SP, DUMMY_SP, + ); + + // other examples with more complex args + more_complex_args( + |a, b, c| {if a == 998765390 {- b * 3 } else {c} }, + std::ops::Range { start: 3, end: 5 }, + std::i8::MAX, String::from(" hello world!!").as_bytes(), + thread::Builder::new() + .name("thread1".to_string()) + .spawn(move || { + use std::sync::Arc; + + let mut values = Arc::<[u32]>::new_uninit_slice(3); + + // Deferred initialization: + let data = Arc::get_mut(&mut values).unwrap(); + data[0].write(1); + data[1].write(2); + data[2].write(3); + + let values = unsafe { values.assume_init() }; + }), "another argument" + ); + + // nested calls + ipsum(dolor(sit::amet(consectetur(aaaaaaaaaaaaaa, bbbbbbbbbbbb, ccccccccccccc, ddddddddddddd, eeeeeeeee)))); + + ipsum(dolor(sit::amet(consectetur(aaaaaaaaaaaaaa, bbbbbbbbbbbb, ccccccccccccc, ddddddddddddd, adipiscing(elit(|| ipsum(dolor(sit::amet(consectetur()))))))))); + + aaaaaaaaaaaaaaaaaa::bbbbbbbbbbbbbb::cccccccccc(ipsum(), dolor(sit::amet(consectetur, adipiscing), elit(vivamus::ipsum::orci(rhoncus())))); + + ipsum(dolor(sit::amet(consectetur(adipiscing(elit(vivamus::ipsum::orci(rhoncus()))))))); +} diff --git a/tests/source/configs/fn_call_layout/tall.rs b/tests/source/configs/fn_call_layout/tall.rs new file mode 100644 index 00000000000..a0691d8c4de --- /dev/null +++ b/tests/source/configs/fn_call_layout/tall.rs @@ -0,0 +1,73 @@ +// rustfmt-fn_call_layout:Tall + +fn main() { + empty_args(); + single_arg(ipsum); + two_args(ipsum, dolor); + + lorem(ipsum, dolor, sit, amet); + lorem(ipsum, // some inine comment + dolor, sit, amet); + lorem(ipsum, /* some inine comment */ + dolor, sit, amet); + ipsum(dolor, sit, amet, consectetur, adipiscing, elit, vivamus, ipsum, orci, rhoncus, vel, imperdiet); + + // issue 2010 + let a = i8x32::new( + 0, 1, -1, 2, + -2, 3, -3, 4, + -4, 5, -5, std::i8::MAX, + std::i8::MIN + 1, 100, -100, -32, + 0, 1, -1, 2, + -2, 3, -3, 4, + -4, 5, -5, std::i8::MAX, + std::i8::MIN + 1, 100, -100, -32); + + // issue 4146 + return_monitor_err( + e, + channel_state, + chan, + order, + commitment_update.is_some(), + revoke_and_ack.is_some(), + ); + + // line comment in the middle of the args + CastCheck::new( + &fn_ctxt, e, from_ty, to_ty, + // We won't show any error to the user, so we don't care what the span is here. + DUMMY_SP, DUMMY_SP, + ); + + // other examples with more complex args + more_complex_args( + |a, b, c| {if a == 998765390 {- b * 3 } else {c} }, + std::ops::Range { start: 3, end: 5 }, + std::i8::MAX, String::from(" hello world!!").as_bytes(), + thread::Builder::new() + .name("thread1".to_string()) + .spawn(move || { + use std::sync::Arc; + + let mut values = Arc::<[u32]>::new_uninit_slice(3); + + // Deferred initialization: + let data = Arc::get_mut(&mut values).unwrap(); + data[0].write(1); + data[1].write(2); + data[2].write(3); + + let values = unsafe { values.assume_init() }; + }), "another argument" + ); + + // nested calls + ipsum(dolor(sit::amet(consectetur(aaaaaaaaaaaaaa, bbbbbbbbbbbb, ccccccccccccc, ddddddddddddd, eeeeeeeee)))); + + ipsum(dolor(sit::amet(consectetur(aaaaaaaaaaaaaa, bbbbbbbbbbbb, ccccccccccccc, ddddddddddddd, adipiscing(elit(|| ipsum(dolor(sit::amet(consectetur()))))))))); + + aaaaaaaaaaaaaaaaaa::bbbbbbbbbbbbbb::cccccccccc(ipsum(), dolor(sit::amet(consectetur, adipiscing), elit(vivamus::ipsum::orci(rhoncus())))); + + ipsum(dolor(sit::amet(consectetur(adipiscing(elit(vivamus::ipsum::orci(rhoncus()))))))); +} diff --git a/tests/source/configs/fn_call_layout/vertical.rs b/tests/source/configs/fn_call_layout/vertical.rs new file mode 100644 index 00000000000..5026a5d9a10 --- /dev/null +++ b/tests/source/configs/fn_call_layout/vertical.rs @@ -0,0 +1,73 @@ +// rustfmt-fn_call_layout:Vertical + +fn main() { + empty_args(); + single_arg(ipsum); + two_args(ipsum, dolor); + + lorem(ipsum, dolor, sit, amet); + lorem(ipsum, // some inine comment + dolor, sit, amet); + lorem(ipsum, /* some inine comment */ + dolor, sit, amet); + ipsum(dolor, sit, amet, consectetur, adipiscing, elit, vivamus, ipsum, orci, rhoncus, vel, imperdiet); + + // issue 2010 + let a = i8x32::new( + 0, 1, -1, 2, + -2, 3, -3, 4, + -4, 5, -5, std::i8::MAX, + std::i8::MIN + 1, 100, -100, -32, + 0, 1, -1, 2, + -2, 3, -3, 4, + -4, 5, -5, std::i8::MAX, + std::i8::MIN + 1, 100, -100, -32); + + // issue 4146 + return_monitor_err( + e, + channel_state, + chan, + order, + commitment_update.is_some(), + revoke_and_ack.is_some(), + ); + + // line comment in the middle of the args + CastCheck::new( + &fn_ctxt, e, from_ty, to_ty, + // We won't show any error to the user, so we don't care what the span is here. + DUMMY_SP, DUMMY_SP, + ); + + // other examples with more complex args + more_complex_args( + |a, b, c| {if a == 998765390 {- b * 3 } else {c} }, + std::ops::Range { start: 3, end: 5 }, + std::i8::MAX, String::from(" hello world!!").as_bytes(), + thread::Builder::new() + .name("thread1".to_string()) + .spawn(move || { + use std::sync::Arc; + + let mut values = Arc::<[u32]>::new_uninit_slice(3); + + // Deferred initialization: + let data = Arc::get_mut(&mut values).unwrap(); + data[0].write(1); + data[1].write(2); + data[2].write(3); + + let values = unsafe { values.assume_init() }; + }), "another argument" + ); + + // nested calls + ipsum(dolor(sit::amet(consectetur(aaaaaaaaaaaaaa, bbbbbbbbbbbb, ccccccccccccc, ddddddddddddd, eeeeeeeee)))); + + ipsum(dolor(sit::amet(consectetur(aaaaaaaaaaaaaa, bbbbbbbbbbbb, ccccccccccccc, ddddddddddddd, adipiscing(elit(|| ipsum(dolor(sit::amet(consectetur()))))))))); + + aaaaaaaaaaaaaaaaaa::bbbbbbbbbbbbbb::cccccccccc(ipsum(), dolor(sit::amet(consectetur, adipiscing), elit(vivamus::ipsum::orci(rhoncus())))); + + ipsum(dolor(sit::amet(consectetur(adipiscing(elit(vivamus::ipsum::orci(rhoncus()))))))); +} diff --git a/tests/target/configs/fn_call_layout/compressed.rs b/tests/target/configs/fn_call_layout/compressed.rs new file mode 100644 index 00000000000..807a80b7c98 --- /dev/null +++ b/tests/target/configs/fn_call_layout/compressed.rs @@ -0,0 +1,86 @@ +// rustfmt-fn_call_layout:Compressed + +fn main() { + empty_args(); + single_arg(ipsum); + two_args(ipsum, dolor); + + lorem(ipsum, dolor, sit, amet); + lorem( + ipsum, // some inine comment + dolor, sit, amet, + ); + lorem(ipsum /* some inine comment */, dolor, sit, amet); + ipsum( + dolor, sit, amet, consectetur, adipiscing, elit, vivamus, ipsum, orci, rhoncus, vel, + imperdiet, + ); + + // issue 2010 + let a = i8x32::new( + 0, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5, std::i8::MAX, std::i8::MIN + 1, 100, -100, -32, 0, 1, + -1, 2, -2, 3, -3, 4, -4, 5, -5, std::i8::MAX, std::i8::MIN + 1, 100, -100, -32, + ); + + // issue 4146 + return_monitor_err( + e, channel_state, chan, order, commitment_update.is_some(), revoke_and_ack.is_some(), + ); + + // line comment in the middle of the args + CastCheck::new( + &fn_ctxt, e, from_ty, to_ty, + // We won't show any error to the user, so we don't care what the span is here. + DUMMY_SP, DUMMY_SP, + ); + + // other examples with more complex args + more_complex_args( + |a, b, c| { + if a == 998765390 { + -b * 3 + } else { + c + } + }, + std::ops::Range { start: 3, end: 5 }, std::i8::MAX, + String::from(" hello world!!").as_bytes(), + thread::Builder::new() + .name("thread1".to_string()) + .spawn(move || { + use std::sync::Arc; + + let mut values = Arc::<[u32]>::new_uninit_slice(3); + + // Deferred initialization: + let data = Arc::get_mut(&mut values).unwrap(); + data[0].write(1); + data[1].write(2); + data[2].write(3); + + let values = unsafe { values.assume_init() }; + }), + "another argument", + ); + + // nested calls + ipsum(dolor(sit::amet(consectetur( + aaaaaaaaaaaaaa, bbbbbbbbbbbb, ccccccccccccc, ddddddddddddd, eeeeeeeee, + )))); + + ipsum(dolor(sit::amet(consectetur( + aaaaaaaaaaaaaa, bbbbbbbbbbbb, ccccccccccccc, ddddddddddddd, + adipiscing(elit(|| ipsum(dolor(sit::amet(consectetur()))))), + )))); + + aaaaaaaaaaaaaaaaaa::bbbbbbbbbbbbbb::cccccccccc( + ipsum(), + dolor( + sit::amet(consectetur, adipiscing), elit(vivamus::ipsum::orci(rhoncus())), + ), + ); + + ipsum(dolor(sit::amet(consectetur(adipiscing(elit( + vivamus::ipsum::orci(rhoncus()), + )))))); +} diff --git a/tests/target/configs/fn_call_layout/foo.rs b/tests/target/configs/fn_call_layout/foo.rs new file mode 100644 index 00000000000..276a0bcf4fc --- /dev/null +++ b/tests/target/configs/fn_call_layout/foo.rs @@ -0,0 +1,140 @@ +// rustfmt-fn_call_layout: Foo + +fn main() { + empty_args(); + single_arg(ipsum); + two_args(ipsum, dolor); + + lorem(ipsum, dolor, sit, amet); + lorem( + ipsum, // some inine comment + dolor, sit, amet, + ); + lorem(ipsum /* some inine comment */, dolor, sit, amet); + ipsum( + dolor, + sit, + amet, + consectetur, + adipiscing, + elit, + vivamus, + ipsum, + orci, + rhoncus, + vel, + imperdiet, + ); + + // issue 2010 + let a = i8x32::new( + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + std::i8::MAX, + std::i8::MIN + 1, + 100, + -100, + -32, + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + std::i8::MAX, + std::i8::MIN + 1, + 100, + -100, + -32, + ); + + // issue 4146 + return_monitor_err( + e, + channel_state, + chan, + order, + commitment_update.is_some(), + revoke_and_ack.is_some(), + ); + + // line comment in the middle of the args + CastCheck::new( + &fn_ctxt, e, from_ty, to_ty, + // We won't show any error to the user, so we don't care what the span is here. + DUMMY_SP, DUMMY_SP, + ); + + // other examples with more complex args + more_complex_args( + |a, b, c| { + if a == 998765390 { + -b * 3 + } else { + c + } + }, + std::ops::Range { start: 3, end: 5 }, + std::i8::MAX, + String::from(" hello world!!").as_bytes(), + thread::Builder::new() + .name("thread1".to_string()) + .spawn(move || { + use std::sync::Arc; + + let mut values = Arc::<[u32]>::new_uninit_slice(3); + + // Deferred initialization: + let data = Arc::get_mut(&mut values).unwrap(); + data[0].write(1); + data[1].write(2); + data[2].write(3); + + let values = unsafe { values.assume_init() }; + }), + "another argument", + ); + + // nested calls + ipsum(dolor(sit::amet(consectetur( + aaaaaaaaaaaaaa, + bbbbbbbbbbbb, + ccccccccccccc, + ddddddddddddd, + eeeeeeeee, + )))); + + ipsum(dolor(sit::amet(consectetur( + aaaaaaaaaaaaaa, + bbbbbbbbbbbb, + ccccccccccccc, + ddddddddddddd, + adipiscing(elit(|| ipsum(dolor(sit::amet(consectetur()))))), + )))); + + aaaaaaaaaaaaaaaaaa::bbbbbbbbbbbbbb::cccccccccc( + ipsum(), + dolor( + sit::amet(consectetur, adipiscing), + elit(vivamus::ipsum::orci(rhoncus())), + ), + ); + + ipsum(dolor(sit::amet(consectetur(adipiscing(elit( + vivamus::ipsum::orci(rhoncus()), + )))))); +} diff --git a/tests/target/configs/fn_call_layout/tall.rs b/tests/target/configs/fn_call_layout/tall.rs new file mode 100644 index 00000000000..9dfc6b51bdb --- /dev/null +++ b/tests/target/configs/fn_call_layout/tall.rs @@ -0,0 +1,146 @@ +// rustfmt-fn_call_layout:Tall + +fn main() { + empty_args(); + single_arg(ipsum); + two_args(ipsum, dolor); + + lorem(ipsum, dolor, sit, amet); + lorem( + ipsum, // some inine comment + dolor, + sit, + amet, + ); + lorem(ipsum /* some inine comment */, dolor, sit, amet); + ipsum( + dolor, + sit, + amet, + consectetur, + adipiscing, + elit, + vivamus, + ipsum, + orci, + rhoncus, + vel, + imperdiet, + ); + + // issue 2010 + let a = i8x32::new( + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + std::i8::MAX, + std::i8::MIN + 1, + 100, + -100, + -32, + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + std::i8::MAX, + std::i8::MIN + 1, + 100, + -100, + -32, + ); + + // issue 4146 + return_monitor_err( + e, + channel_state, + chan, + order, + commitment_update.is_some(), + revoke_and_ack.is_some(), + ); + + // line comment in the middle of the args + CastCheck::new( + &fn_ctxt, + e, + from_ty, + to_ty, + // We won't show any error to the user, so we don't care what the span is here. + DUMMY_SP, + DUMMY_SP, + ); + + // other examples with more complex args + more_complex_args( + |a, b, c| { + if a == 998765390 { + -b * 3 + } else { + c + } + }, + std::ops::Range { start: 3, end: 5 }, + std::i8::MAX, + String::from(" hello world!!").as_bytes(), + thread::Builder::new() + .name("thread1".to_string()) + .spawn(move || { + use std::sync::Arc; + + let mut values = Arc::<[u32]>::new_uninit_slice(3); + + // Deferred initialization: + let data = Arc::get_mut(&mut values).unwrap(); + data[0].write(1); + data[1].write(2); + data[2].write(3); + + let values = unsafe { values.assume_init() }; + }), + "another argument", + ); + + // nested calls + ipsum(dolor(sit::amet(consectetur( + aaaaaaaaaaaaaa, + bbbbbbbbbbbb, + ccccccccccccc, + ddddddddddddd, + eeeeeeeee, + )))); + + ipsum(dolor(sit::amet(consectetur( + aaaaaaaaaaaaaa, + bbbbbbbbbbbb, + ccccccccccccc, + ddddddddddddd, + adipiscing(elit(|| ipsum(dolor(sit::amet(consectetur()))))), + )))); + + aaaaaaaaaaaaaaaaaa::bbbbbbbbbbbbbb::cccccccccc( + ipsum(), + dolor( + sit::amet(consectetur, adipiscing), + elit(vivamus::ipsum::orci(rhoncus())), + ), + ); + + ipsum(dolor(sit::amet(consectetur(adipiscing(elit( + vivamus::ipsum::orci(rhoncus()), + )))))); +} diff --git a/tests/target/configs/fn_call_layout/vertical.rs b/tests/target/configs/fn_call_layout/vertical.rs new file mode 100644 index 00000000000..6c2a9836fd5 --- /dev/null +++ b/tests/target/configs/fn_call_layout/vertical.rs @@ -0,0 +1,162 @@ +// rustfmt-fn_call_layout:Vertical + +fn main() { + empty_args(); + single_arg(ipsum); + two_args( + ipsum, + dolor, + ); + + lorem( + ipsum, + dolor, + sit, + amet, + ); + lorem( + ipsum, // some inine comment + dolor, + sit, + amet, + ); + lorem( + ipsum, /* some inine comment */ + dolor, + sit, + amet, + ); + ipsum( + dolor, + sit, + amet, + consectetur, + adipiscing, + elit, + vivamus, + ipsum, + orci, + rhoncus, + vel, + imperdiet, + ); + + // issue 2010 + let a = i8x32::new( + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + std::i8::MAX, + std::i8::MIN + 1, + 100, + -100, + -32, + 0, + 1, + -1, + 2, + -2, + 3, + -3, + 4, + -4, + 5, + -5, + std::i8::MAX, + std::i8::MIN + 1, + 100, + -100, + -32, + ); + + // issue 4146 + return_monitor_err( + e, + channel_state, + chan, + order, + commitment_update.is_some(), + revoke_and_ack.is_some(), + ); + + // line comment in the middle of the args + CastCheck::new( + &fn_ctxt, + e, + from_ty, + to_ty, + // We won't show any error to the user, so we don't care what the span is here. + DUMMY_SP, + DUMMY_SP, + ); + + // other examples with more complex args + more_complex_args( + |a, b, c| { + if a == 998765390 { + -b * 3 + } else { + c + } + }, + std::ops::Range { start: 3, end: 5 }, + std::i8::MAX, + String::from(" hello world!!").as_bytes(), + thread::Builder::new() + .name("thread1".to_string()) + .spawn(move || { + use std::sync::Arc; + + let mut values = Arc::<[u32]>::new_uninit_slice(3); + + // Deferred initialization: + let data = Arc::get_mut(&mut values).unwrap(); + data[0].write(1); + data[1].write(2); + data[2].write(3); + + let values = unsafe { values.assume_init() }; + }), + "another argument", + ); + + // nested calls + ipsum(dolor(sit::amet(consectetur( + aaaaaaaaaaaaaa, + bbbbbbbbbbbb, + ccccccccccccc, + ddddddddddddd, + eeeeeeeee, + )))); + + ipsum(dolor(sit::amet(consectetur( + aaaaaaaaaaaaaa, + bbbbbbbbbbbb, + ccccccccccccc, + ddddddddddddd, + adipiscing(elit(|| ipsum(dolor(sit::amet(consectetur()))))), + )))); + + aaaaaaaaaaaaaaaaaa::bbbbbbbbbbbbbb::cccccccccc( + ipsum(), + dolor( + sit::amet( + consectetur, + adipiscing, + ), + elit(vivamus::ipsum::orci(rhoncus())), + ), + ); + + ipsum(dolor(sit::amet(consectetur(adipiscing(elit( + vivamus::ipsum::orci(rhoncus()), + )))))); +}