Skip to content

Commit

Permalink
Use stderr for various errors and warnings.
Browse files Browse the repository at this point in the history
Adjusts several error and warning report cases to output using eprintln!
instead of println! so that messages are sent to stderr.
  • Loading branch information
SingingTree committed Oct 25, 2017
1 parent 560b054 commit adac9fb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
34 changes: 22 additions & 12 deletions src/bin/cargo-fmt.rs
Expand Up @@ -55,14 +55,14 @@ fn execute() -> i32 {
.take_while(|a| a != "--")
.find(|a| !a.starts_with('-'))
{
print_usage(&opts, &format!("Invalid argument: `{}`.", arg));
print_usage_to_stderr(&opts, &format!("Invalid argument: `{}`.", arg));
return failure;
}

let matches = match opts.parse(env::args().skip(1).take_while(|a| a != "--")) {
Ok(m) => m,
Err(e) => {
print_usage(&opts, &e.to_string());
print_usage_to_stderr(&opts, &e.to_string());
return failure;
}
};
Expand All @@ -72,21 +72,21 @@ fn execute() -> i32 {
(false, true) => Verbosity::Quiet,
(true, false) => Verbosity::Verbose,
(true, true) => {
print_usage(&opts, "quiet mode and verbose mode are not compatible");
print_usage_to_stderr(&opts, "quiet mode and verbose mode are not compatible");
return failure;
}
};

if matches.opt_present("h") {
print_usage(&opts, "");
print_usage_to_stdout(&opts, "");
return success;
}

let workspace_hitlist = WorkspaceHitlist::from_matches(&matches);

match format_crate(verbosity, &workspace_hitlist) {
Err(e) => {
print_usage(&opts, &e.to_string());
print_usage_to_stderr(&opts, &e.to_string());
failure
}
Ok(status) => if status.success() {
Expand All @@ -97,13 +97,23 @@ fn execute() -> i32 {
}
}

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

fn print_usage_to_stdout(opts: &Options, reason: &str) {
print_usage!(println, opts, reason);
}

fn print_usage_to_stderr(opts: &Options, reason: &str) {
print_usage!(eprintln, opts, reason);
}

#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down
36 changes: 23 additions & 13 deletions src/bin/rustfmt.rs
Expand Up @@ -161,7 +161,7 @@ fn execute(opts: &Options) -> FmtResult<Summary> {

match determine_operation(&matches)? {
Operation::Help => {
print_usage(opts, "");
print_usage_to_stdout(opts, "");
Summary::print_exit_codes();
Ok(Summary::default())
}
Expand Down Expand Up @@ -196,7 +196,7 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
config.set().file_lines(file_lines.parse()?);
for f in config.file_lines().files() {
if f != "stdin" {
println!("Warning: Extra file listed in file_lines option '{}'", f);
eprintln!("Warning: Extra file listed in file_lines option '{}'", f);
}
}
}
Expand All @@ -212,7 +212,7 @@ fn execute(opts: &Options) -> FmtResult<Summary> {

for f in options.file_lines.files() {
if !files.contains(&PathBuf::from(f)) {
println!("Warning: Extra file listed in file_lines option '{}'", f);
eprintln!("Warning: Extra file listed in file_lines option '{}'", f);
}
}

Expand All @@ -231,10 +231,10 @@ fn execute(opts: &Options) -> FmtResult<Summary> {
let mut error_summary = Summary::default();
for file in files {
if !file.exists() {
println!("Error: file `{}` does not exist", file.to_str().unwrap());
eprintln!("Error: file `{}` does not exist", file.to_str().unwrap());
error_summary.add_operational_error();
} else if file.is_dir() {
println!("Error: `{}` is a directory", file.to_str().unwrap());
eprintln!("Error: `{}` is a directory", file.to_str().unwrap());
error_summary.add_operational_error();
} else {
// Check the file directory if the config-path could not be read or not provided
Expand Down Expand Up @@ -293,7 +293,7 @@ fn main() {
}
}
Err(e) => {
print_usage(&opts, &e.to_string());
print_usage_to_stderr(&opts, &e.to_string());
1
}
};
Expand All @@ -307,13 +307,23 @@ fn main() {
std::process::exit(exit_code);
}

fn print_usage(opts: &Options, reason: &str) {
let reason = format!(
"{}\n\nusage: {} [options] <file>...",
reason,
env::args_os().next().unwrap().to_string_lossy()
);
println!("{}", opts.usage(&reason));
macro_rules! print_usage {
($print:ident, $opts:ident, $reason:expr) => ({
let msg = format!(
"{}\n\nusage: {} [options] <file>...",
$reason,
env::args_os().next().unwrap().to_string_lossy()
);
$print!("{}", $opts.usage(&msg));
})
}

fn print_usage_to_stdout(opts: &Options, reason: &str) {
print_usage!(println, opts, reason);
}

fn print_usage_to_stderr(opts: &Options, reason: &str) {
print_usage!(eprintln, opts, reason);
}

fn print_version() {
Expand Down
2 changes: 1 addition & 1 deletion src/visitor.rs
Expand Up @@ -601,7 +601,7 @@ impl<'a> FmtVisitor<'a> {
match self.codemap.span_to_snippet(span) {
Ok(s) => s,
Err(_) => {
println!(
eprintln!(
"Couldn't make snippet for span {:?}->{:?}",
self.codemap.lookup_char_pos(span.lo()),
self.codemap.lookup_char_pos(span.hi())
Expand Down

0 comments on commit adac9fb

Please sign in to comment.