Skip to content

Commit 5bd036f

Browse files
committed
Optimise common => {{ macro pattern
1 parent 9fca907 commit 5bd036f

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

src/bin/cargo-fmt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ fn execute() -> i32 {
9797
}
9898

9999
macro_rules! print_usage {
100-
($print: ident, $opts: ident, $reason: expr) => ({
100+
($print: ident, $opts: ident, $reason: expr) => {{
101101
let msg = format!("{}\nusage: cargo fmt [options]", $reason);
102102
$print!(
103103
"{}\nThis utility formats all bin and lib files of the current crate using rustfmt. \
104104
Arguments after `--` are passed to rustfmt.",
105105
$opts.usage(&msg)
106106
);
107-
})
107+
}};
108108
}
109109

110110
fn print_usage_to_stdout(opts: &Options, reason: &str) {

src/macros.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,6 @@ pub fn rewrite_macro_def(
335335
result += &args;
336336
}
337337

338-
result += " {\n";
339-
340338
// The macro body is the most interesting part. It might end up as various
341339
// AST nodes, but also has special variables (e.g, `$foo`) which can't be
342340
// parsed as regular Rust code (and note that these can be escaped using
@@ -349,13 +347,23 @@ pub fn rewrite_macro_def(
349347
None => return snippet,
350348
};
351349

352-
// We'll hack the indent below, take this into account when formatting,
353350
let mut config = context.config.clone();
354-
let body_indent = mac_indent.block_indent(&config);
355-
let new_width = config.max_width() - body_indent.width();
356-
config.set().max_width(new_width);
357351
config.set().hide_parse_errors(true);
358352

353+
result += " {";
354+
355+
let has_block_body = old_body.starts_with("{");
356+
357+
let body_indent = if has_block_body {
358+
mac_indent
359+
} else {
360+
// We'll hack the indent below, take this into account when formatting,
361+
let body_indent = mac_indent.block_indent(&config);
362+
let new_width = config.max_width() - body_indent.width();
363+
config.set().max_width(new_width);
364+
body_indent
365+
};
366+
359367
// First try to format as items, then as statements.
360368
let new_body = match ::format_snippet(&body_str, &config) {
361369
Some(new_body) => new_body,
@@ -390,9 +398,14 @@ pub fn rewrite_macro_def(
390398
new_body = new_body.replace(new, old);
391399
}
392400

393-
result += &new_body;
401+
if has_block_body {
402+
result += new_body.trim();
403+
} else {
404+
result += "\n";
405+
result += &new_body;
406+
result += &mac_indent_str;
407+
}
394408

395-
result += &mac_indent_str;
396409
result += "}";
397410
if def.legacy {
398411
result += ";";

tests/target/macro_rules.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
macro_rules! m {
2-
($expr: expr, $func: ident) => {
3-
{
4-
let x = $expr;
5-
$func(x)
6-
}
7-
};
2+
($expr: expr, $func: ident) => {{
3+
let x = $expr;
4+
$func(x)
5+
}};
86

97
() => {
108
};

0 commit comments

Comments
 (0)