1 #![cfg_attr(feature = "deny-warnings", deny(warnings))]
3 use rustc_tools_util::VersionInfo;
5 const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
8 cargo clippy [options] [--] [<opts>...]
11 -h, --help Print this message
12 -V, --version Print version info and exit
14 Other options are the same as `cargo check`.
16 To allow or deny a lint from the command line you can use `cargo clippy --`
19 -W --warn OPT Set lint warnings
20 -A --allow OPT Set lint allowed
21 -D --deny OPT Set lint denied
22 -F --forbid OPT Set lint forbidden
24 You can use tool lints to allow or deny lints from your code, eg.:
26 #[allow(clippy::needless_lifetimes)]
30 println!("{}", CARGO_CLIPPY_HELP);
34 let version_info = rustc_tools_util::get_version_info!();
35 println!("{}", version_info);
39 // Check for version and help flags even when invoked as 'cargo-clippy'
40 if std::env::args().any(|a| a == "--help" || a == "-h") {
45 if std::env::args().any(|a| a == "--version" || a == "-V") {
50 if let Err(code) = process(std::env::args().skip(2)) {
51 std::process::exit(code);
55 fn process<I>(mut old_args: I) -> Result<(), i32>
57 I: Iterator<Item = String>,
59 let mut args = vec!["check".to_owned()];
62 let mut unstable_options = false;
64 for arg in old_args.by_ref() {
71 // Cover -Zunstable-options and -Z unstable-options
72 s if s.ends_with("unstable-options") => unstable_options = true,
80 if !unstable_options {
81 panic!("Usage of `--fix` requires `-Z unstable-options`");
83 args[0] = "fix".to_owned();
87 let path_env = if unstable_options {
88 "RUSTC_WORKSPACE_WRAPPER"
93 let clippy_args: String = old_args.map(|arg| format!("{}__CLIPPY_HACKERY__", arg)).collect();
95 let mut path = std::env::current_exe()
96 .expect("current executable path invalid")
97 .with_file_name("clippy-driver");
99 path.set_extension("exe");
102 let target_dir = std::env::var_os("CLIPPY_DOGFOOD")
104 std::env::var_os("CARGO_MANIFEST_DIR").map_or_else(
105 || std::ffi::OsString::from("clippy_dogfood"),
107 std::path::PathBuf::from(d)
114 .map(|p| ("CARGO_TARGET_DIR", p));
116 // Run the dogfood tests directly on nightly cargo. This is required due
117 // to a bug in rustup.rs when running cargo on custom toolchains. See issue #3118.
118 if std::env::var_os("CLIPPY_DOGFOOD").is_some() && cfg!(windows) {
119 args.insert(0, "+nightly".to_string());
122 let exit_status = std::process::Command::new("cargo")
125 .env("CLIPPY_ARGS", clippy_args)
128 .expect("could not run cargo")
130 .expect("failed to wait for cargo?");
132 if exit_status.success() {
135 Err(exit_status.code().unwrap_or(-1))