1 #![cfg_attr(feature = "deny-warnings", deny(warnings))]
2 // warn on lints, that are included in `rust-lang/rust`s bootstrap
3 #![warn(rust_2018_idioms, unused_lifetimes)]
5 use rustc_tools_util::VersionInfo;
7 use std::ffi::OsString;
8 use std::path::PathBuf;
9 use std::process::{self, Command};
11 const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
14 cargo clippy [options] [--] [<opts>...]
17 -h, --help Print this message
18 -V, --version Print version info and exit
20 Other options are the same as `cargo check`.
22 To allow or deny a lint from the command line you can use `cargo clippy --`
25 -W --warn OPT Set lint warnings
26 -A --allow OPT Set lint allowed
27 -D --deny OPT Set lint denied
28 -F --forbid OPT Set lint forbidden
30 You can use tool lints to allow or deny lints from your code, eg.:
32 #[allow(clippy::needless_lifetimes)]
36 println!("{}", CARGO_CLIPPY_HELP);
40 let version_info = rustc_tools_util::get_version_info!();
41 println!("{}", version_info);
45 // Check for version and help flags even when invoked as 'cargo-clippy'
46 if env::args().any(|a| a == "--help" || a == "-h") {
51 if env::args().any(|a| a == "--version" || a == "-V") {
56 if let Err(code) = process(env::args().skip(2)) {
62 cargo_subcommand: &'static str,
64 clippy_args: Vec<String>,
68 fn new<I>(mut old_args: I) -> Self
70 I: Iterator<Item = String>,
72 let mut cargo_subcommand = "check";
73 let mut unstable_options = false;
74 let mut args = vec![];
76 for arg in old_args.by_ref() {
79 cargo_subcommand = "fix";
83 // Cover -Zunstable-options and -Z unstable-options
84 s if s.ends_with("unstable-options") => unstable_options = true,
91 if cargo_subcommand == "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 mut clippy_args: Vec<String> = old_args.collect();
102 if cargo_subcommand == "fix" && !clippy_args.iter().any(|arg| arg == "--no-deps") {
103 clippy_args.push("--no-deps".into());
113 fn path() -> PathBuf {
114 let mut path = env::current_exe()
115 .expect("current executable path invalid")
116 .with_file_name("clippy-driver");
119 path.set_extension("exe");
125 fn target_dir() -> Option<(&'static str, OsString)> {
126 env::var_os("CLIPPY_DOGFOOD")
128 env::var_os("CARGO_MANIFEST_DIR").map_or_else(
129 || std::ffi::OsString::from("clippy_dogfood"),
131 std::path::PathBuf::from(d)
138 .map(|p| ("CARGO_TARGET_DIR", p))
141 fn into_std_cmd(self) -> Command {
142 let mut cmd = Command::new("cargo");
143 let clippy_args: String = self
146 .map(|arg| format!("{}__CLIPPY_HACKERY__", arg))
149 cmd.env("RUSTC_WORKSPACE_WRAPPER", Self::path())
150 .envs(ClippyCmd::target_dir())
151 .env("CLIPPY_ARGS", 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 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!(cmd.args.iter().any(|arg| arg.ends_with("unstable-options")));
202 fn fix_implies_no_deps() {
203 let args = "cargo clippy --fix -Zunstable-options"
205 .map(ToString::to_string);
206 let cmd = ClippyCmd::new(args);
207 assert!(cmd.clippy_args.iter().any(|arg| arg == "--no-deps"));
211 fn no_deps_not_duplicated_with_fix() {
212 let args = "cargo clippy --fix -Zunstable-options -- --no-deps"
214 .map(ToString::to_string);
215 let cmd = ClippyCmd::new(args);
216 assert_eq!(cmd.clippy_args.iter().filter(|arg| *arg == "--no-deps").count(), 1);
221 let args = "cargo clippy".split_whitespace().map(ToString::to_string);
222 let cmd = ClippyCmd::new(args);
223 assert_eq!("check", cmd.cargo_subcommand);