1 #![cfg_attr(feature = "deny-warnings", deny(warnings))]
3 use rustc_tools_util::VersionInfo;
5 use std::path::PathBuf;
6 use std::process::{self, Command};
7 use std::ffi::OsString;
9 const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
12 cargo clippy [options] [--] [<opts>...]
15 -h, --help Print this message
16 -V, --version Print version info and exit
18 Other options are the same as `cargo check`.
20 To allow or deny a lint from the command line you can use `cargo clippy --`
23 -W --warn OPT Set lint warnings
24 -A --allow OPT Set lint allowed
25 -D --deny OPT Set lint denied
26 -F --forbid OPT Set lint forbidden
28 You can use tool lints to allow or deny lints from your code, eg.:
30 #[allow(clippy::needless_lifetimes)]
34 println!("{}", CARGO_CLIPPY_HELP);
38 let version_info = rustc_tools_util::get_version_info!();
39 println!("{}", version_info);
43 // Check for version and help flags even when invoked as 'cargo-clippy'
44 if env::args().any(|a| a == "--help" || a == "-h") {
49 if env::args().any(|a| a == "--version" || a == "-V") {
54 if let Err(code) = process(env::args().skip(2)) {
60 unstable_options: bool,
68 fn new<I>(mut old_args: I) -> Self
70 I: Iterator<Item = String>,
72 let mut cmd = "check";
73 let mut unstable_options = false;
74 let mut args = vec![];
76 for arg in old_args.by_ref() {
83 // Cover -Zunstable-options and -Z unstable-options
84 s if s.ends_with("unstable-options") => unstable_options = true,
91 if cmd == "fix" && !unstable_options {
92 panic!("Usage of `--fix` requires `-Z unstable-options`");
95 // Run the dogfood tests directly on nightly cargo. This is required due
96 // to a bug in rustup.rs when running cargo on custom toolchains. See issue #3118.
97 if env::var_os("CLIPPY_DOGFOOD").is_some() && cfg!(windows) {
98 args.insert(0, "+nightly".to_string());
101 let clippy_args: String =
103 .map(|arg| format!("{}__CLIPPY_HACKERY__", arg))
114 fn path_env(&self) -> &'static str {
115 if self.unstable_options {
116 "RUSTC_WORKSPACE_WRAPPER"
122 fn path(&self) -> PathBuf {
123 let mut path = env::current_exe()
124 .expect("current executable path invalid")
125 .with_file_name("clippy-driver");
128 path.set_extension("exe");
134 fn target_dir() -> Option<(&'static str, OsString)> {
135 env::var_os("CLIPPY_DOGFOOD")
137 env::var_os("CARGO_MANIFEST_DIR").map_or_else(
138 || std::ffi::OsString::from("clippy_dogfood"),
140 std::path::PathBuf::from(d)
147 .map(|p| ("CARGO_TARGET_DIR", p))
150 fn to_std_cmd(self) -> Command {
151 let mut cmd = Command::new("cargo");
153 cmd.env(self.path_env(), self.path())
154 .envs(ClippyCmd::target_dir())
155 .env("CLIPPY_ARGS", self.clippy_args)
164 fn process<I>(old_args: I) -> Result<(), i32>
166 I: Iterator<Item = String>,
168 let cmd = ClippyCmd::new(old_args);
170 let mut cmd = cmd.to_std_cmd();
172 let exit_status = cmd
174 .expect("could not run cargo")
176 .expect("failed to wait for cargo?");
178 if exit_status.success() {
181 Err(exit_status.code().unwrap_or(-1))