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 --fix Automatically apply lint suggestions
18 --no-deps Run Clippy only on the given crate
19 -h, --help Print this message
20 -V, --version Print version info and exit
22 Other options are the same as `cargo check`.
24 To allow or deny a lint from the command line you can use `cargo clippy --`
27 -W --warn OPT Set lint warnings
28 -A --allow OPT Set lint allowed
29 -D --deny OPT Set lint denied
30 -F --forbid OPT Set lint forbidden
32 You can use tool lints to allow or deny lints from your code, eg.:
34 #[allow(clippy::needless_lifetimes)]
38 println!("{}", CARGO_CLIPPY_HELP);
42 let version_info = rustc_tools_util::get_version_info!();
43 println!("{}", version_info);
47 // Check for version and help flags even when invoked as 'cargo-clippy'
48 if env::args().any(|a| a == "--help" || a == "-h") {
53 if env::args().any(|a| a == "--version" || a == "-V") {
58 if let Err(code) = process(env::args().skip(2)) {
64 cargo_subcommand: &'static str,
66 clippy_args: Vec<String>,
70 fn new<I>(mut old_args: I) -> Self
72 I: Iterator<Item = String>,
74 let mut cargo_subcommand = "check";
75 let mut args = vec![];
77 for arg in old_args.by_ref() {
80 cargo_subcommand = "fix";
90 let mut clippy_args: Vec<String> = old_args.collect();
91 if cargo_subcommand == "fix" && !clippy_args.iter().any(|arg| arg == "--no-deps") {
92 clippy_args.push("--no-deps".into());
102 fn path() -> PathBuf {
103 let mut path = env::current_exe()
104 .expect("current executable path invalid")
105 .with_file_name("clippy-driver");
108 path.set_extension("exe");
114 fn target_dir() -> Option<(&'static str, OsString)> {
115 env::var_os("CLIPPY_DOGFOOD")
117 env::var_os("CARGO_MANIFEST_DIR").map_or_else(
118 || std::ffi::OsString::from("clippy_dogfood"),
120 std::path::PathBuf::from(d)
127 .map(|p| ("CARGO_TARGET_DIR", p))
130 fn into_std_cmd(self) -> Command {
131 let mut cmd = Command::new("cargo");
132 let clippy_args: String = self
135 .map(|arg| format!("{}__CLIPPY_HACKERY__", arg))
138 cmd.env("RUSTC_WORKSPACE_WRAPPER", Self::path())
139 .envs(ClippyCmd::target_dir())
140 .env("CLIPPY_ARGS", clippy_args)
141 .arg(self.cargo_subcommand)
148 fn process<I>(old_args: I) -> Result<(), i32>
150 I: Iterator<Item = String>,
152 let cmd = ClippyCmd::new(old_args);
154 let mut cmd = cmd.into_std_cmd();
156 let exit_status = cmd
158 .expect("could not run cargo")
160 .expect("failed to wait for cargo?");
162 if exit_status.success() {
165 Err(exit_status.code().unwrap_or(-1))
171 use super::ClippyCmd;
175 let args = "cargo clippy --fix".split_whitespace().map(ToString::to_string);
176 let cmd = ClippyCmd::new(args);
177 assert_eq!("fix", cmd.cargo_subcommand);
178 assert!(!cmd.args.iter().any(|arg| arg.ends_with("unstable-options")));
182 fn fix_implies_no_deps() {
183 let args = "cargo clippy --fix".split_whitespace().map(ToString::to_string);
184 let cmd = ClippyCmd::new(args);
185 assert!(cmd.clippy_args.iter().any(|arg| arg == "--no-deps"));
189 fn no_deps_not_duplicated_with_fix() {
190 let args = "cargo clippy --fix -- --no-deps"
192 .map(ToString::to_string);
193 let cmd = ClippyCmd::new(args);
194 assert_eq!(cmd.clippy_args.iter().filter(|arg| *arg == "--no-deps").count(), 1);
199 let args = "cargo clippy".split_whitespace().map(ToString::to_string);
200 let cmd = ClippyCmd::new(args);
201 assert_eq!("check", cmd.cargo_subcommand);