-// error-pattern:yummy
-#![feature(box_syntax)]
-#![feature(rustc_private)]
-#![allow(unknown_lints, missing_docs_in_private_items)]
+#![cfg_attr(feature = "deny-warnings", deny(warnings))]
+// warn on lints, that are included in `rust-lang/rust`s bootstrap
+#![warn(rust_2018_idioms, unused_lifetimes)]
-use std::collections::HashMap;
-use std::process;
-use std::io::{self, Write};
-
-extern crate cargo_metadata;
-
-use std::path::{Path, PathBuf};
+use rustc_tools_util::VersionInfo;
+use std::env;
+use std::ffi::OsString;
+use std::path::PathBuf;
+use std::process::{self, Command};
const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
Common options:
-h, --help Print this message
- --features Features to compile for the package
-V, --version Print version info and exit
- --all Run over all packages in the current workspace
-Other options are the same as `cargo rustc`.
+Other options are the same as `cargo check`.
To allow or deny a lint from the command line you can use `cargo clippy --`
with:
-D --deny OPT Set lint denied
-F --forbid OPT Set lint forbidden
-The feature `cargo-clippy` is automatically defined for convenience. You can use
-it to allow or deny lints from the code, eg.:
+You can use tool lints to allow or deny lints from your code, eg.:
- #[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))]
+ #[allow(clippy::needless_lifetimes)]
"#;
-#[allow(print_stdout)]
fn show_help() {
println!("{}", CARGO_CLIPPY_HELP);
}
-#[allow(print_stdout)]
fn show_version() {
- println!("{}", env!("CARGO_PKG_VERSION"));
+ let version_info = rustc_tools_util::get_version_info!();
+ println!("{}", version_info);
}
pub fn main() {
// Check for version and help flags even when invoked as 'cargo-clippy'
- if std::env::args().any(|a| a == "--help" || a == "-h") {
+ if env::args().any(|a| a == "--help" || a == "-h") {
show_help();
return;
}
- if std::env::args().any(|a| a == "--version" || a == "-V") {
+
+ if env::args().any(|a| a == "--version" || a == "-V") {
show_version();
return;
}
- let mut manifest_path_arg = std::env::args()
- .skip(2)
- .skip_while(|val| !val.starts_with("--manifest-path"));
- let manifest_path_arg = manifest_path_arg.next().and_then(|val| {
- if val == "--manifest-path" {
- manifest_path_arg.next()
- } else if val.starts_with("--manifest-path=") {
- Some(val["--manifest-path=".len()..].to_owned())
- } else {
- None
- }
- });
+ if let Err(code) = process(env::args().skip(2)) {
+ process::exit(code);
+ }
+}
- let mut metadata = if let Ok(metadata) = cargo_metadata::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref)) {
- metadata
- } else {
- println!(
- "{:?}",
- cargo_metadata::metadata(manifest_path_arg.as_ref().map(AsRef::as_ref))
- );
- let _ = io::stderr().write_fmt(format_args!("error: Could not obtain cargo metadata.\n"));
- process::exit(101);
- };
-
- let manifest_path = manifest_path_arg.map(|arg| {
- PathBuf::from(arg)
- .canonicalize()
- .expect("manifest path could not be canonicalized")
- });
-
- let packages = if std::env::args().any(|a| a == "--all") {
- metadata.packages
- } else {
- let package_index = {
- if let Some(manifest_path) = manifest_path {
- metadata.packages.iter().position(|package| {
- let package_manifest_path = Path::new(&package.manifest_path)
- .canonicalize()
- .expect("package manifest path could not be canonicalized");
- package_manifest_path == manifest_path
- })
- } else {
- let package_manifest_paths: HashMap<_, _> = metadata
- .packages
- .iter()
- .enumerate()
- .map(|(i, package)| {
- let package_manifest_path = Path::new(&package.manifest_path)
- .parent()
- .expect("could not find parent directory of package manifest")
- .canonicalize()
- .expect("package directory cannot be canonicalized");
- (package_manifest_path, i)
- })
- .collect();
-
- let current_dir = std::env::current_dir()
- .expect("CARGO_MANIFEST_DIR not set")
- .canonicalize()
- .expect("manifest directory cannot be canonicalized");
-
- let mut current_path: &Path = ¤t_dir;
-
- // This gets the most-recent parent (the one that takes the fewest `cd ..`s to
- // reach).
- loop {
- if let Some(&package_index) = package_manifest_paths.get(current_path) {
- break Some(package_index);
- } else {
- // We'll never reach the filesystem root, because to get to this point in the
- // code
- // the call to `cargo_metadata::metadata` must have succeeded. So it's okay to
- // unwrap the current path's parent.
- current_path = current_path
- .parent()
- .unwrap_or_else(|| panic!("could not find parent of path {}", current_path.display()));
- }
- }
- }
- }.expect("could not find matching package");
-
- vec![metadata.packages.remove(package_index)]
- };
-
- for package in packages {
- let manifest_path = package.manifest_path;
-
- for target in package.targets {
- let args = std::env::args()
- .skip(2)
- .filter(|a| a != "--all" && !a.starts_with("--manifest-path="));
-
- let args = std::iter::once(format!("--manifest-path={}", manifest_path)).chain(args);
- if let Some(first) = target.kind.get(0) {
- if target.kind.len() > 1 || first.ends_with("lib") {
- println!("lib: {}", target.name);
- if let Err(code) = process(std::iter::once("--lib".to_owned()).chain(args)) {
- std::process::exit(code);
- }
- } else if ["bin", "example", "test", "bench"].contains(&&**first) {
- println!("{}: {}", first, target.name);
- if let Err(code) = process(
- vec![format!("--{}", first), target.name]
- .into_iter()
- .chain(args),
- ) {
- std::process::exit(code);
- }
- }
- } else {
- panic!("badly formatted cargo metadata: target::kind is an empty array");
+struct ClippyCmd {
+ cargo_subcommand: &'static str,
+ args: Vec<String>,
+ clippy_args: Vec<String>,
+}
+
+impl ClippyCmd {
+ fn new<I>(mut old_args: I) -> Self
+ where
+ I: Iterator<Item = String>,
+ {
+ let mut cargo_subcommand = "check";
+ let mut args = vec![];
+
+ for arg in old_args.by_ref() {
+ match arg.as_str() {
+ "--fix" => {
+ cargo_subcommand = "fix";
+ continue;
+ },
+ "--" => break,
+ _ => {},
}
+
+ args.push(arg);
+ }
+
+ let mut clippy_args: Vec<String> = old_args.collect();
+ if cargo_subcommand == "fix" && !clippy_args.iter().any(|arg| arg == "--no-deps") {
+ clippy_args.push("--no-deps".into());
+ }
+
+ ClippyCmd {
+ cargo_subcommand,
+ args,
+ clippy_args,
}
}
-}
-fn process<I>(mut old_args: I) -> Result<(), i32>
-where
- I: Iterator<Item = String>,
-{
- let mut args = vec!["check".to_owned()];
+ fn path() -> PathBuf {
+ let mut path = env::current_exe()
+ .expect("current executable path invalid")
+ .with_file_name("clippy-driver");
- let mut found_dashes = false;
- for arg in old_args.by_ref() {
- found_dashes |= arg == "--";
- if found_dashes {
- break;
+ if cfg!(windows) {
+ path.set_extension("exe");
}
- args.push(arg);
+
+ path
}
- let clippy_args: String = old_args.map(|arg| format!("{}__CLIPPY_HACKERY__", arg)).collect();
+ fn target_dir() -> Option<(&'static str, OsString)> {
+ env::var_os("CLIPPY_DOGFOOD")
+ .map(|_| {
+ env::var_os("CARGO_MANIFEST_DIR").map_or_else(
+ || std::ffi::OsString::from("clippy_dogfood"),
+ |d| {
+ std::path::PathBuf::from(d)
+ .join("target")
+ .join("dogfood")
+ .into_os_string()
+ },
+ )
+ })
+ .map(|p| ("CARGO_TARGET_DIR", p))
+ }
- let mut path = std::env::current_exe()
- .expect("current executable path invalid")
- .with_file_name("clippy-driver");
- if cfg!(windows) {
- path.set_extension("exe");
+ fn into_std_cmd(self) -> Command {
+ let mut cmd = Command::new("cargo");
+ let clippy_args: String = self
+ .clippy_args
+ .iter()
+ .map(|arg| format!("{}__CLIPPY_HACKERY__", arg))
+ .collect();
+
+ cmd.env("RUSTC_WORKSPACE_WRAPPER", Self::path())
+ .envs(ClippyCmd::target_dir())
+ .env("CLIPPY_ARGS", clippy_args)
+ .arg(self.cargo_subcommand)
+ .args(&self.args);
+
+ cmd
}
- let exit_status = std::process::Command::new("cargo")
- .args(&args)
- .env("RUSTC_WRAPPER", path)
- .env("CLIPPY_ARGS", clippy_args)
+}
+
+fn process<I>(old_args: I) -> Result<(), i32>
+where
+ I: Iterator<Item = String>,
+{
+ let cmd = ClippyCmd::new(old_args);
+
+ let mut cmd = cmd.into_std_cmd();
+
+ let exit_status = cmd
.spawn()
.expect("could not run cargo")
.wait()
Err(exit_status.code().unwrap_or(-1))
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::ClippyCmd;
+
+ #[test]
+ fn fix() {
+ let args = "cargo clippy --fix".split_whitespace().map(ToString::to_string);
+ let cmd = ClippyCmd::new(args);
+ assert_eq!("fix", cmd.cargo_subcommand);
+ assert!(!cmd.args.iter().any(|arg| arg.ends_with("unstable-options")));
+ }
+
+ #[test]
+ fn fix_implies_no_deps() {
+ let args = "cargo clippy --fix".split_whitespace().map(ToString::to_string);
+ let cmd = ClippyCmd::new(args);
+ assert!(cmd.clippy_args.iter().any(|arg| arg == "--no-deps"));
+ }
+
+ #[test]
+ fn no_deps_not_duplicated_with_fix() {
+ let args = "cargo clippy --fix -- --no-deps"
+ .split_whitespace()
+ .map(ToString::to_string);
+ let cmd = ClippyCmd::new(args);
+ assert_eq!(cmd.clippy_args.iter().filter(|arg| *arg == "--no-deps").count(), 1);
+ }
+
+ #[test]
+ fn check() {
+ let args = "cargo clippy".split_whitespace().map(ToString::to_string);
+ let cmd = ClippyCmd::new(args);
+ assert_eq!("check", cmd.cargo_subcommand);
+ }
+}