Skip to content

Commit f8a74f1

Browse files
committed
Document cargo-clippy feature
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 ##10220 — Ability to skip files or blocks entirely
1 parent 23ea47b commit f8a74f1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

book/src/configuration.md

+21
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ interested in:
7373
cargo clippy -- -A clippy::all -W clippy::useless_format -W clippy::...
7474
```
7575

76+
### Disabling evaluation of certain code
77+
78+
Rarely, you may wish to prevent Clippy from evaluating certain sections of code entirely. Clippy sets the `cargo-clippy`
79+
feature when it compiles your code, so you can use [conditional
80+
compilation](https://doc.rust-lang.org/reference/conditional-compilation.html) to prevent files or blocks from being
81+
parsed at all. Note that since the code is not evaluated you cannot turn evaluation or lints back on within the code.
82+
Also, if the code is referenced from elsewhere in your crate, you may need to provide a stub so that the code compiles:
83+
84+
```rust
85+
#[cfg(not(feature = "cargo-clippy"))]
86+
include!(concat!(env!("OUT_DIR"), "/my_big_function-generated.rs"));
87+
88+
#[cfg(feature = "cargo-clippy")]
89+
fn my_big_function(_input: &str) -> Option<MyStruct> {
90+
None
91+
}
92+
```
93+
94+
This feature is not actually part of your crate, so specifying `--all-features` to other tools, e.g. `cargo test
95+
--all-features`, will not disable it.
96+
7697
### Specifying the minimum supported Rust version
7798

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

0 commit comments

Comments
 (0)