Optimise common => {{ macro pattern

This commit is contained in:
Ingvar Stepanyan 2018-01-25 14:06:37 +00:00
parent 9fca9073d9
commit 5bd036fcac
3 changed files with 27 additions and 16 deletions

View file

@ -97,14 +97,14 @@ fn execute() -> i32 {
} }
macro_rules! print_usage { macro_rules! print_usage {
($print: ident, $opts: ident, $reason: expr) => ({ ($print: ident, $opts: ident, $reason: expr) => {{
let msg = format!("{}\nusage: cargo fmt [options]", $reason); let msg = format!("{}\nusage: cargo fmt [options]", $reason);
$print!( $print!(
"{}\nThis utility formats all bin and lib files of the current crate using rustfmt. \ "{}\nThis utility formats all bin and lib files of the current crate using rustfmt. \
Arguments after `--` are passed to rustfmt.", Arguments after `--` are passed to rustfmt.",
$opts.usage(&msg) $opts.usage(&msg)
); );
}) }};
} }
fn print_usage_to_stdout(opts: &Options, reason: &str) { fn print_usage_to_stdout(opts: &Options, reason: &str) {

View file

@ -335,8 +335,6 @@ pub fn rewrite_macro_def(
result += &args; result += &args;
} }
result += " {\n";
// The macro body is the most interesting part. It might end up as various // The macro body is the most interesting part. It might end up as various
// AST nodes, but also has special variables (e.g, `$foo`) which can't be // AST nodes, but also has special variables (e.g, `$foo`) which can't be
// parsed as regular Rust code (and note that these can be escaped using // parsed as regular Rust code (and note that these can be escaped using
@ -349,13 +347,23 @@ pub fn rewrite_macro_def(
None => return snippet, None => return snippet,
}; };
// We'll hack the indent below, take this into account when formatting,
let mut config = context.config.clone(); let mut config = context.config.clone();
let body_indent = mac_indent.block_indent(&config);
let new_width = config.max_width() - body_indent.width();
config.set().max_width(new_width);
config.set().hide_parse_errors(true); config.set().hide_parse_errors(true);
result += " {";
let has_block_body = old_body.starts_with("{");
let body_indent = if has_block_body {
mac_indent
} else {
// We'll hack the indent below, take this into account when formatting,
let body_indent = mac_indent.block_indent(&config);
let new_width = config.max_width() - body_indent.width();
config.set().max_width(new_width);
body_indent
};
// First try to format as items, then as statements. // First try to format as items, then as statements.
let new_body = match ::format_snippet(&body_str, &config) { let new_body = match ::format_snippet(&body_str, &config) {
Some(new_body) => new_body, Some(new_body) => new_body,
@ -390,9 +398,14 @@ pub fn rewrite_macro_def(
new_body = new_body.replace(new, old); new_body = new_body.replace(new, old);
} }
result += &new_body; if has_block_body {
result += new_body.trim();
} else {
result += "\n";
result += &new_body;
result += &mac_indent_str;
}
result += &mac_indent_str;
result += "}"; result += "}";
if def.legacy { if def.legacy {
result += ";"; result += ";";

View file

@ -1,10 +1,8 @@
macro_rules! m { macro_rules! m {
($expr: expr, $func: ident) => { ($expr: expr, $func: ident) => {{
{ let x = $expr;
let x = $expr; $func(x)
$func(x) }};
}
};
() => { () => {
}; };