Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions book/src/continuous_integration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
10 changes: 10 additions & 0 deletions book/src/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 25 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down Expand Up @@ -154,15 +166,17 @@ pub fn help_message() -> &'static str {

<green,bold>Usage</>:
<cyan,bold>cargo clippy</> <cyan>[OPTIONS] [--] [<<ARGS>>...]</>
<cyan,bold>cargo clippy test</> <cyan>[OPTIONS] [--] [<<ARGS>>...]</> for running `test` with Clippy as add-on
<cyan,bold>cargo clippy build</> <cyan>[OPTIONS] [--] [<<ARGS>>...]</> for running `build` with Clippy as add-on

<green,bold>Common options:</>
<cyan,bold>--no-deps</> Run Clippy only on the given crate, without linting the dependencies
<cyan,bold>--fix</> Automatically apply lint suggestions. This flag implies <cyan>--no-deps</> and <cyan>--all-targets</>
<cyan,bold>--fix</> Automatically apply lint suggestions. This flag implies <cyan>--no-deps</> and <cyan>--all-targets</> (not available on test/build)
<cyan,bold>-h</>, <cyan,bold>--help</> Print this message
<cyan,bold>-V</>, <cyan,bold>--version</> Print version info and exit
<cyan,bold>--explain [LINT]</> Print the documentation for a given lint

See all options with <cyan,bold>cargo check --help</>.
See all options with <cyan,bold>cargo check --help</> or <cyan,bold>cargo build/test --help</> for the build or test subcommands

<green,bold>Allowing / Denying lints</>

Expand Down Expand Up @@ -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);
}
}