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::path::PathBuf;
8 use std::process::{self, Command};
10 const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
13 cargo clippy [options] [--] [<opts>...]
16 --no-deps Run Clippy only on the given crate, without linting the dependencies
17 --fix Automatically apply lint suggestions. This flag implies `--no-deps`
18 -h, --help Print this message
19 -V, --version Print version info and exit
21 Other options are the same as `cargo check`.
23 To allow or deny a lint from the command line you can use `cargo clippy --`
26 -W --warn OPT Set lint warnings
27 -A --allow OPT Set lint allowed
28 -D --deny OPT Set lint denied
29 -F --forbid OPT Set lint forbidden
31 You can use tool lints to allow or deny lints from your code, eg.:
33 #[allow(clippy::needless_lifetimes)]
37 println!("{}", CARGO_CLIPPY_HELP);
41 let version_info = rustc_tools_util::get_version_info!();
42 println!("{}", version_info);
46 // Check for version and help flags even when invoked as 'cargo-clippy'
47 if env::args().any(|a| a == "--help" || a == "-h") {
52 if env::args().any(|a| a == "--version" || a == "-V") {
57 if let Err(code) = process(env::args().skip(2)) {
63 cargo_subcommand: &'static str,
65 clippy_args: Vec<String>,
69 fn new<I>(mut old_args: I) -> Self
71 I: Iterator<Item = String>,
73 let mut cargo_subcommand = "check";
74 let mut args = vec![];
75 let mut clippy_args: Vec<String> = vec![];
77 for arg in old_args.by_ref() {
80 cargo_subcommand = "fix";
84 clippy_args.push("--no-deps".into());
94 clippy_args.append(&mut (old_args.collect()));
95 if cargo_subcommand == "fix" && !clippy_args.iter().any(|arg| arg == "--no-deps") {
96 clippy_args.push("--no-deps".into());
106 fn path() -> PathBuf {
107 let mut path = env::current_exe()
108 .expect("current executable path invalid")
109 .with_file_name("clippy-driver");
112 path.set_extension("exe");
118 fn into_std_cmd(self) -> Command {
119 let mut cmd = Command::new("cargo");
120 let clippy_args: String = self
123 .map(|arg| format!("{}__CLIPPY_HACKERY__", arg))
126 // Currently, `CLIPPY_TERMINAL_WIDTH` is used only to format "unknown field" error messages.
127 let terminal_width = termize::dimensions().map_or(0, |(w, _)| w);
129 cmd.env("RUSTC_WORKSPACE_WRAPPER", Self::path())
130 .env("CLIPPY_ARGS", clippy_args)
131 .env("CLIPPY_TERMINAL_WIDTH", terminal_width.to_string())
132 .arg(self.cargo_subcommand)
139 fn process<I>(old_args: I) -> Result<(), i32>
141 I: Iterator<Item = String>,
143 let cmd = ClippyCmd::new(old_args);
145 let mut cmd = cmd.into_std_cmd();
147 let exit_status = cmd
149 .expect("could not run cargo")
151 .expect("failed to wait for cargo?");
153 if exit_status.success() {
156 Err(exit_status.code().unwrap_or(-1))
162 use super::ClippyCmd;
166 let args = "cargo clippy --fix".split_whitespace().map(ToString::to_string);
167 let cmd = ClippyCmd::new(args);
168 assert_eq!("fix", cmd.cargo_subcommand);
169 assert!(!cmd.args.iter().any(|arg| arg.ends_with("unstable-options")));
173 fn fix_implies_no_deps() {
174 let args = "cargo clippy --fix".split_whitespace().map(ToString::to_string);
175 let cmd = ClippyCmd::new(args);
176 assert!(cmd.clippy_args.iter().any(|arg| arg == "--no-deps"));
180 fn no_deps_not_duplicated_with_fix() {
181 let args = "cargo clippy --fix -- --no-deps"
183 .map(ToString::to_string);
184 let cmd = ClippyCmd::new(args);
185 assert_eq!(cmd.clippy_args.iter().filter(|arg| *arg == "--no-deps").count(), 1);
190 let args = "cargo clippy".split_whitespace().map(ToString::to_string);
191 let cmd = ClippyCmd::new(args);
192 assert_eq!("check", cmd.cargo_subcommand);