Skip to content

Commit 418a612

Browse files
authored
Merge pull request #2440 from topecongiro/issue-2439
Disable formatting macro-def if args do not fit on one line
2 parents 9cbda90 + b49b307 commit 418a612

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

rustfmt-core/src/macros.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,8 @@ fn replace_names(input: &str) -> Option<(String, HashMap<String, String>)> {
431431
// and `(`/`)` have special meaning.
432432
//
433433
// We always try and format on one line.
434-
fn format_macro_args(toks: ThinTokenStream) -> Option<String> {
434+
// FIXME: Use multi-line when every thing does not fit on one line.
435+
fn format_macro_args(toks: ThinTokenStream, shape: Shape) -> Option<String> {
435436
let mut result = String::with_capacity(128);
436437
let mut insert_space = SpaceState::Never;
437438

@@ -459,7 +460,7 @@ fn format_macro_args(toks: ThinTokenStream) -> Option<String> {
459460
if let SpaceState::Always = insert_space {
460461
result.push(' ');
461462
}
462-
let formatted = format_macro_args(d.tts)?;
463+
let formatted = format_macro_args(d.tts, shape)?;
463464
match d.delim {
464465
DelimToken::Paren => {
465466
result.push_str(&format!("({})", formatted));
@@ -482,7 +483,11 @@ fn format_macro_args(toks: ThinTokenStream) -> Option<String> {
482483
}
483484
}
484485

485-
Some(result)
486+
if result.len() <= shape.width {
487+
Some(result)
488+
} else {
489+
None
490+
}
486491
}
487492

488493
// We should insert a space if the next token is a:
@@ -617,7 +622,7 @@ fn macro_style(mac: &ast::Mac, context: &RewriteContext) -> MacroStyle {
617622
/// a,
618623
/// b,
619624
/// c,
620-
// ),
625+
/// ),
621626
/// }
622627
/// ```
623628
fn indent_macro_snippet(
@@ -757,7 +762,8 @@ impl MacroBranch {
757762
return None;
758763
}
759764

760-
let mut result = format_macro_args(self.args.clone())?;
765+
// 5 = " => {"
766+
let mut result = format_macro_args(self.args.clone(), shape.sub_width(5)?)?;
761767

762768
if multi_branch_style {
763769
result += " =>";
@@ -847,7 +853,12 @@ mod test {
847853
&ParseSess::new(FilePathMapping::empty()),
848854
None,
849855
);
850-
format_macro_args(input.into()).unwrap()
856+
let shape = Shape {
857+
width: 100,
858+
indent: Indent::empty(),
859+
offset: 0,
860+
};
861+
format_macro_args(input.into(), shape).unwrap()
851862
}
852863

853864
#[test]

rustfmt-core/tests/source/macro_rules.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,13 @@ macro m2 {
4949
mod macro_item { struct $item ; }
5050
}
5151
}
52+
53+
// #2439
54+
macro_rules! m {
55+
(
56+
$line0_xxxxxxxxxxxxxxxxx: expr,
57+
$line1_xxxxxxxxxxxxxxxxx: expr,
58+
$line2_xxxxxxxxxxxxxxxxx: expr,
59+
$line3_xxxxxxxxxxxxxxxxx: expr,
60+
) => {};
61+
}

rustfmt-core/tests/target/macro_rules.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,13 @@ macro m2 {
4141
}
4242
}
4343
}
44+
45+
// #2439
46+
macro_rules! m {
47+
(
48+
$line0_xxxxxxxxxxxxxxxxx: expr,
49+
$line1_xxxxxxxxxxxxxxxxx: expr,
50+
$line2_xxxxxxxxxxxxxxxxx: expr,
51+
$line3_xxxxxxxxxxxxxxxxx: expr,
52+
) => {};
53+
}

0 commit comments

Comments
 (0)