Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 923da60

Browse files
authored
Merge pull request rust-lang#3298 from topecongiro/issue-3272
Use the same rule between function and macro
2 parents 36c9dc6 + 181ca42 commit 923da60

File tree

11 files changed

+124
-22
lines changed

11 files changed

+124
-22
lines changed

src/overflow.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! Rewrite a list some items with overflow.
1212
1313
use config::lists::*;
14+
use config::Version;
1415
use syntax::parse::token::DelimToken;
1516
use syntax::source_map::Span;
1617
use syntax::{ast, ptr};
@@ -466,6 +467,13 @@ impl<'a> Context<'a> {
466467
{
467468
self.context.force_one_line_chain.replace(true);
468469
}
470+
Some(OverflowableItem::MacroArg(MacroArg::Expr(expr)))
471+
if !combine_arg_with_callee
472+
&& is_method_call(expr)
473+
&& self.context.config.version() == Version::Two =>
474+
{
475+
self.context.force_one_line_chain.replace(true);
476+
}
469477
_ => (),
470478
}
471479
let result = last_item_shape(
@@ -632,8 +640,6 @@ impl<'a> Context<'a> {
632640
_ => (self.prefix, self.suffix),
633641
};
634642

635-
// 2 = `()`
636-
let fits_one_line = items_str.len() + 2 <= shape.width;
637643
let extend_width = if items_str.is_empty() {
638644
2
639645
} else {
@@ -652,10 +658,16 @@ impl<'a> Context<'a> {
652658
);
653659
result.push_str(self.ident);
654660
result.push_str(prefix);
655-
if !self.context.use_block_indent()
656-
|| (self.context.inside_macro() && !items_str.contains('\n') && fits_one_line)
657-
|| (is_extendable && extend_width <= shape.width)
658-
{
661+
let force_single_line = if self.context.config.version() == Version::Two {
662+
!self.context.use_block_indent() || (is_extendable && extend_width <= shape.width)
663+
} else {
664+
// 2 = `()`
665+
let fits_one_line = items_str.len() + 2 <= shape.width;
666+
!self.context.use_block_indent()
667+
|| (self.context.inside_macro() && !items_str.contains('\n') && fits_one_line)
668+
|| (is_extendable && extend_width <= shape.width)
669+
};
670+
if force_single_line {
659671
result.push_str(items_str);
660672
} else {
661673
if !items_str.is_empty() {

tests/source/issue-3272/v1.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// rustfmt-version: One
2+
3+
fn main() {
4+
assert!(HAYSTACK
5+
.par_iter()
6+
.find_any(|&&x| x[0] % 1000 == 999)
7+
.is_some());
8+
9+
assert(
10+
HAYSTACK
11+
.par_iter()
12+
.find_any(|&&x| x[0] % 1000 == 999)
13+
.is_some(),
14+
);
15+
}

tests/source/issue-3272/v2.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// rustfmt-version: Two
2+
3+
fn main() {
4+
assert!(HAYSTACK
5+
.par_iter()
6+
.find_any(|&&x| x[0] % 1000 == 999)
7+
.is_some());
8+
9+
assert(
10+
HAYSTACK
11+
.par_iter()
12+
.find_any(|&&x| x[0] % 1000 == 999)
13+
.is_some(),
14+
);
15+
}

tests/source/macros.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,6 @@ fn foo() {
400400
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,);
401401
}
402402

403-
// #2652
404-
// Preserve trailing comma inside macro, even if it looks an array.
405-
macro_rules! bar {
406-
($m:ident) => {
407-
$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]);
408-
};
409-
}
410-
411403
// #2830
412404
// Preserve trailing comma-less/ness inside nested macro.
413405
named!(

tests/source/single-line-macro/v1.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-version: One
2+
3+
// #2652
4+
// Preserve trailing comma inside macro, even if it looks an array.
5+
macro_rules! bar {
6+
($m:ident) => {
7+
$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,]);
8+
$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]);
9+
};
10+
}

tests/source/single-line-macro/v2.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-version: Two
2+
3+
// #2652
4+
// Preserve trailing comma inside macro, even if it looks an array.
5+
macro_rules! bar {
6+
($m:ident) => {
7+
$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,]);
8+
$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]);
9+
};
10+
}

tests/target/issue-3272/v1.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// rustfmt-version: One
2+
3+
fn main() {
4+
assert!(HAYSTACK
5+
.par_iter()
6+
.find_any(|&&x| x[0] % 1000 == 999)
7+
.is_some());
8+
9+
assert(
10+
HAYSTACK
11+
.par_iter()
12+
.find_any(|&&x| x[0] % 1000 == 999)
13+
.is_some(),
14+
);
15+
}

tests/target/issue-3272/v2.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// rustfmt-version: Two
2+
3+
fn main() {
4+
assert!(
5+
HAYSTACK
6+
.par_iter()
7+
.find_any(|&&x| x[0] % 1000 == 999)
8+
.is_some()
9+
);
10+
11+
assert(
12+
HAYSTACK
13+
.par_iter()
14+
.find_any(|&&x| x[0] % 1000 == 999)
15+
.is_some(),
16+
);
17+
}

tests/target/macros.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -980,14 +980,6 @@ fn foo() {
980980
);
981981
}
982982

983-
// #2652
984-
// Preserve trailing comma inside macro, even if it looks an array.
985-
macro_rules! bar {
986-
($m:ident) => {
987-
$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]);
988-
};
989-
}
990-
991983
// #2830
992984
// Preserve trailing comma-less/ness inside nested macro.
993985
named!(

tests/target/single-line-macro/v1.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-version: One
2+
3+
// #2652
4+
// Preserve trailing comma inside macro, even if it looks an array.
5+
macro_rules! bar {
6+
($m:ident) => {
7+
$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,]);
8+
$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]);
9+
};
10+
}

tests/target/single-line-macro/v2.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// rustfmt-version: Two
2+
3+
// #2652
4+
// Preserve trailing comma inside macro, even if it looks an array.
5+
macro_rules! bar {
6+
($m:ident) => {
7+
$m!([
8+
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,
9+
]);
10+
$m!([
11+
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
12+
]);
13+
};
14+
}

0 commit comments

Comments
 (0)