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

Commit bc5124e

Browse files
committed
Always enforce exactly one space between macro! and braces ({})
1 parent 1a3bc79 commit bc5124e

File tree

6 files changed

+26
-35
lines changed

6 files changed

+26
-35
lines changed

src/macros.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -256,19 +256,7 @@ pub fn rewrite_macro_inner(
256256
}
257257
DelimToken::Paren => Some(format!("{}()", macro_name)),
258258
DelimToken::Bracket => Some(format!("{}[]", macro_name)),
259-
DelimToken::Brace => {
260-
// Preserve at most one space before the braces.
261-
let char_after_bang = context
262-
.snippet(mac.span)
263-
.split('!')
264-
.nth(1)
265-
.and_then(|x| x.chars().next());
266-
if let Some(' ') = char_after_bang {
267-
Some(format!("{} {{}}", macro_name))
268-
} else {
269-
Some(format!("{}{{}}", macro_name))
270-
}
271-
}
259+
DelimToken::Brace => Some(format!("{} {{}}", macro_name)),
272260
_ => unreachable!(),
273261
};
274262
}
@@ -428,8 +416,15 @@ pub fn rewrite_macro_inner(
428416
}
429417
}
430418
DelimToken::Brace => {
431-
// Skip macro invocations with braces, for now.
432-
trim_left_preserve_layout(context.snippet(mac.span), shape.indent, &context.config)
419+
// For macro invocations with braces, always put a space between
420+
// the `macro_name!` and `{ /* macro_body */ }` but skip modifying
421+
// anything in between the braces (for now).
422+
let snippet = context.snippet(mac.span);
423+
let macro_raw = snippet.split_at(snippet.find('!')? + 1).1.trim_start();
424+
match trim_left_preserve_layout(macro_raw, &shape.indent, &context.config) {
425+
Some(macro_body) => Some(format!("{} {}", macro_name, macro_body)),
426+
None => Some(format!("{} {}", macro_name, macro_raw)),
427+
}
433428
}
434429
_ => unreachable!(),
435430
}

tests/source/macros.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ itemmacro!(this, is.now() .formatted(yay));
44

55
itemmacro!(really, long.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb() .is.formatted());
66

7-
itemmacro!{this, is.bracket().formatted()}
7+
itemmacro!{this, is.brace().formatted()}
88

99
peg_file! modname ("mygrammarfile.rustpeg");
1010

@@ -106,8 +106,6 @@ fn main() {
106106

107107
impl X {
108108
empty_invoc!{}
109-
110-
// Don't format empty either!
111109
empty_invoc! {}
112110
}
113111

tests/target/issue-2917/packed_simd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ macro_rules! impl_from_vector {
3131
}
3232
*/
3333

34-
test_if!{
34+
test_if! {
3535
$test_tt:
3636
interpolate_idents! {
3737
mod [$id _from_ $source] {

tests/target/lazy_static.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ lazy_static! {
1010
// We need to be able to format `lazy_static!` without known syntax.
1111
lazy_static!(xxx, yyyy, zzzzz);
1212

13-
lazy_static!{}
13+
lazy_static! {}
1414

1515
// #2354
1616
lazy_static! {

tests/target/macros.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ itemmacro!(
99
.formatted()
1010
);
1111

12-
itemmacro!{this, is.bracket().formatted()}
12+
itemmacro! {this, is.brace().formatted()}
1313

1414
peg_file! modname("mygrammarfile.rustpeg");
1515

@@ -94,7 +94,7 @@ fn main() {
9494

9595
foo(makro!(1, 3));
9696

97-
hamkaas!{ () };
97+
hamkaas! { () };
9898

9999
macrowithbraces! {dont, format, me}
100100

@@ -104,11 +104,11 @@ fn main() {
104104

105105
some_macro![];
106106

107-
some_macro!{
107+
some_macro! {
108108
// comment
109109
};
110110

111-
some_macro!{
111+
some_macro! {
112112
// comment
113113
};
114114

@@ -131,9 +131,7 @@ fn main() {
131131
}
132132

133133
impl X {
134-
empty_invoc!{}
135-
136-
// Don't format empty either!
134+
empty_invoc! {}
137135
empty_invoc! {}
138136
}
139137

@@ -952,7 +950,7 @@ macro_rules! m {
952950
};
953951
}
954952
fn foo() {
955-
f!{r#"
953+
f! {r#"
956954
test
957955
"#};
958956
}

tests/target/match.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,19 @@ fn issue355() {
204204
vec![3; 4]
205205
}
206206
// Funky bracketing styles
207-
t => println!{"a", b},
207+
t => println! {"a", b},
208208
u => vec![1, 2],
209209
v => vec![3; 4],
210210
w => println!["a", b],
211211
x => vec![1, 2],
212212
y => vec![3; 4],
213213
// Brackets with comments
214-
tc => println!{"a", b}, // comment
215-
uc => vec![1, 2], // comment
216-
vc => vec![3; 4], // comment
217-
wc => println!["a", b], // comment
218-
xc => vec![1, 2], // comment
219-
yc => vec![3; 4], // comment
214+
tc => println! {"a", b}, // comment
215+
uc => vec![1, 2], // comment
216+
vc => vec![3; 4], // comment
217+
wc => println!["a", b], // comment
218+
xc => vec![1, 2], // comment
219+
yc => vec![3; 4], // comment
220220
yd => looooooooooooooooooooooooooooooooooooooooooooooooooooooooong_func(
221221
aaaaaaaaaa, bbbbbbbbbb, cccccccccc, dddddddddd,
222222
),

0 commit comments

Comments
 (0)