From a181fd318b12d0648d126b81ed031d074c8471cf Mon Sep 17 00:00:00 2001 From: Alan Du Date: Mon, 3 Jun 2019 09:43:06 -0400 Subject: [PATCH] Implement cargo lint to run clippy --- .cargo/config | 3 +++ crates/tools/src/lib.rs | 28 ++++++++++++++++++++++++++++ crates/tools/src/main.rs | 4 +++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/.cargo/config b/.cargo/config index b12f407e6f..51ae33910b 100644 --- a/.cargo/config +++ b/.cargo/config @@ -12,6 +12,9 @@ install-code = "run --package tools --bin tools -- install-code" # Formats the full repository or installs the git hook to do it automatically. format = "run --package tools --bin tools -- format" format-hook = "run --package tools --bin tools -- format-hook" +# Run clippy +lint = "run --package tools --bin tools -- lint" + # Runs the fuzzing test suite (currently only parser) fuzz-tests = "run --package tools --bin tools -- fuzz-tests" diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs index 11b52ccb7f..92634655d2 100644 --- a/crates/tools/src/lib.rs +++ b/crates/tools/src/lib.rs @@ -133,6 +133,34 @@ pub fn install_format_hook() -> Result<()> { Ok(()) } +pub fn run_clippy() -> Result<()> { + match Command::new("rustup") + .args(&["run", TOOLCHAIN, "--", "cargo", "clippy", "--version"]) + .stderr(Stdio::null()) + .stdout(Stdio::null()) + .status() + { + Ok(status) if status.success() => (), + _ => install_clippy()?, + }; + + let allowed_lints = ["clippy::collapsible_if", "clippy::nonminimal_bool"]; + run( + &format!( + "rustup run {} -- cargo clippy --all-features --all-targets -- -A {}", + TOOLCHAIN, + allowed_lints.join(" -A ") + ), + ".", + )?; + Ok(()) +} + +pub fn install_clippy() -> Result<()> { + run(&format!("rustup install {}", TOOLCHAIN), ".")?; + run(&format!("rustup component add clippy --toolchain {}", TOOLCHAIN), ".") +} + pub fn run_fuzzer() -> Result<()> { match Command::new("cargo") .args(&["fuzz", "--help"]) diff --git a/crates/tools/src/main.rs b/crates/tools/src/main.rs index 0c33396857..cf189bf1c5 100644 --- a/crates/tools/src/main.rs +++ b/crates/tools/src/main.rs @@ -3,7 +3,7 @@ use core::str; use failure::bail; use tools::{ generate, gen_tests, install_format_hook, run, run_with_output, run_rustfmt, - Overwrite, Result, run_fuzzer, + Overwrite, Result, run_fuzzer, run_clippy, }; use std::{path::{PathBuf}, env}; @@ -16,6 +16,7 @@ fn main() -> Result<()> { .subcommand(SubCommand::with_name("format")) .subcommand(SubCommand::with_name("format-hook")) .subcommand(SubCommand::with_name("fuzz-tests")) + .subcommand(SubCommand::with_name("lint")) .get_matches(); match matches.subcommand_name().expect("Subcommand must be specified") { "install-code" => { @@ -28,6 +29,7 @@ fn main() -> Result<()> { "gen-syntax" => generate(Overwrite)?, "format" => run_rustfmt(Overwrite)?, "format-hook" => install_format_hook()?, + "lint" => run_clippy()?, "fuzz-tests" => run_fuzzer()?, _ => unreachable!(), }