Skip to content

Commit 128133e

Browse files
committed
Fix issue #3805 - wrap macro line with width of one char beyond max
1 parent ef91154 commit 128133e

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

src/macros.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,14 @@ impl MacroBranch {
12191219
}
12201220

12211221
// 5 = " => {"
1222-
let mut result = format_macro_args(context, self.args.clone(), shape.sub_width(5)?)?;
1222+
let old_body = context.snippet(self.body).trim();
1223+
let prefix_width = 5 + if context.config.format_macro_bodies() {
1224+
old_body.find(|ch| ch != '{').unwrap_or(old_body.len()) // " => {{..."
1225+
} else {
1226+
0
1227+
};
1228+
let mut result =
1229+
format_macro_args(context, self.args.clone(), shape.sub_width(prefix_width)?)?;
12231230

12241231
if multi_branch_style {
12251232
result += " =>";
@@ -1237,7 +1244,6 @@ impl MacroBranch {
12371244
// `$$`). We'll try and format like an AST node, but we'll substitute
12381245
// variables for new names with the same length first.
12391246

1240-
let old_body = context.snippet(self.body).trim();
12411247
let (body_str, substs) = replace_names(old_body)?;
12421248
let has_block_body = old_body.starts_with('{');
12431249

tests/source/issue-3805.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// rustfmt-format_macro_matchers: true
2+
3+
// From original issue example - Line length 101
4+
macro_rules! test {
5+
($aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeio:expr, $add:expr) => {{
6+
return;
7+
}};
8+
}
9+
10+
// Line length 102
11+
macro_rules! test {
12+
($aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeiou:expr, $add:expr) => {{
13+
return;
14+
}};
15+
}
16+
17+
// Line length 103
18+
macro_rules! test {
19+
($aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeioua:expr, $add:expr) => {{
20+
return;
21+
}};
22+
}
23+
24+
// With extended macro body - Line length 101
25+
macro_rules! test {
26+
($aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeio:expr, $add:expr) => {{
27+
let VAR = "VALUE"; return VAR;
28+
}};
29+
}
30+
31+
// With extended macro body - Line length 102
32+
macro_rules! test {
33+
($aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeiou:expr, $add:expr) => {{
34+
let VAR = "VALUE"; return VAR;
35+
}};
36+
}
37+
38+
// With extended macro body - Line length 103
39+
macro_rules! test {
40+
($aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeioua:expr, $add:expr) => {{
41+
let VAR = "VALUE"; return VAR;
42+
}};
43+
}

tests/target/issue-3805.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// rustfmt-format_macro_matchers: true
2+
3+
// From original issue example - Line length 101
4+
macro_rules! test {
5+
(
6+
$aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeio:expr, $add:expr
7+
) => {{
8+
return;
9+
}};
10+
}
11+
12+
// Line length 102
13+
macro_rules! test {
14+
(
15+
$aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeiou:expr, $add:expr
16+
) => {{
17+
return;
18+
}};
19+
}
20+
21+
// Line length 103
22+
macro_rules! test {
23+
(
24+
$aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeioua:expr, $add:expr
25+
) => {{
26+
return;
27+
}};
28+
}
29+
30+
// With extended macro body - Line length 101
31+
macro_rules! test {
32+
(
33+
$aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeio:expr, $add:expr
34+
) => {{
35+
let VAR = "VALUE";
36+
return VAR;
37+
}};
38+
}
39+
40+
// With extended macro body - Line length 102
41+
macro_rules! test {
42+
(
43+
$aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeiou:expr, $add:expr
44+
) => {{
45+
let VAR = "VALUE";
46+
return VAR;
47+
}};
48+
}
49+
50+
// With extended macro body - Line length 103
51+
macro_rules! test {
52+
(
53+
$aasdfghj:expr, $qwertyuiop:expr, $zxcvbnmasdfghjkl:expr, $aeiouaeiouaeioua:expr, $add:expr
54+
) => {{
55+
let VAR = "VALUE";
56+
return VAR;
57+
}};
58+
}

0 commit comments

Comments
 (0)