diff --git a/README.md b/README.md index 20a5e997e629..8d454a86d80f 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,13 @@ Now you can run Clippy by invoking the following command: cargo clippy ``` +You can also test or build your project with Clippy lints as add-on with this command: + +```terminal +cargo clippy build +cargo clippy test +``` + #### Automatically applying Clippy suggestions Clippy can automatically apply some lint suggestions, just like the compiler. Note that `--fix` implies diff --git a/book/src/continuous_integration/README.md b/book/src/continuous_integration/README.md index e5c3673bde45..410db3a93a26 100644 --- a/book/src/continuous_integration/README.md +++ b/book/src/continuous_integration/README.md @@ -16,3 +16,11 @@ your crate for maximum compatibility. E.g. if your crate is compiled with the This chapter will give an overview on how to use Clippy on different popular CI providers. + +## CI Time optimization + +You can optimize time in your CI by running `cargo clippy test`, this will test your application at the same time as +it's linted. If Clippy doesn't report anything (or only has warnings), you will be saving precious CI time, as +there's no need to rebuild your application. + +So, compilation time is halved compared to running `cargo clippy` and THEN `cargo test`. diff --git a/book/src/usage.md b/book/src/usage.md index 23edf6c2abaa..3a414497c071 100644 --- a/book/src/usage.md +++ b/book/src/usage.md @@ -16,6 +16,16 @@ just run cargo clippy ``` +## Running build/test at the same time as Clippy + +You can also emit binaries and test your project with Clippy enabled! Reducing your compilation times as there's no +need to recompile. + +```bash +cargo clippy test +cargo clippy build +``` + ### Lint configuration The above command will run the default set of lints, which are included in the diff --git a/src/main.rs b/src/main.rs index 3c2eec1f05b9..da2ba2a33eb0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -67,7 +67,19 @@ impl ClippyCmd { for arg in old_args.by_ref() { match arg.as_str() { + "build" => { + cargo_subcommand = "build"; + continue; + }, + "test" => { + cargo_subcommand = "test"; + continue; + }, "--fix" => { + if cargo_subcommand != "check" { + eprintln!("Running `cargo clippy {cargo_subcommand}` is not available with the --fix flag"); + process::exit(1) // Same code as `--explain` encountering an unknown lint + } cargo_subcommand = "fix"; continue; }, @@ -154,15 +166,17 @@ pub fn help_message() -> &'static str { Usage: cargo clippy [OPTIONS] [--] [<>...] + cargo clippy test [OPTIONS] [--] [<>...] for running `test` with Clippy as add-on + cargo clippy build [OPTIONS] [--] [<>...] for running `build` with Clippy as add-on Common options: --no-deps Run Clippy only on the given crate, without linting the dependencies - --fix Automatically apply lint suggestions. This flag implies --no-deps and --all-targets + --fix Automatically apply lint suggestions. This flag implies --no-deps and --all-targets (not available on test/build) -h, --help Print this message -V, --version Print version info and exit --explain [LINT] Print the documentation for a given lint -See all options with cargo check --help. +See all options with cargo check --help or cargo build/test --help for the build or test subcommands Allowing / Denying lints @@ -213,9 +227,17 @@ mod tests { } #[test] - fn check() { + fn cargo_subcommand() { let args = "cargo clippy".split_whitespace().map(ToString::to_string); let cmd = ClippyCmd::new(args); assert_eq!("check", cmd.cargo_subcommand); + + let args = "cargo clippy build".split_whitespace().map(ToString::to_string); + let cmd = ClippyCmd::new(args); + assert_eq!("build", cmd.cargo_subcommand); + + let args = "cargo clippy test".split_whitespace().map(ToString::to_string); + let cmd = ClippyCmd::new(args); + assert_eq!("test", cmd.cargo_subcommand); } }