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 args = vec![];
75 for arg in old_args.by_ref() {
78 cargo_subcommand = "fix";
88 let mut clippy_args: Vec<String> = old_args.collect();
89 if cargo_subcommand == "fix" && !clippy_args.iter().any(|arg| arg == "--no-deps") {
90 clippy_args.push("--no-deps".into());
100 fn path() -> PathBuf {
101 let mut path = env::current_exe()
102 .expect("current executable path invalid")
103 .with_file_name("clippy-driver");
106 path.set_extension("exe");
112 fn target_dir() -> Option<(&'static str, OsString)> {
113 env::var_os("CLIPPY_DOGFOOD")
115 env::var_os("CARGO_MANIFEST_DIR").map_or_else(
116 || std::ffi::OsString::from("clippy_dogfood"),
118 std::path::PathBuf::from(d)
125 .map(|p| ("CARGO_TARGET_DIR", p))
128 fn into_std_cmd(self) -> Command {
129 let mut cmd = Command::new("cargo");
130 let clippy_args: String = self
133 .map(|arg| format!("{}__CLIPPY_HACKERY__", arg))
136 cmd.env("RUSTC_WORKSPACE_WRAPPER", Self::path())
137 .envs(ClippyCmd::target_dir())
138 .env("CLIPPY_ARGS", clippy_args)
139 .arg(self.cargo_subcommand)
146 fn process<I>(old_args: I) -> Result<(), i32>
148 I: Iterator<Item = String>,
150 let cmd = ClippyCmd::new(old_args);
152 let mut cmd = cmd.into_std_cmd();
154 let exit_status = cmd
156 .expect("could not run cargo")
158 .expect("failed to wait for cargo?");
160 if exit_status.success() {
163 Err(exit_status.code().unwrap_or(-1))
169 use super::ClippyCmd;
173 let args = "cargo clippy --fix".split_whitespace().map(ToString::to_string);
174 let cmd = ClippyCmd::new(args);
175 assert_eq!("fix", cmd.cargo_subcommand);
176 assert!(!cmd.args.iter().any(|arg| arg.ends_with("unstable-options")));
180 fn fix_implies_no_deps() {
181 let args = "cargo clippy --fix".split_whitespace().map(ToString::to_string);
182 let cmd = ClippyCmd::new(args);
183 assert!(cmd.clippy_args.iter().any(|arg| arg == "--no-deps"));
187 fn no_deps_not_duplicated_with_fix() {
188 let args = "cargo clippy --fix -- --no-deps"
190 .map(ToString::to_string);
191 let cmd = ClippyCmd::new(args);
192 assert_eq!(cmd.clippy_args.iter().filter(|arg| *arg == "--no-deps").count(), 1);
197 let args = "cargo clippy".split_whitespace().map(ToString::to_string);
198 let cmd = ClippyCmd::new(args);
199 assert_eq!("check", cmd.cargo_subcommand);