Skip to content

Commit

Permalink
Merge pull request rust-lang#3298 from topecongiro/issue-3272
Browse files Browse the repository at this point in the history
Use the same rule between function and macro
  • Loading branch information
topecongiro authored Jan 29, 2019
2 parents 36c9dc6 + 181ca42 commit 923da60
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 22 deletions.
24 changes: 18 additions & 6 deletions src/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand All @@ -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() {
Expand Down
15 changes: 15 additions & 0 deletions tests/source/issue-3272/v1.rs
Original file line number Diff line number Diff line change
@@ -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(),
);
}
15 changes: 15 additions & 0 deletions tests/source/issue-3272/v2.rs
Original file line number Diff line number Diff line change
@@ -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(),
);
}
8 changes: 0 additions & 8 deletions tests/source/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
10 changes: 10 additions & 0 deletions tests/source/single-line-macro/v1.rs
Original file line number Diff line number Diff line change
@@ -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]);
};
}
10 changes: 10 additions & 0 deletions tests/source/single-line-macro/v2.rs
Original file line number Diff line number Diff line change
@@ -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]);
};
}
15 changes: 15 additions & 0 deletions tests/target/issue-3272/v1.rs
Original file line number Diff line number Diff line change
@@ -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(),
);
}
17 changes: 17 additions & 0 deletions tests/target/issue-3272/v2.rs
Original file line number Diff line number Diff line change
@@ -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(),
);
}
8 changes: 0 additions & 8 deletions tests/target/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
10 changes: 10 additions & 0 deletions tests/target/single-line-macro/v1.rs
Original file line number Diff line number Diff line change
@@ -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]);
};
}
14 changes: 14 additions & 0 deletions tests/target/single-line-macro/v2.rs
Original file line number Diff line number Diff line change
@@ -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
]);
};
}

0 comments on commit 923da60

Please sign in to comment.