|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Clippy: Deprecating `feature = \"cargo-clippy\"`" |
| 4 | +author: The Clippy Team |
| 5 | +release: true |
| 6 | +--- |
| 7 | + |
| 8 | +Since Clippy [`v0.0.97`] and before it was shipped with `rustup`, Clippy |
| 9 | +implicitly added a `feature = "cargo-clippy"` config[^1] when linting your code |
| 10 | +with `cargo clippy`. |
| 11 | + |
| 12 | +[^1]: It's likely that you didn't even know that Clippy implicitly sets this |
| 13 | + config (which was not a Cargo feature). This is intentional, as we stopped |
| 14 | + advertising and documenting this a long time ago. |
| 15 | + |
| 16 | +Back in the day (2016) this was necessary to allow, warn or deny Clippy lints |
| 17 | +using attributes: |
| 18 | + |
| 19 | +```rust |
| 20 | +#[cfg_attr(feature = "cargo-clippy", allow(clippy_lint_name))] |
| 21 | +``` |
| 22 | + |
| 23 | +Doing this hasn't been necessary for a long time. Today, Clippy users will set |
| 24 | +lint levels with tool lint attributes using the `clippy::` prefix: |
| 25 | + |
| 26 | +```rust |
| 27 | +#[allow(clippy::lint_name)] |
| 28 | +``` |
| 29 | + |
| 30 | +The implicit `feature = "cargo-clippy"` has only been kept for backwards |
| 31 | +compatibility, but will now be deprecated. |
| 32 | + |
| 33 | +## Alternative |
| 34 | + |
| 35 | +As there is a rare [use case] for conditional compilation depending on Clippy, |
| 36 | +we will provide an alternative. So in the future you will be able to use: |
| 37 | + |
| 38 | +```rust |
| 39 | +#[cfg(clippy)] |
| 40 | +``` |
| 41 | + |
| 42 | +## Transitioning |
| 43 | + |
| 44 | +Should you have instances of `feature = "cargo-clippy"` in your code base, you |
| 45 | +will see a warning from the new Clippy lint |
| 46 | +[`clippy::deprecated_clippy_cfg_attr`][pr-12292]. This lint can automatically fix |
| 47 | +your code. So if you should see this lint triggering, just run: |
| 48 | + |
| 49 | +``` |
| 50 | +cargo clippy --fix -- -Aclippy::all -Wclippy::deprecated_clippy_cfg_attr |
| 51 | +``` |
| 52 | + |
| 53 | +This will fix all instances in your code. |
| 54 | + |
| 55 | +In addition, check your `.cargo/config` file for: |
| 56 | + |
| 57 | +```toml |
| 58 | +[target.'cfg(feature = "cargo-clippy")'] |
| 59 | +rustflags = ["-Aclippy::..."] |
| 60 | +``` |
| 61 | + |
| 62 | +If you have this config, you will have to update it yourself, by either changing |
| 63 | +it to `cfg(clippy)` or taking this opportunity to transition to [setting lint |
| 64 | +levels in `Cargo.toml`][cargo-lints] directly. |
| 65 | + |
| 66 | +## Motivation for Deprecation |
| 67 | + |
| 68 | +Currently, there's a [call for testing], in order to stabilize [checking |
| 69 | +conditional compilation at compile time][rfc-3013], aka `cargo check |
| 70 | +-Zcheck-cfg`. If we were to keep the `feature = "cargo-clippy"` config, users |
| 71 | +would start seeing a lot of warnings on their `feature = "cargo-clippy"` |
| 72 | +conditions. To work around this, they would either need to allow the lint or |
| 73 | +have to add a dummy feature to their `Cargo.toml` in order to silence those |
| 74 | +warnings: |
| 75 | + |
| 76 | +```toml |
| 77 | +[features] |
| 78 | +cargo-clippy = [] |
| 79 | +``` |
| 80 | + |
| 81 | +We didn't think this would be user friendly, and decided that instead we want to |
| 82 | +deprecate the implicit `feature = "cargo-clippy"` config and replace it with the |
| 83 | +`clippy` config. |
| 84 | + |
| 85 | +[`v0.0.97`]: https://github.com/rust-lang/rust-clippy/blob/61daf674eaf17f3b504c51f01b4ee63fac47dfcf/CHANGELOG.md?plain=0#0097--2016-11-03 |
| 86 | +[rfc-3013]: https://github.com/rust-lang/rfcs/pull/3013 |
| 87 | +[use case]: https://doc.rust-lang.org/clippy/configuration.html#disabling-evaluation-of-certain-code |
| 88 | +[pr-12292]: https://github.com/rust-lang/rust-clippy/pull/12292 |
| 89 | +[cargo-lints]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-lints-section |
| 90 | +[call for testing]: https://github.com/rust-lang/rfcs/pull/3013#issuecomment-1936648479 |
0 commit comments