diff --git a/src/overflow.rs b/src/overflow.rs index 8c4bac2a8a50..344fb28620b0 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -11,6 +11,7 @@ //! Rewrite a list some items with overflow. use config::lists::*; +use config::Version; use syntax::parse::token::DelimToken; use syntax::source_map::Span; use syntax::{ast, ptr}; @@ -466,6 +467,13 @@ impl<'a> Context<'a> { { self.context.force_one_line_chain.replace(true); } + Some(OverflowableItem::MacroArg(MacroArg::Expr(expr))) + if !combine_arg_with_callee + && is_method_call(expr) + && self.context.config.version() == Version::Two => + { + self.context.force_one_line_chain.replace(true); + } _ => (), } let result = last_item_shape( @@ -632,8 +640,6 @@ impl<'a> Context<'a> { _ => (self.prefix, self.suffix), }; - // 2 = `()` - let fits_one_line = items_str.len() + 2 <= shape.width; let extend_width = if items_str.is_empty() { 2 } else { @@ -652,10 +658,16 @@ impl<'a> Context<'a> { ); result.push_str(self.ident); result.push_str(prefix); - if !self.context.use_block_indent() - || (self.context.inside_macro() && !items_str.contains('\n') && fits_one_line) - || (is_extendable && extend_width <= shape.width) - { + let force_single_line = if self.context.config.version() == Version::Two { + !self.context.use_block_indent() || (is_extendable && extend_width <= shape.width) + } else { + // 2 = `()` + let fits_one_line = items_str.len() + 2 <= shape.width; + !self.context.use_block_indent() + || (self.context.inside_macro() && !items_str.contains('\n') && fits_one_line) + || (is_extendable && extend_width <= shape.width) + }; + if force_single_line { result.push_str(items_str); } else { if !items_str.is_empty() { diff --git a/tests/source/issue-3272/v1.rs b/tests/source/issue-3272/v1.rs new file mode 100644 index 000000000000..f4c1b7c992bf --- /dev/null +++ b/tests/source/issue-3272/v1.rs @@ -0,0 +1,15 @@ +// rustfmt-version: One + +fn main() { + assert!(HAYSTACK + .par_iter() + .find_any(|&&x| x[0] % 1000 == 999) + .is_some()); + + assert( + HAYSTACK + .par_iter() + .find_any(|&&x| x[0] % 1000 == 999) + .is_some(), + ); +} diff --git a/tests/source/issue-3272/v2.rs b/tests/source/issue-3272/v2.rs new file mode 100644 index 000000000000..0148368edc8e --- /dev/null +++ b/tests/source/issue-3272/v2.rs @@ -0,0 +1,15 @@ +// rustfmt-version: Two + +fn main() { + assert!(HAYSTACK + .par_iter() + .find_any(|&&x| x[0] % 1000 == 999) + .is_some()); + + assert( + HAYSTACK + .par_iter() + .find_any(|&&x| x[0] % 1000 == 999) + .is_some(), + ); +} diff --git a/tests/source/macros.rs b/tests/source/macros.rs index 29d8f0661268..5386d68898de 100644 --- a/tests/source/macros.rs +++ b/tests/source/macros.rs @@ -400,14 +400,6 @@ fn foo() { foo!(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,); } -// #2652 -// Preserve trailing comma inside macro, even if it looks an array. -macro_rules! bar { - ($m:ident) => { - $m!([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]); - }; -} - // #2830 // Preserve trailing comma-less/ness inside nested macro. named!( diff --git a/tests/source/single-line-macro/v1.rs b/tests/source/single-line-macro/v1.rs new file mode 100644 index 000000000000..a3aa631ed4af --- /dev/null +++ b/tests/source/single-line-macro/v1.rs @@ -0,0 +1,10 @@ +// rustfmt-version: One + +// #2652 +// Preserve trailing comma inside macro, even if it looks an array. +macro_rules! bar { + ($m:ident) => { + $m!([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z,]); + $m!([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]); + }; +} diff --git a/tests/source/single-line-macro/v2.rs b/tests/source/single-line-macro/v2.rs new file mode 100644 index 000000000000..51a665f75605 --- /dev/null +++ b/tests/source/single-line-macro/v2.rs @@ -0,0 +1,10 @@ +// rustfmt-version: Two + +// #2652 +// Preserve trailing comma inside macro, even if it looks an array. +macro_rules! bar { + ($m:ident) => { + $m!([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z,]); + $m!([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]); + }; +} diff --git a/tests/target/issue-3272/v1.rs b/tests/target/issue-3272/v1.rs new file mode 100644 index 000000000000..aab201027d56 --- /dev/null +++ b/tests/target/issue-3272/v1.rs @@ -0,0 +1,15 @@ +// rustfmt-version: One + +fn main() { + assert!(HAYSTACK + .par_iter() + .find_any(|&&x| x[0] % 1000 == 999) + .is_some()); + + assert( + HAYSTACK + .par_iter() + .find_any(|&&x| x[0] % 1000 == 999) + .is_some(), + ); +} diff --git a/tests/target/issue-3272/v2.rs b/tests/target/issue-3272/v2.rs new file mode 100644 index 000000000000..a42a2fccd5b0 --- /dev/null +++ b/tests/target/issue-3272/v2.rs @@ -0,0 +1,17 @@ +// rustfmt-version: Two + +fn main() { + assert!( + HAYSTACK + .par_iter() + .find_any(|&&x| x[0] % 1000 == 999) + .is_some() + ); + + assert( + HAYSTACK + .par_iter() + .find_any(|&&x| x[0] % 1000 == 999) + .is_some(), + ); +} diff --git a/tests/target/macros.rs b/tests/target/macros.rs index bc15a86cf349..ca5f91b715e2 100644 --- a/tests/target/macros.rs +++ b/tests/target/macros.rs @@ -980,14 +980,6 @@ fn foo() { ); } -// #2652 -// Preserve trailing comma inside macro, even if it looks an array. -macro_rules! bar { - ($m:ident) => { - $m!([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]); - }; -} - // #2830 // Preserve trailing comma-less/ness inside nested macro. named!( diff --git a/tests/target/single-line-macro/v1.rs b/tests/target/single-line-macro/v1.rs new file mode 100644 index 000000000000..a3aa631ed4af --- /dev/null +++ b/tests/target/single-line-macro/v1.rs @@ -0,0 +1,10 @@ +// rustfmt-version: One + +// #2652 +// Preserve trailing comma inside macro, even if it looks an array. +macro_rules! bar { + ($m:ident) => { + $m!([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z,]); + $m!([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]); + }; +} diff --git a/tests/target/single-line-macro/v2.rs b/tests/target/single-line-macro/v2.rs new file mode 100644 index 000000000000..9c6bcf33ad53 --- /dev/null +++ b/tests/target/single-line-macro/v2.rs @@ -0,0 +1,14 @@ +// rustfmt-version: Two + +// #2652 +// Preserve trailing comma inside macro, even if it looks an array. +macro_rules! bar { + ($m:ident) => { + $m!([ + a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, + ]); + $m!([ + a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z + ]); + }; +}