Skip to content

Commit 30abfd6

Browse files
authored
Merge pull request #2274 from topecongiro/better-error-msg
Attempt to improve error meassage from rustfmt
2 parents 6ce082e + 17154c3 commit 30abfd6

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

src/bin/rustfmt.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ fn main() {
350350
}
351351
}
352352
Err(e) => {
353-
print_usage_to_stderr(&opts, &e.to_string());
353+
eprintln!("{}", e.to_string());
354354
1
355355
}
356356
};
@@ -364,23 +364,18 @@ fn main() {
364364
std::process::exit(exit_code);
365365
}
366366

367-
macro_rules! print_usage {
368-
($print:ident, $opts:ident, $reason:expr) => ({
369-
let msg = format!(
370-
"{}\n\nusage: {} [options] <file>...",
371-
$reason,
372-
env::args_os().next().unwrap().to_string_lossy()
373-
);
374-
$print!("{}", $opts.usage(&msg));
375-
})
376-
}
377-
378367
fn print_usage_to_stdout(opts: &Options, reason: &str) {
379-
print_usage!(println, opts, reason);
380-
}
381-
382-
fn print_usage_to_stderr(opts: &Options, reason: &str) {
383-
print_usage!(eprintln, opts, reason);
368+
let sep = if reason.is_empty() {
369+
String::new()
370+
} else {
371+
format!("{}\n\n", reason)
372+
};
373+
let msg = format!(
374+
"{}Format Rust code\n\nusage: {} [options] <file>...",
375+
sep,
376+
env::args_os().next().unwrap().to_string_lossy()
377+
);
378+
println!("{}", opts.usage(&msg));
384379
}
385380

386381
fn print_version() {

src/config.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -394,22 +394,23 @@ macro_rules! create_config {
394394
$(
395395
stringify!($i) => (),
396396
)+
397-
_ => {
398-
let msg =
399-
&format!("Warning: Unknown configuration option `{}`\n",
400-
key);
401-
err.push_str(msg)
402-
}
397+
_ => {
398+
let msg =
399+
&format!("Warning: Unknown configuration option `{}`\n", key);
400+
err.push_str(msg)
401+
}
403402
}
404403
}
405404
}
406405
match parsed.try_into() {
407-
Ok(parsed_config) =>
408-
Ok(Config::default().fill_from_parsed_config(parsed_config)),
406+
Ok(parsed_config) => {
407+
eprintln!("{}", err);
408+
Ok(Config::default().fill_from_parsed_config(parsed_config))
409+
}
409410
Err(e) => {
410411
err.push_str("Error: Decoding config file failed:\n");
411412
err.push_str(format!("{}\n", e).as_str());
412-
err.push_str("Please check your config file.\n");
413+
err.push_str("Please check your config file.");
413414
Err(err)
414415
}
415416
}

0 commit comments

Comments
 (0)