From 5df0a18849b11baf33f9a5ee0cc58e6f3be7e518 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Sun, 27 Jan 2019 20:50:16 +0900 Subject: [PATCH 1/2] Avoid putting a long macro call in a single line --- src/overflow.rs | 17 +++++++++++------ tests/source/macros.rs | 8 -------- tests/source/single-line-macro/v1.rs | 10 ++++++++++ tests/source/single-line-macro/v2.rs | 10 ++++++++++ tests/target/macros.rs | 8 -------- tests/target/single-line-macro/v1.rs | 10 ++++++++++ tests/target/single-line-macro/v2.rs | 14 ++++++++++++++ 7 files changed, 55 insertions(+), 22 deletions(-) create mode 100644 tests/source/single-line-macro/v1.rs create mode 100644 tests/source/single-line-macro/v2.rs create mode 100644 tests/target/single-line-macro/v1.rs create mode 100644 tests/target/single-line-macro/v2.rs diff --git a/src/overflow.rs b/src/overflow.rs index 8c4bac2a8a50..8cc42cadfd40 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}; @@ -632,8 +633,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 +651,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/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/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 + ]); + }; +} From 181ca427dccf3ed4b3e483177718f67fb8ddec5c Mon Sep 17 00:00:00 2001 From: topecongiro Date: Sun, 27 Jan 2019 21:01:12 +0900 Subject: [PATCH 2/2] Use the same rule with macro and function calls with a single chain --- src/overflow.rs | 7 +++++++ tests/source/issue-3272/v1.rs | 15 +++++++++++++++ tests/source/issue-3272/v2.rs | 15 +++++++++++++++ tests/target/issue-3272/v1.rs | 15 +++++++++++++++ tests/target/issue-3272/v2.rs | 17 +++++++++++++++++ 5 files changed, 69 insertions(+) create mode 100644 tests/source/issue-3272/v1.rs create mode 100644 tests/source/issue-3272/v2.rs create mode 100644 tests/target/issue-3272/v1.rs create mode 100644 tests/target/issue-3272/v2.rs diff --git a/src/overflow.rs b/src/overflow.rs index 8cc42cadfd40..344fb28620b0 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -467,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( 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/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(), + ); +}