Skip to content

Commit e5a5a95

Browse files
author
Machtan
committed
Add '--version' flag and use version and help flags when called as 'cargo-clippy'
1 parent 3800bff commit e5a5a95

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/main.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ use rustc_driver::{driver, CompilerCalls, RustcDefaultCalls, Compilation};
1717
use rustc::session::{config, Session};
1818
use rustc::session::config::{Input, ErrorOutputType};
1919
use std::path::PathBuf;
20-
use std::process::Command;
20+
use std::process::{self, Command};
2121
use syntax::ast;
22+
use std::io::{self, Write};
2223

2324
use clippy_lints::utils::cargo;
2425

@@ -141,6 +142,12 @@ fn show_help() {
141142
println!("{}", CARGO_CLIPPY_HELP);
142143
}
143144

145+
#[allow(print_stdout)]
146+
fn show_version() {
147+
println!("{}", env!("CARGO_PKG_VERSION"));
148+
}
149+
150+
#[cfg_attr(feature = "cargo-clippy", allow(print_stdout))]
144151
pub fn main() {
145152
use std::env;
146153

@@ -154,7 +161,7 @@ pub fn main() {
154161
return;
155162
}
156163
if std::env::args().any(|a| a == "--version" || a == "-V") {
157-
println!("{}", env!("CARGO_PKG_VERSION"));
164+
show_version();
158165
return;
159166
}
160167

@@ -165,7 +172,12 @@ pub fn main() {
165172

166173
let manifest_path_arg = std::env::args().skip(2).find(|val| val.starts_with("--manifest-path="));
167174

168-
let mut metadata = cargo::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref)).expect("could not obtain cargo metadata");
175+
let mut metadata = if let Ok(metadata) = cargo::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref)) {
176+
metadata
177+
} else {
178+
let _ = io::stderr().write_fmt(format_args!("error: Could not obtain cargo metadata."));
179+
process::exit(101);
180+
};
169181

170182
assert_eq!(metadata.version, 1);
171183

0 commit comments

Comments
 (0)