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.
11 // error-pattern:yummy
12 #![feature(box_syntax)]
13 #![feature(rustc_private)]
14 #![feature(tool_lints)]
15 #![allow(unknown_lints, clippy::missing_docs_in_private_items)]
17 use rustc_tools_util::*;
19 const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
22 cargo clippy [options] [--] [<opts>...]
25 -h, --help Print this message
26 -V, --version Print version info and exit
28 Other options are the same as `cargo check`.
30 To allow or deny a lint from the command line you can use `cargo clippy --`
33 -W --warn OPT Set lint warnings
34 -A --allow OPT Set lint allowed
35 -D --deny OPT Set lint denied
36 -F --forbid OPT Set lint forbidden
38 The feature `cargo-clippy` is automatically defined for convenience. You can use
39 it to allow or deny lints from the code, eg.:
41 #[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))]
44 #[allow(clippy::print_stdout)]
46 println!("{}", CARGO_CLIPPY_HELP);
49 #[allow(clippy::print_stdout)]
51 let version_info = rustc_tools_util::get_version_info!();
52 println!("{}", version_info);
56 // Check for version and help flags even when invoked as 'cargo-clippy'
57 if std::env::args().any(|a| a == "--help" || a == "-h") {
62 if std::env::args().any(|a| a == "--version" || a == "-V") {
67 if let Err(code) = process(std::env::args().skip(2)) {
68 std::process::exit(code);
72 fn process<I>(mut old_args: I) -> Result<(), i32>
74 I: Iterator<Item = String>,
76 let mut args = vec!["check".to_owned()];
78 let mut found_dashes = false;
79 for arg in old_args.by_ref() {
80 found_dashes |= arg == "--";
87 let clippy_args: String = old_args.map(|arg| format!("{}__CLIPPY_HACKERY__", arg)).collect();
89 let mut path = std::env::current_exe()
90 .expect("current executable path invalid")
91 .with_file_name("clippy-driver");
93 path.set_extension("exe");
96 let target_dir = std::env::var_os("CLIPPY_DOGFOOD")
98 std::env::var_os("CARGO_MANIFEST_DIR").map_or_else(
100 let mut fallback = std::ffi::OsString::new();
101 fallback.push("clippy_dogfood");
105 std::path::PathBuf::from(d)
111 }).map(|p| ("CARGO_TARGET_DIR", p));
113 let exit_status = std::process::Command::new("cargo")
115 .env("RUSTC_WRAPPER", path)
116 .env("CLIPPY_ARGS", clippy_args)
119 .expect("could not run cargo")
121 .expect("failed to wait for cargo?");
123 if exit_status.success() {
126 Err(exit_status.code().unwrap_or(-1))