Merge pull request #3298 from topecongiro/issue-3272

Use the same rule between function and macro
This commit is contained in:
Seiichi Uchida 2019-01-30 00:36:55 +09:00 committed by GitHub
commit 923da60f72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 124 additions and 22 deletions

View file

@ -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() {

View file

@ -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(),
);
}

View file

@ -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(),
);
}

View file

@ -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!(

View file

@ -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]);
};
}

View file

@ -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]);
};
}

View file

@ -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(),
);
}

View file

@ -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(),
);
}

View file

@ -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!(

View file

@ -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]);
};
}

View file

@ -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
]);
};
}