rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
10.89k stars 1.46k forks source link

`[workspace.lints.clippy]` cannot override a category `"warn"` with a lint specific `"allow"` #12716

Closed Kriskras99 closed 2 weeks ago

Kriskras99 commented 2 weeks ago

Summary

I like to set categories as "warn" or "deny", and then allow specific lints. For example "warn" on cargo lints, but "allow" the multiple_crate_versions. Currently this does not work, and I have to add an override when running Clippy on the command line

Reproducer

I tried this code:

[workspace.lints.clippy]
cargo = "warn"
multiple_crate_versions = "allow"

I expected to see this happen: Warnings for cargo lints, but not for multiple_crate_versions

Instead, this happened:

warning: multiple versions for dependency `hashbrown`: 0.12.3, 0.14.3
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#multiple_crate_versions

warning: multiple versions for dependency `indexmap`: 1.9.3, 2.2.6
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#multiple_crate_versions

warning: multiple versions for dependency `regex-automata`: 0.1.10, 0.4.6
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#multiple_crate_versions

warning: multiple versions for dependency `regex-syntax`: 0.6.29, 0.8.3
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#multiple_crate_versions

warning: multiple versions for dependency `strsim`: 0.10.0, 0.11.1
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#multiple_crate_versions

Version

rustc 1.79.0-nightly (ef8b9dcf2 2024-04-24)
binary: rustc
commit-hash: ef8b9dcf23700f2e2265317611460d3a65c19eff
commit-date: 2024-04-24
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.4

Additional Labels

No response

y21 commented 2 weeks ago

This is more or less expected behavior with how lint tables are implemented in cargo currently. You need to give the lint group cargo a lower priority so that the specific lint multiple_crate_versions overrides it, e.g. cargo = { level = "warn", priority = -1 }. See also #12161 and #12093 for similar issues where this has caused confusion in the past.

Clippy does have a lint against this kind of mistake, but it does not work for workspaces.

error: lint group `cargo` has the same priority (0) as a lint
  --> Cargo.toml:16:1
   |
16 | cargo = "warn"
   | ^^^^^   ------ has an implicit priority of 0
17 | multiple_crate_versions = "allow"
   | ----------------------- has the same priority as this lint
   |
   = note: the order of the lints in the table is ignored by Cargo
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#lint_groups_priority
   = note: `#[deny(clippy::lint_groups_priority)]` on by default
help: to have lints override the group set `cargo` to a lower priority
   |
16 | cargo = { level = "warn", priority = -1 }
   |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Kriskras99 commented 2 weeks ago

Ah, that makes sense. Should I open an issue for lint_groups_priority not firing for workspaces? Or reuse this one?

Kriskras99 commented 2 weeks ago

Closing in favor of #12729