Skip to content

Commit 5bf08f0

Browse files
committed
Optimise common => {{ macro pattern
1 parent ab00c26 commit 5bf08f0

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
@@ -346,13 +344,23 @@ pub fn rewrite_macro_def(
346344
let old_body = context.snippet(branch.body).trim();
347345
let (body_str, substs) = replace_names(old_body);
348346

349-
// We'll hack the indent below, take this into account when formatting,
350347
let mut config = context.config.clone();
351-
let body_indent = mac_indent.block_indent(&config);
352-
let new_width = config.max_width() - body_indent.width();
353-
config.set().max_width(new_width);
354348
config.set().hide_parse_errors(true);
355349

350+
result += " {";
351+
352+
let has_block_body = old_body.starts_with("{");
353+
354+
let body_indent = if has_block_body {
355+
mac_indent
356+
} else {
357+
// We'll hack the indent below, take this into account when formatting,
358+
let body_indent = mac_indent.block_indent(&config);
359+
let new_width = config.max_width() - body_indent.width();
360+
config.set().max_width(new_width);
361+
body_indent
362+
};
363+
356364
// First try to format as items, then as statements.
357365
let new_body = match ::format_snippet(&body_str, &config) {
358366
Some(new_body) => new_body,
@@ -387,9 +395,14 @@ pub fn rewrite_macro_def(
387395
new_body = new_body.replace(new, old);
388396
}
389397

390-
result += &new_body;
398+
if has_block_body {
399+
result += new_body.trim();
400+
} else {
401+
result += "\n";
402+
result += &new_body;
403+
result += &mac_indent_str;
404+
}
391405

392-
result += &mac_indent_str;
393406
result += "}";
394407
if def.legacy {
395408
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)