-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Description
This issue started its life in clippy (rust-lang/rust-clippy#1313). Once it was realized this should apply to built-in lints as well, it migrated to rust (rust-lang/rust#45832). There @kennytm suggested that it should be Cargo's responsibility to pass the -D
/-W
/-A
flags to rustc.
Motivation
Currently project-wide lint configuration needs to be included in the crate sources. This is okay when the project consists of a single crate, but gets more cumbersome when the project is a workspace with dozen crates.
Being able to define the lints in an external file that will be used when building the crates would have several benefits:
- Sharing one configuration file for all the crates in a workspace.
- A canonical place to check for lint configuration when contributing to a new project.
- An easy way to examine the version history for lint changes.
Proposal
- A configuration
lints.toml
(or[lints]
inCargo.toml
?) file that specifies the lints. - The lints can be specified on the workspace level and for individual packages. Anything on the package level will override the workspace setup on per lint basis.
- The configuration file is cfg/feature-aware and can specify different lints depending on features. Specifying clippy-lints will result in clippy complaining about unknown lints if clippy isn't used.
Also... unlike what @oli-obk suggested in the rust issue, personally I'd prefer the following format:
[lints]
allow_unused = "allow"
non_snake_case = "allow"
While this is more verbose than allow = [ "allow_unused", "non_snake_case" ]
, I think the diff benefits and the ability for the user to group logically similar lints together even if some of them are deny
and some allow
outweighs the increased verbosity.
Also if Cargo/rustc ever support lint configurations, this would be more future proof:
cyclomatic_complexity = { state = "allow", threshold = 30 }
Example configuration
[lints]
allow_unused = "deny"
non_snake_case = "allow"
[lints.'cfg(feature = "clippy")']
cyclomatic_compexity = "deny"
The feature syntax is lifted from Cargo's dependency syntax - at least that was the intention. Personally I'm not a fan of it, but I guess that's a limitation of toml. Don't really have a better proposal for it, given I don't really know what's available int he toml syntax. :<
Activity
Mark-Simulacrum commentedon Feb 12, 2018
I am strongly in favor. We should also probably specify that unknown lints should be a warning, not an error, for the purposes of backwards compatibility.
Rantanen commentedon Feb 13, 2018
Forgot to mention: I'm also interested in contributing code for the implementation, if there is consensus on what should happen. Also do Cargo changes, such as this, go through the RFC process?
Mark-Simulacrum commentedon Feb 13, 2018
This seems potentially worthy of an RFC, but I could see it going either way. There doesn't seem to be much design space. @alexcrichton can probably say more.
alexcrichton commentedon Feb 13, 2018
This seems plausible to me! I think it'd be fine to add this to
Cargo.toml
on an unstable basis (behind a nightly feature) and we can decide to stabilize at a later date.lukaslueg commentedon Feb 14, 2018
Do we have a chance for
rustc
to report where the linter settings are coming from? Given the increasing amount of knobs and switches, it would be very nice to haverustc
report thatallow_unused
was denied and thatdeny
was specified bythe workspace's root Cargo.toml
orthe commandline
.As
rustc
knows nothing about these, we could introduce a new switch torustc
that specifies the source of the following linter settings (e.g.--LS "/home/myproject/foobar/Cargo.toml" -D ... --LS "/home/myproject/Cargo.toml" -D ...
).Is this a thing?
oli-obk commentedon Feb 14, 2018
I'm not sure if these should be in the Cargo.toml, as we many want to be able to specify them globally for a group of projects.
oli-obk commentedon Jul 6, 2018
rustc already reports whether something was
so we just add another variant for config file there.
phansch commentedon Jul 6, 2018
I'm really interested in this, too.
It could be
cyclomatic_complexity = { state = "allow" }
from the start, then we would have the option to add further options in a second step.phansch commentedon Jul 11, 2018
This Rollup PR from rust demonstrates how rust itself could benefit from a shared lint config, too: rust-lang/rust#52268 (note all the 'deny bare trait object' PRs; and there's a few more)
detrumi commentedon Jul 12, 2018
I'd like to start working on this, unless @phansch or @Rantanen started on this already.
Seems like the
lints.toml
name is the one I've seen used most, so let's go with that.As for the syntax,
allow_unused = "allow"
seems reasonable and more ergonomic than requiring thecyclomatic_complexity = { state = "allow" }
syntax.A
Cargo.toml
file can already be used for a workspace, unless you meant a group of workspaces?Mark-Simulacrum commentedon Jul 12, 2018
I'd prefer that we centralize this into Cargo.toml rather than adding an additional file, but that can likely be decided in further detail at a later point in time.
134 remaining items
ParkMyCar commentedon Feb 4, 2023
Hey @flip1995, you mentioned you started on an RFC? I'd be more than happy to help, or pickup the work if you've become too busy?
uninlined_format_args
scylladb/scylla-rust-driver#643flip1995 commentedon Feb 6, 2023
Yes, I started one some time ago, but can't find it anymore. 🤦 There is this attempt at an RFC: https://hackmd.io/@flip1995/HJteAAPgO But it is really sparse and not at a point where I would continue with it, but rather start fresh.
emilk commentedon Feb 6, 2023
For anyone looking for a workaround for this issue, I have been using
cargo cranky
for the past six months, and I'm really liking it: https://github.com/ericseppanen/cargo-crankyManishearth commentedon Feb 6, 2023
I'm also wondering if at this point clippy should just support it in clippy.toml.
We can then do fancy stuff like "disable lints that were introduced in version foo" (or "enable all lints up to version foo").
Ideally this can be done with an additional state for Rust's lint emission UI where it mentions that the lint level comes from a file instead.
epage commentedon Feb 15, 2023
I've created an RFC for this: rust-lang/rfcs#3389
[lints]
table toCargo.toml
rust-lang/rfcs#3389[lints]
table RFC 3389 #12115epage commentedon Sep 26, 2023
Since #12115 is merged, I'm closing this as resolved.
.cargo/config.toml
toCargo.toml
ruffle-rs/ruffle#14614