Skip to content

Commit

Permalink
Document cargo-clippy feature
Browse files Browse the repository at this point in the history
It is possible to use conditional compilation to prevent Clippy from
evaluating certain code at all. Unfortunately, it was no longer
documented anywhere. This adds a brief explanation of how to use the
feature with conditional compilation, and mentions a few downsides.

Fixes #rust-lang#10220 — Ability to skip files or blocks entirely
  • Loading branch information
danielparks committed Jan 26, 2023
1 parent 23ea47b commit f8a74f1
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ interested in:
cargo clippy -- -A clippy::all -W clippy::useless_format -W clippy::...
```

### Disabling evaluation of certain code

Rarely, you may wish to prevent Clippy from evaluating certain sections of code entirely. Clippy sets the `cargo-clippy`
feature when it compiles your code, so you can use [conditional
compilation](https://doc.rust-lang.org/reference/conditional-compilation.html) to prevent files or blocks from being
parsed at all. Note that since the code is not evaluated you cannot turn evaluation or lints back on within the code.
Also, if the code is referenced from elsewhere in your crate, you may need to provide a stub so that the code compiles:

```rust
#[cfg(not(feature = "cargo-clippy"))]
include!(concat!(env!("OUT_DIR"), "/my_big_function-generated.rs"));

#[cfg(feature = "cargo-clippy")]
fn my_big_function(_input: &str) -> Option<MyStruct> {
None
}
```

This feature is not actually part of your crate, so specifying `--all-features` to other tools, e.g. `cargo test
--all-features`, will not disable it.

### Specifying the minimum supported Rust version

Projects that intend to support old versions of Rust can disable lints pertaining to newer features by specifying the
Expand Down

0 comments on commit f8a74f1

Please sign in to comment.