1 #![cfg_attr(feature = "deny-warnings", deny(warnings))]
3 use rustc_tools_util::VersionInfo;
5 use std::ffi::OsString;
6 use std::path::PathBuf;
7 use std::process::{self, Command};
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,
61 cargo_subcommand: &'static str,
67 fn new<I>(mut old_args: I) -> Self
69 I: Iterator<Item = String>,
71 let mut cargo_subcommand = "check";
72 let mut unstable_options = false;
73 let mut args = vec![];
75 for arg in old_args.by_ref() {
78 cargo_subcommand = "fix";
82 // Cover -Zunstable-options and -Z unstable-options
83 s if s.ends_with("unstable-options") => unstable_options = true,
90 if cargo_subcommand == "fix" && !unstable_options {
91 panic!("Usage of `--fix` requires `-Z unstable-options`");
94 // Run the dogfood tests directly on nightly cargo. This is required due
95 // to a bug in rustup.rs when running cargo on custom toolchains. See issue #3118.
96 if env::var_os("CLIPPY_DOGFOOD").is_some() && cfg!(windows) {
97 args.insert(0, "+nightly".to_string());
100 let clippy_args: String = old_args.map(|arg| format!("{}__CLIPPY_HACKERY__", arg)).collect();
110 fn path_env(&self) -> &'static str {
111 if self.unstable_options {
112 "RUSTC_WORKSPACE_WRAPPER"
118 fn path() -> PathBuf {
119 let mut path = env::current_exe()
120 .expect("current executable path invalid")
121 .with_file_name("clippy-driver");
124 path.set_extension("exe");
130 fn target_dir() -> Option<(&'static str, OsString)> {
131 env::var_os("CLIPPY_DOGFOOD")
133 env::var_os("CARGO_MANIFEST_DIR").map_or_else(
134 || std::ffi::OsString::from("clippy_dogfood"),
136 std::path::PathBuf::from(d)
143 .map(|p| ("CARGO_TARGET_DIR", p))
146 fn into_std_cmd(self) -> Command {
147 let mut cmd = Command::new("cargo");
149 cmd.env(self.path_env(), Self::path())
150 .envs(ClippyCmd::target_dir())
151 .env("CLIPPY_ARGS", self.clippy_args)
152 .arg(self.cargo_subcommand)
159 fn process<I>(old_args: I) -> Result<(), i32>
161 I: Iterator<Item = String>,
163 let cmd = ClippyCmd::new(old_args);
165 let mut cmd = cmd.into_std_cmd();
167 let exit_status = cmd
169 .expect("could not run cargo")
171 .expect("failed to wait for cargo?");
173 if exit_status.success() {
176 Err(exit_status.code().unwrap_or(-1))
182 use super::ClippyCmd;
186 fn fix_without_unstable() {
187 let args = "cargo clippy --fix".split_whitespace().map(ToString::to_string);
188 let _ = ClippyCmd::new(args);
193 let args = "cargo clippy --fix -Zunstable-options"
195 .map(ToString::to_string);
196 let cmd = ClippyCmd::new(args);
197 assert_eq!("fix", cmd.cargo_subcommand);
198 assert_eq!("RUSTC_WORKSPACE_WRAPPER", cmd.path_env());
199 assert!(cmd.args.iter().any(|arg| arg.ends_with("unstable-options")));
204 let args = "cargo clippy".split_whitespace().map(ToString::to_string);
205 let cmd = ClippyCmd::new(args);
206 assert_eq!("check", cmd.cargo_subcommand);
207 assert_eq!("RUSTC_WRAPPER", cmd.path_env());
211 fn check_unstable() {
212 let args = "cargo clippy -Zunstable-options"
214 .map(ToString::to_string);
215 let cmd = ClippyCmd::new(args);
216 assert_eq!("check", cmd.cargo_subcommand);
217 assert_eq!("RUSTC_WORKSPACE_WRAPPER", cmd.path_env());