1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 // error-pattern:yummy
11 #![feature(box_syntax)]
12 #![feature(rustc_private)]
13 #![allow(clippy::missing_docs_in_private_items)]
15 use rustc_tools_util::*;
17 const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
20 cargo clippy [options] [--] [<opts>...]
23 -h, --help Print this message
24 -V, --version Print version info and exit
26 Other options are the same as `cargo check`.
28 To allow or deny a lint from the command line you can use `cargo clippy --`
31 -W --warn OPT Set lint warnings
32 -A --allow OPT Set lint allowed
33 -D --deny OPT Set lint denied
34 -F --forbid OPT Set lint forbidden
36 The feature `cargo-clippy` is automatically defined for convenience. You can use
37 it to allow or deny lints from the code, eg.:
39 #[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))]
43 println!("{}", CARGO_CLIPPY_HELP);
47 let version_info = rustc_tools_util::get_version_info!();
48 println!("{}", version_info);
52 // Check for version and help flags even when invoked as 'cargo-clippy'
53 if std::env::args().any(|a| a == "--help" || a == "-h") {
58 if std::env::args().any(|a| a == "--version" || a == "-V") {
63 if let Err(code) = process(std::env::args().skip(2)) {
64 std::process::exit(code);
68 fn process<I>(mut old_args: I) -> Result<(), i32>
70 I: Iterator<Item = String>,
72 let mut args = vec!["check".to_owned()];
74 let mut found_dashes = false;
75 for arg in old_args.by_ref() {
76 found_dashes |= arg == "--";
83 let clippy_args: String = old_args.map(|arg| format!("{}__CLIPPY_HACKERY__", arg)).collect();
85 let mut path = std::env::current_exe()
86 .expect("current executable path invalid")
87 .with_file_name("clippy-driver");
89 path.set_extension("exe");
92 let target_dir = std::env::var_os("CLIPPY_DOGFOOD")
94 std::env::var_os("CARGO_MANIFEST_DIR").map_or_else(
96 let mut fallback = std::ffi::OsString::new();
97 fallback.push("clippy_dogfood");
101 std::path::PathBuf::from(d)
108 .map(|p| ("CARGO_TARGET_DIR", p));
110 let exit_status = std::process::Command::new("cargo")
112 .env("RUSTC_WRAPPER", path)
113 .env("CLIPPY_ARGS", clippy_args)
116 .expect("could not run cargo")
118 .expect("failed to wait for cargo?");
120 if exit_status.success() {
123 Err(exit_status.code().unwrap_or(-1))