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

Commit 5df0a18

Browse files
committed
Avoid putting a long macro call in a single line
1 parent 203e6d2 commit 5df0a18

File tree

7 files changed

+55
-22
lines changed

7 files changed

+55
-22
lines changed

src/overflow.rs

Lines changed: 11 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};
@@ -632,8 +633,6 @@ impl<'a> Context<'a> {
632633
_ => (self.prefix, self.suffix),
633634
};
634635

635-
// 2 = `()`
636-
let fits_one_line = items_str.len() + 2 <= shape.width;
637636
let extend_width = if items_str.is_empty() {
638637
2
639638
} else {
@@ -652,10 +651,16 @@ impl<'a> Context<'a> {
652651
);
653652
result.push_str(self.ident);
654653
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-
{
654+
let force_single_line = if self.context.config.version() == Version::Two {
655+
!self.context.use_block_indent() || (is_extendable && extend_width <= shape.width)
656+
} else {
657+
// 2 = `()`
658+
let fits_one_line = items_str.len() + 2 <= shape.width;
659+
!self.context.use_block_indent()
660+
|| (self.context.inside_macro() && !items_str.contains('\n') && fits_one_line)
661+
|| (is_extendable && extend_width <= shape.width)
662+
};
663+
if force_single_line {
659664
result.push_str(items_str);
660665
} else {
661666
if !items_str.is_empty() {

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/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)