Skip to content

Support defining enabled and disabled lints in a configuration file #5034

@Rantanen

Description

@Rantanen

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] in Cargo.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

Mark-Simulacrum commented on Feb 12, 2018

@Mark-Simulacrum
Member

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

Rantanen commented on Feb 13, 2018

@Rantanen
Author

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

Mark-Simulacrum commented on Feb 13, 2018

@Mark-Simulacrum
Member

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

alexcrichton commented on Feb 13, 2018

@alexcrichton
Member

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

lukaslueg commented on Feb 14, 2018

@lukaslueg
Contributor

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 have rustc report that allow_unused was denied and that deny was specified by the workspace's root Cargo.toml or the commandline.

As rustc knows nothing about these, we could introduce a new switch to rustc 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

oli-obk commented on Feb 14, 2018

@oli-obk
Contributor

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

oli-obk commented on Jul 6, 2018

@oli-obk
Contributor

Is this a thing?

rustc already reports whether something was

  • defined by command line
  • defined by default settings
  • defined by code

so we just add another variant for config file there.

phansch

phansch commented on Jul 6, 2018

@phansch
Member

I'm really interested in this, too.

Also if Cargo/rustc ever support lint configurations, this would be more future proof:
cyclomatic_complexity = { state = "allow", threshold = 30 }

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

phansch commented on Jul 11, 2018

@phansch
Member

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

detrumi commented on Jul 12, 2018

@detrumi
Member

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 the cyclomatic_complexity = { state = "allow" } syntax.

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.

A Cargo.toml file can already be used for a workspace, unless you meant a group of workspaces?

Mark-Simulacrum

Mark-Simulacrum commented on Jul 12, 2018

@Mark-Simulacrum
Member

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

ParkMyCar commented on Feb 4, 2023

@ParkMyCar

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?

flip1995

flip1995 commented on Feb 6, 2023

@flip1995
Member

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

emilk commented on Feb 6, 2023

@emilk

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-cranky

Manishearth

Manishearth commented on Feb 6, 2023

@Manishearth
Member

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

epage commented on Feb 15, 2023

@epage
Contributor

I've created an RFC for this: rust-lang/rfcs#3389

epage

epage commented on Sep 26, 2023

@epage
Contributor

Since #12115 is merged, I'm closing this as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: rustc lint configuration

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @wagenet@ehuss@epage@alexcrichton@Nemo157

        Issue actions

          Support defining enabled and disabled lints in a configuration file · Issue #5034 · rust-lang/cargo