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)]
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 --no-deps Run Clippy only on the given crate, without linting the dependencies
16 --fix Automatically apply lint suggestions. This flag implies `--no-deps`
17 -h, --help Print this message
18 -V, --version Print version info and exit
19 --explain LINT Print the documentation for a given lint
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 Some(pos) = env::args().position(|a| a == "--explain") {
58 if let Some(mut lint) = env::args().nth(pos + 1) {
59 lint.make_ascii_lowercase();
60 clippy_lints::explain(&lint.strip_prefix("clippy::").unwrap_or(&lint).replace('-', "_"));
67 if let Err(code) = process(env::args().skip(2)) {
73 cargo_subcommand: &'static str,
75 clippy_args: Vec<String>,
79 fn new<I>(mut old_args: I) -> Self
81 I: Iterator<Item = String>,
83 let mut cargo_subcommand = "check";
84 let mut args = vec![];
85 let mut clippy_args: Vec<String> = vec![];
87 for arg in old_args.by_ref() {
90 cargo_subcommand = "fix";
94 clippy_args.push("--no-deps".into());
104 clippy_args.append(&mut (old_args.collect()));
105 if cargo_subcommand == "fix" && !clippy_args.iter().any(|arg| arg == "--no-deps") {
106 clippy_args.push("--no-deps".into());
116 fn path() -> PathBuf {
117 let mut path = env::current_exe()
118 .expect("current executable path invalid")
119 .with_file_name("clippy-driver");
122 path.set_extension("exe");
128 fn into_std_cmd(self) -> Command {
129 let mut cmd = Command::new("cargo");
130 let clippy_args: String = self
133 .map(|arg| format!("{arg}__CLIPPY_HACKERY__"))
136 // Currently, `CLIPPY_TERMINAL_WIDTH` is used only to format "unknown field" error messages.
137 let terminal_width = termize::dimensions().map_or(0, |(w, _)| w);
139 cmd.env("RUSTC_WORKSPACE_WRAPPER", Self::path())
140 .env("CLIPPY_ARGS", clippy_args)
141 .env("CLIPPY_TERMINAL_WIDTH", terminal_width.to_string())
142 .arg(self.cargo_subcommand)
149 fn process<I>(old_args: I) -> Result<(), i32>
151 I: Iterator<Item = String>,
153 let cmd = ClippyCmd::new(old_args);
155 let mut cmd = cmd.into_std_cmd();
157 let exit_status = cmd
159 .expect("could not run cargo")
161 .expect("failed to wait for cargo?");
163 if exit_status.success() {
166 Err(exit_status.code().unwrap_or(-1))
172 use super::ClippyCmd;
176 let args = "cargo clippy --fix".split_whitespace().map(ToString::to_string);
177 let cmd = ClippyCmd::new(args);
178 assert_eq!("fix", cmd.cargo_subcommand);
179 assert!(!cmd.args.iter().any(|arg| arg.ends_with("unstable-options")));
183 fn fix_implies_no_deps() {
184 let args = "cargo clippy --fix".split_whitespace().map(ToString::to_string);
185 let cmd = ClippyCmd::new(args);
186 assert!(cmd.clippy_args.iter().any(|arg| arg == "--no-deps"));
190 fn no_deps_not_duplicated_with_fix() {
191 let args = "cargo clippy --fix -- --no-deps"
193 .map(ToString::to_string);
194 let cmd = ClippyCmd::new(args);
195 assert_eq!(cmd.clippy_args.iter().filter(|arg| *arg == "--no-deps").count(), 1);
200 let args = "cargo clippy".split_whitespace().map(ToString::to_string);
201 let cmd = ClippyCmd::new(args);
202 assert_eq!("check", cmd.cargo_subcommand);