From 9eefc6fc8d113e80f92de53ae756cb5203e173b1 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Sat, 10 Jun 2017 18:24:23 +0900 Subject: Share codes when rewriting generics --- tests/target/configs-generics_indent-block.rs | 2 +- tests/target/fn-custom-2.rs | 6 +++--- tests/target/fn-custom-3.rs | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'tests') diff --git a/tests/target/configs-generics_indent-block.rs b/tests/target/configs-generics_indent-block.rs index 789243c02c3..848e59c7c0a 100644 --- a/tests/target/configs-generics_indent-block.rs +++ b/tests/target/configs-generics_indent-block.rs @@ -8,7 +8,7 @@ fn lorem< Amet: Eq = usize, Adipiscing: Eq = usize, Consectetur: Eq = usize, - Elit: Eq = usize + Elit: Eq = usize, >(ipsum: Ipsum, dolor: Dolor, sit: Sit, diff --git a/tests/target/fn-custom-2.rs b/tests/target/fn-custom-2.rs index e9ba39f666c..f0923bd1a85 100644 --- a/tests/target/fn-custom-2.rs +++ b/tests/target/fn-custom-2.rs @@ -16,7 +16,7 @@ fn foo( fn bar< 'a: 'bbbbbbbbbbbbbbbbbbbbbbbbbbb, TTTTTTTTTTTTT, - UUUUUUUUUUUUUUUUUUUU: WWWWWWWWWWWWWWWWWWWWWWWW + UUUUUUUUUUUUUUUUUUUU: WWWWWWWWWWWWWWWWWWWWWWWW, >( a: Aaaaaaaaaaaaaaa, ) { @@ -51,7 +51,7 @@ impl Foo { fn bar< 'a: 'bbbbbbbbbbbbbbbbbbbbbbbbbbb, TTTTTTTTTTTTT, - UUUUUUUUUUUUUUUUUUUU: WWWWWWWWWWWWWWWWWWWWWWWW + UUUUUUUUUUUUUUUUUUUU: WWWWWWWWWWWWWWWWWWWWWWWW, >( a: Aaaaaaaaaaaaaaa, ) { @@ -69,7 +69,7 @@ struct Foo< TTTTTTTTTTTTTTTTTTTTTTTTTTTT, UUUUUUUUUUUUUUUUUUUUUU, VVVVVVVVVVVVVVVVVVVVVVVVVVV, - WWWWWWWWWWWWWWWWWWWWWWWW + WWWWWWWWWWWWWWWWWWWWWWWW, > { foo: Foo, } diff --git a/tests/target/fn-custom-3.rs b/tests/target/fn-custom-3.rs index a29aac411ba..4d26c9b6951 100644 --- a/tests/target/fn-custom-3.rs +++ b/tests/target/fn-custom-3.rs @@ -16,7 +16,7 @@ fn foo( fn bar< 'a: 'bbbbbbbbbbbbbbbbbbbbbbbbbbb, TTTTTTTTTTTTT, - UUUUUUUUUUUUUUUUUUUU: WWWWWWWWWWWWWWWWWWWWWWWW + UUUUUUUUUUUUUUUUUUUU: WWWWWWWWWWWWWWWWWWWWWWWW, >( a: Aaaaaaaaaaaaaaa, ) { @@ -53,7 +53,7 @@ impl Foo { fn bar< 'a: 'bbbbbbbbbbbbbbbbbbbbbbbbbbb, TTTTTTTTTTTTT, - UUUUUUUUUUUUUUUUUUUU: WWWWWWWWWWWWWWWWWWWWWWWW + UUUUUUUUUUUUUUUUUUUU: WWWWWWWWWWWWWWWWWWWWWWWW, >( a: Aaaaaaaaaaaaaaa, ) { @@ -65,7 +65,7 @@ struct Foo< TTTTTTTTTTTTTTTTTTTTTTTTTTTT, UUUUUUUUUUUUUUUUUUUUUU, VVVVVVVVVVVVVVVVVVVVVVVVVVV, - WWWWWWWWWWWWWWWWWWWWWWWW + WWWWWWWWWWWWWWWWWWWWWWWW, > { foo: Foo, } -- cgit 1.4.1-3-g733a5 From bd80077be89b3e8584d8c93af263e38c0e156d5b Mon Sep 17 00:00:00 2001 From: topecongiro Date: Sat, 10 Jun 2017 20:37:34 +0900 Subject: Add format_trait_ref_then_update_result --- src/items.rs | 136 +++++++++++++++++++++---------------------- tests/source/big-impl-rfc.rs | 114 ++++++++++++++++++++++++++++++++++++ tests/target/big-impl-rfc.rs | 65 +++++++++++++++++++++ 3 files changed, 245 insertions(+), 70 deletions(-) create mode 100644 tests/source/big-impl-rfc.rs create mode 100644 tests/target/big-impl-rfc.rs (limited to 'tests') diff --git a/src/items.rs b/src/items.rs index f228bdf9d08..a0b50bd50b7 100644 --- a/src/items.rs +++ b/src/items.rs @@ -678,83 +678,30 @@ fn format_impl_ref_and_type(context: &RewriteContext, 0); let mut generics_str = try_opt!(rewrite_generics(context, generics, shape, shape.width, mk_sp(lo, hi))); + add_polarity(&mut generics_str, &polarity, trait_ref.is_some()); - if polarity == ast::ImplPolarity::Negative { - generics_str.push_str(" !"); - } - - let mut retry_with_multiline = true; if let Some(ref trait_ref) = *trait_ref { - if polarity != ast::ImplPolarity::Negative { - generics_str.push_str(" "); - } - let used_space = if generics_str.contains('\n') { - last_line_width(&generics_str) - } else { - result.len() + generics_str.len() - }; - let budget = context - .config - .max_width() - .checked_sub(used_space) - .unwrap_or(0); - let indent = offset + used_space; - if let Some(trait_ref_str) = trait_ref.rewrite(context, Shape::legacy(budget, indent)) { - if !trait_ref_str.contains('\n') { - result.push_str(&generics_str); - result.push_str(&trait_ref_str); - if split_at_for { - result.push('\n'); - // Add indentation of one additional tab. - result.push_str(&offset - .block_indent(context.config) - .to_string(context.config)); - result.push_str("for"); - } else { - result.push_str(" for"); - } - retry_with_multiline = false; - } - } - if retry_with_multiline { + let success = format_trait_ref_then_update_result(context, + &trait_ref, + offset, + &generics_str, + split_at_for, + &mut result); + if !success { let mut generics_str = try_opt!(rewrite_generics(context, generics, shape, 0, mk_sp(lo, hi))); - if polarity == ast::ImplPolarity::Negative { - generics_str.push_str(" !"); - } else { - generics_str.push_str(" "); - } - let used_space = if generics_str.contains('\n') { - last_line_width(&generics_str) - } else { - result.len() + generics_str.len() - }; - let budget = context - .config - .max_width() - .checked_sub(used_space) - .unwrap_or(0); - let indent = offset + used_space; - if let Some(trait_ref_str) = - trait_ref.rewrite(context, Shape::legacy(budget, indent)) { - result.push_str(&generics_str); - result.push_str(&trait_ref_str); - if split_at_for { - result.push('\n'); - // Add indentation of one additional tab. - result.push_str(&offset - .block_indent(context.config) - .to_string(context.config)); - result.push_str("for"); - } else { - result.push_str(" for"); - } + add_polarity(&mut generics_str, &polarity, true); + if !format_trait_ref_then_update_result(context, + &trait_ref, + offset, + &generics_str, + split_at_for, + &mut result) { + // FIXME: should be unreachable + return None; } } } else { - if polarity == ast::ImplPolarity::Negative { - generics_str.push_str(" "); - } result.push_str(&generics_str); } @@ -790,6 +737,55 @@ fn format_impl_ref_and_type(context: &RewriteContext, } } +// Returns false if failed to update result: then, try using multiline. +fn format_trait_ref_then_update_result(context: &RewriteContext, + trait_ref: &ast::TraitRef, + offset: Indent, + generics_str: &str, + split_at_for: bool, + result: &mut String) + -> bool { + let used_space = if generics_str.contains('\n') { + last_line_width(&generics_str) + } else { + result.len() + generics_str.len() + }; + let budget = context + .config + .max_width() + .checked_sub(used_space) + .unwrap_or(0); + let indent = offset + used_space; + if let Some(trait_ref_str) = trait_ref.rewrite(context, Shape::legacy(budget, indent)) { + if !trait_ref_str.contains('\n') { + result.push_str(&generics_str); + result.push_str(&trait_ref_str); + if split_at_for { + result.push('\n'); + // Add indentation of one additional tab. + let for_offset = match context.config.where_style() { + Style::Legacy => offset.block_indent(context.config), + Style::Rfc => offset, + }; + result.push_str(&for_offset.to_string(context.config)); + result.push_str("for"); + } else { + result.push_str(" for"); + } + return true; + } + } + false +} + +fn add_polarity(s: &mut String, polarity: &ast::ImplPolarity, has_trait_ref: bool) { + if polarity == &ast::ImplPolarity::Negative { + s.push_str(" !") + } else if has_trait_ref { + s.push(' ') + } +} + pub fn format_struct(context: &RewriteContext, item_name: &str, ident: ast::Ident, diff --git a/tests/source/big-impl-rfc.rs b/tests/source/big-impl-rfc.rs new file mode 100644 index 00000000000..167f654cc43 --- /dev/null +++ b/tests/source/big-impl-rfc.rs @@ -0,0 +1,114 @@ +// rustfmt-fn_args_layout: Block +// rustfmt-fn_call_style: Block +// rustfmt-generics_indent: Block +// rustfmt-where_style: Rfc + +// #1357 +impl< + 'a, + Select, + From, + Distinct, + Where, + Order, + Limit, + Offset, + Groupby, + DB, +> InternalBoxedDsl<'a, DB> + for SelectStatement< + Select, + From, + Distinct, + Where, + Order, + Limit, + Offset, + GroupBy, + > where + DB: Backend, + Select: QueryFragment + SelectableExpression + 'a, + Distinct: QueryFragment + 'a, + Where: Into + 'a>>>, + Order: QueryFragment + 'a, + Limit: QueryFragment + 'a, + Offset: QueryFragment + 'a, +{ + type Output = BoxedSelectStatement<'a, Select::SqlTypeForSelect, From, DB>; + + fn internal_into_boxed(self) -> Self::Output { + BoxedSelectStatement::new( + Box::new(self.select), + self.from, + Box::new(self.distinct), + self.where_clause.into(), + Box::new(self.order), + Box::new(self.limit), + Box::new(self.offset), + ) + } +} + +// #1369 +impl< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> Foo for Bar { + fn foo() {} +} +impl Foo< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> for Bar { + fn foo() {} +} +impl< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> Foo< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> for Bar { + fn foo() {} +} +impl< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> Foo for Bar< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> { + fn foo() {} +} +impl Foo< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> for Bar< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> { + fn foo() {} +} +impl< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> Foo< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> for Bar< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> { + fn foo() {} +} diff --git a/tests/target/big-impl-rfc.rs b/tests/target/big-impl-rfc.rs new file mode 100644 index 00000000000..18fa59cb5a0 --- /dev/null +++ b/tests/target/big-impl-rfc.rs @@ -0,0 +1,65 @@ +// rustfmt-fn_args_layout: Block +// rustfmt-fn_call_style: Block +// rustfmt-generics_indent: Block +// rustfmt-where_style: Rfc + +// #1357 +impl<'a, Select, From, Distinct, Where, Order, Limit, Offset, Groupby, DB> InternalBoxedDsl<'a, DB> +for SelectStatement +where + DB: Backend, + Select: QueryFragment + SelectableExpression + 'a, + Distinct: QueryFragment + 'a, + Where: Into + 'a>>>, + Order: QueryFragment + 'a, + Limit: QueryFragment + 'a, + Offset: QueryFragment + 'a, +{ + type Output = BoxedSelectStatement<'a, Select::SqlTypeForSelect, From, DB>; + + fn internal_into_boxed(self) -> Self::Output { + BoxedSelectStatement::new( + Box::new(self.select), + self.from, + Box::new(self.distinct), + self.where_clause.into(), + Box::new(self.order), + Box::new(self.limit), + Box::new(self.offset), + ) + } +} + +// #1369 +impl Foo +for Bar { + fn foo() {} +} +impl Foo +for Bar { + fn foo() {} +} +impl< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> Foo +for Bar { + fn foo() {} +} +impl Foo +for Bar { + fn foo() {} +} +impl Foo +for Bar { + fn foo() {} +} +impl< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> Foo +for Bar { + fn foo() {} +} -- cgit 1.4.1-3-g733a5 From e94fcfcd39c2658812a59f1adc0f6fca5a36a0cf Mon Sep 17 00:00:00 2001 From: topecongiro Date: Sun, 11 Jun 2017 23:27:02 +0900 Subject: Update tests --- tests/source/big-impl.rs | 104 +++++++++++++++++++++++++++++++++++++++++++ tests/source/impls.rs | 17 +++++++ tests/target/big-impl-rfc.rs | 26 ++++++++--- tests/target/big-impl.rs | 62 ++++++++++++++++++++++++++ tests/target/impls.rs | 36 ++++++++++++++- 5 files changed, 236 insertions(+), 9 deletions(-) create mode 100644 tests/source/big-impl.rs create mode 100644 tests/target/big-impl.rs (limited to 'tests') diff --git a/tests/source/big-impl.rs b/tests/source/big-impl.rs new file mode 100644 index 00000000000..c36b7e6cadb --- /dev/null +++ b/tests/source/big-impl.rs @@ -0,0 +1,104 @@ +// #1357 +impl< + 'a, + Select, + From, + Distinct, + Where, + Order, + Limit, + Offset, + Groupby, + DB, +> InternalBoxedDsl<'a, DB> + for SelectStatement< + Select, + From, + Distinct, + Where, + Order, + Limit, + Offset, + GroupBy, + > where + DB: Backend, + Select: QueryFragment + SelectableExpression + 'a, + Distinct: QueryFragment + 'a, + Where: Into + 'a>>>, + Order: QueryFragment + 'a, + Limit: QueryFragment + 'a, + Offset: QueryFragment + 'a, +{ + type Output = BoxedSelectStatement<'a, Select::SqlTypeForSelect, From, DB>; + + fn internal_into_boxed(self) -> Self::Output { + BoxedSelectStatement::new( + Box::new(self.select), + self.from, + Box::new(self.distinct), + self.where_clause.into(), + Box::new(self.order), + Box::new(self.limit), + Box::new(self.offset), + ) + } +} + +// #1369 +impl< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> Foo for Bar { + fn foo() {} +} +impl Foo< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> for Bar { + fn foo() {} +} +impl< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> Foo< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> for Bar { + fn foo() {} +} +impl< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> Foo for Bar< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> { + fn foo() {} +} +impl Foo< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> for Bar< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, +> { + fn foo() {} +} +impl Foo + for Bar { + fn foo() {} +} diff --git a/tests/source/impls.rs b/tests/source/impls.rs index 1c38a2ff284..17588fb9d35 100644 --- a/tests/source/impls.rs +++ b/tests/source/impls.rs @@ -122,3 +122,20 @@ impl Issue1249 Drop for RawTable { fn drop() {} } + +// #1168 +pub trait Number: Copy + Eq + Not + Shl + + Shr + + BitAnd + BitOr + BitAndAssign + BitOrAssign + + + +{ + // test + fn zero() -> Self; +} + +// #1642 +pub trait SomeTrait : Clone + Eq + PartialEq + Ord + PartialOrd + Default + Hash + Debug + Display + Write + Read + FromStr { + // comment +} diff --git a/tests/target/big-impl-rfc.rs b/tests/target/big-impl-rfc.rs index 18fa59cb5a0..108968faaed 100644 --- a/tests/target/big-impl-rfc.rs +++ b/tests/target/big-impl-rfc.rs @@ -5,7 +5,7 @@ // #1357 impl<'a, Select, From, Distinct, Where, Order, Limit, Offset, Groupby, DB> InternalBoxedDsl<'a, DB> -for SelectStatement + for SelectStatement where DB: Backend, Select: QueryFragment + SelectableExpression + 'a, @@ -32,11 +32,11 @@ where // #1369 impl Foo -for Bar { + for Bar { fn foo() {} } impl Foo -for Bar { + for Bar { fn foo() {} } impl< @@ -44,15 +44,23 @@ impl< ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName, > Foo -for Bar { + for Bar { fn foo() {} } impl Foo -for Bar { + for Bar< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, + > { fn foo() {} } impl Foo -for Bar { + for Bar< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, + > { fn foo() {} } impl< @@ -60,6 +68,10 @@ impl< ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName, > Foo -for Bar { + for Bar< + ExcessivelyLongGenericName, + ExcessivelyLongGenericName, + AnotherExcessivelyLongGenericName, + > { fn foo() {} } diff --git a/tests/target/big-impl.rs b/tests/target/big-impl.rs new file mode 100644 index 00000000000..afe2571ec68 --- /dev/null +++ b/tests/target/big-impl.rs @@ -0,0 +1,62 @@ +// #1357 +impl<'a, Select, From, Distinct, Where, Order, Limit, Offset, Groupby, DB> InternalBoxedDsl<'a, DB> + for SelectStatement + where DB: Backend, + Select: QueryFragment + SelectableExpression + 'a, + Distinct: QueryFragment + 'a, + Where: Into + 'a>>>, + Order: QueryFragment + 'a, + Limit: QueryFragment + 'a, + Offset: QueryFragment + 'a +{ + type Output = BoxedSelectStatement<'a, Select::SqlTypeForSelect, From, DB>; + + fn internal_into_boxed(self) -> Self::Output { + BoxedSelectStatement::new(Box::new(self.select), + self.from, + Box::new(self.distinct), + self.where_clause.into(), + Box::new(self.order), + Box::new(self.limit), + Box::new(self.offset)) + } +} + +// #1369 +impl Foo + for Bar { + fn foo() {} +} +impl Foo + for Bar { + fn foo() {} +} +impl Foo for Bar { + fn foo() {} +} +impl Foo + for Bar { + fn foo() {} +} +impl Foo + for Bar { + fn foo() {} +} +impl Foo + for Bar { + fn foo() {} +} diff --git a/tests/target/impls.rs b/tests/target/impls.rs index f8b9f43aeb0..654d4be8967 100644 --- a/tests/target/impls.rs +++ b/tests/target/impls.rs @@ -126,8 +126,8 @@ mod m { impl PartialEq for S where T: PartialEq {} } -impl Handle, - HandleType> { +impl + Handle, HandleType> { } impl PartialEq @@ -154,3 +154,35 @@ impl impl<#[may_dangle] K, #[may_dangle] V> Drop for RawTable { fn drop() {} } + +// #1168 +pub trait Number + : Copy + + Eq + + Not + + Shl + + Shr + + BitAnd + + BitOr + + BitAndAssign + + BitOrAssign { + // test + fn zero() -> Self; +} + +// #1642 +pub trait SomeTrait + : Clone + + Eq + + PartialEq + + Ord + + PartialOrd + + Default + + Hash + + Debug + + Display + + Write + + Read + + FromStr { + // comment +} -- cgit 1.4.1-3-g733a5 From 9ad499786d1eb8008423d9d9810b47c4124e8d47 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Mon, 12 Jun 2017 15:58:27 +0900 Subject: Use block indent for args if single line args exceeds max width --- src/expr.rs | 18 +++++++++++++++--- tests/source/configs-fn_call_style-block.rs | 6 ++++++ .../configs-fn_call_style-block-trailing-comma.rs | 4 +++- tests/target/configs-fn_call_style-block.rs | 7 +++++++ 4 files changed, 31 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/src/expr.rs b/src/expr.rs index aa5913e5f0f..b47bf391ac1 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1734,9 +1734,12 @@ fn rewrite_call_inner(context: &RewriteContext, force_trailing_comma); } + let args_shape = shape + .sub_width(last_line_width(&callee_str)) + .ok_or(Ordering::Less)?; Ok(format!("{}{}", callee_str, - wrap_args_with_parens(context, &list_str, extendable, shape, nested_shape))) + wrap_args_with_parens(context, &list_str, extendable, args_shape, nested_shape))) } fn need_block_indent(s: &str, shape: Shape) -> bool { @@ -1906,14 +1909,23 @@ fn can_be_overflowed_expr(context: &RewriteContext, expr: &ast::Expr, args_len: } } +fn paren_overhead(context: &RewriteContext) -> usize { + if context.config.spaces_within_parens() { + 4 + } else { + 2 + } +} + fn wrap_args_with_parens(context: &RewriteContext, args_str: &str, is_extendable: bool, shape: Shape, nested_shape: Shape) -> String { - if !context.use_block_indent() || (context.inside_macro && !args_str.contains('\n')) || - is_extendable { + if !context.use_block_indent() || + (context.inside_macro && !args_str.contains('\n') && + args_str.len() + paren_overhead(context) <= shape.width) || is_extendable { if context.config.spaces_within_parens() && args_str.len() > 0 { format!("( {} )", args_str) } else { diff --git a/tests/source/configs-fn_call_style-block.rs b/tests/source/configs-fn_call_style-block.rs index 2068216d18e..ee6178c1902 100644 --- a/tests/source/configs-fn_call_style-block.rs +++ b/tests/source/configs-fn_call_style-block.rs @@ -125,3 +125,9 @@ fn issue1581() { }, ); } + +fn issue1651() { + { + let type_list: Vec<_> = try_opt!(types.iter().map(|ty| ty.rewrite(context, shape)).collect()); + } +} diff --git a/tests/target/configs-fn_call_style-block-trailing-comma.rs b/tests/target/configs-fn_call_style-block-trailing-comma.rs index ebdf41d0e3b..b6eb94eb677 100644 --- a/tests/target/configs-fn_call_style-block-trailing-comma.rs +++ b/tests/target/configs-fn_call_style-block-trailing-comma.rs @@ -3,7 +3,9 @@ // rustfmt should not add trailing comma when rewriting macro. See #1528. fn a() { - panic!("this is a long string that goes past the maximum line length causing rustfmt to insert a comma here:"); + panic!( + "this is a long string that goes past the maximum line length causing rustfmt to insert a comma here:" + ); foo( oooptoptoptoptptooptoptoptoptptooptoptoptoptptoptoptoptoptpt(), ); diff --git a/tests/target/configs-fn_call_style-block.rs b/tests/target/configs-fn_call_style-block.rs index ddead8ce5a8..dfc8daef6d9 100644 --- a/tests/target/configs-fn_call_style-block.rs +++ b/tests/target/configs-fn_call_style-block.rs @@ -145,3 +145,10 @@ fn issue1581() { } }); } + +fn issue1651() { + { + let type_list: Vec<_> = + try_opt!(types.iter().map(|ty| ty.rewrite(context, shape)).collect()); + } +} -- cgit 1.4.1-3-g733a5 From d269189f19f62de61564d9225cef18d9482bb55c Mon Sep 17 00:00:00 2001 From: topecongiro Date: Mon, 12 Jun 2017 16:23:10 +0900 Subject: Use correct indent for return type when it goes multi line --- src/items.rs | 2 +- tests/source/configs-fn_args_layout-block.rs | 4 ++++ tests/target/configs-fn_args_layout-block.rs | 11 +++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/src/items.rs b/src/items.rs index ac2f36ad76a..43101d9187b 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1798,7 +1798,7 @@ fn rewrite_fn_base(context: &RewriteContext, indent } else { result.push(' '); - Indent::new(indent.width(), result.len()) + Indent::new(indent.block_indent, last_line_width(&result)) }; if multi_line_ret_str || ret_should_indent { diff --git a/tests/source/configs-fn_args_layout-block.rs b/tests/source/configs-fn_args_layout-block.rs index 55b38101abc..28d1dffe6b3 100644 --- a/tests/source/configs-fn_args_layout-block.rs +++ b/tests/source/configs-fn_args_layout-block.rs @@ -20,3 +20,7 @@ extern "C" { second_parameter: SecondParameterType, ...); } + +// #1652 +fn deconstruct(foo: Bar) -> (SocketAddr, Header, Method, RequestUri, HttpVersion, AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA) { +} diff --git a/tests/target/configs-fn_args_layout-block.rs b/tests/target/configs-fn_args_layout-block.rs index 04a4d631ca4..f93b84f2d4a 100644 --- a/tests/target/configs-fn_args_layout-block.rs +++ b/tests/target/configs-fn_args_layout-block.rs @@ -32,3 +32,14 @@ extern "C" { ... ); } + +// #1652 +fn deconstruct( + foo: Bar, +) -> (SocketAddr, + Header, + Method, + RequestUri, + HttpVersion, + AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA) { +} -- cgit 1.4.1-3-g733a5 From 57fc39305dc765d4454869ab6a6b9667c17b6e57 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Mon, 12 Jun 2017 17:25:38 +0900 Subject: Put guard on newline if it exceeds max width --- src/expr.rs | 28 +++++++++++----------------- tests/target/configs-control_style-rfc.rs | 17 +++++++++++++++++ tests/target/match.rs | 4 ++-- 3 files changed, 30 insertions(+), 19 deletions(-) (limited to 'tests') diff --git a/src/expr.rs b/src/expr.rs index e6889991b07..06993c729b7 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1495,28 +1495,22 @@ fn rewrite_guard(context: &RewriteContext, if let Some(ref guard) = *guard { // First try to fit the guard string on the same line as the pattern. // 4 = ` if `, 5 = ` => {` - let overhead = pattern_width + 4 + 5; - if overhead < shape.width { - let cond_shape = shape - .shrink_left(pattern_width + 4) - .unwrap() - .sub_width(5) - .unwrap(); - let cond_str = guard.rewrite(context, cond_shape); - if let Some(cond_str) = cond_str { + if let Some(cond_shape) = shape + .shrink_left(pattern_width + 4) + .and_then(|s| s.sub_width(5)) { + if let Some(cond_str) = guard + .rewrite(context, cond_shape) + .and_then(|s| s.rewrite(context, cond_shape)) { return Some(format!(" if {}", cond_str)); } } // Not enough space to put the guard after the pattern, try a newline. - let overhead = shape.indent.block_indent(context.config).width() + 4 + 5; - if overhead < shape.width { - let cond_str = guard.rewrite(context, - Shape::legacy(shape.width - overhead, - // 3 == `if ` - shape.indent.block_indent(context.config) + - 3)); - if let Some(cond_str) = cond_str { + // 3 == `if ` + if let Some(cond_shape) = Shape::indented(shape.indent.block_indent(context.config) + 3, + context.config) + .sub_width(3) { + if let Some(cond_str) = guard.rewrite(context, cond_shape) { return Some(format!("\n{}if {}", shape .indent diff --git a/tests/target/configs-control_style-rfc.rs b/tests/target/configs-control_style-rfc.rs index a7213c34dfb..43a10e92339 100644 --- a/tests/target/configs-control_style-rfc.rs +++ b/tests/target/configs-control_style-rfc.rs @@ -21,3 +21,20 @@ fn main() { } } } + +fn issue1656() { + { + { + match rewrite { + Some(ref body_str) + if (!body_str.contains('\n') && body_str.len() <= arm_shape.width) || + !context.config.wrap_match_arms() || + (extend && first_line_width(body_str) <= arm_shape.width) || + is_block => { + return None; + } + _ => {} + } + } + } +} diff --git a/tests/target/match.rs b/tests/target/match.rs index 6acab043e67..45f62ca6845 100644 --- a/tests/target/match.rs +++ b/tests/target/match.rs @@ -306,8 +306,8 @@ fn guards() { barrrrrrrrrrrr => {} aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa if fooooooooooooooooooooo && - (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb || - cccccccccccccccccccccccccccccccccccccccc) => {} + (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb || cccccccccccccccccccccccccccccccccccccccc) => { + } } } -- cgit 1.4.1-3-g733a5