|
| 1 | +# Clippy.toml stabilization |
| 2 | + |
| 3 | +- Start Date: 2025-09-07 |
| 4 | + |
| 5 | +**Progress** |
| 6 | +- [ ] Deprecating `CLIPPY_CONF_DIR` and creating `CLIPPY_CONF_PATH` with an improved algorithm |
| 7 | +- [ ] Create the `unstable-conf` key and allow for unknown values. |
| 8 | +- \[0/88\] Options stabilized |
| 9 | + |
| 10 | +# Summary |
| 11 | + |
| 12 | +Stabilization of the Clippy configuration file and other ways to change Clippy’s behaviour via values passed to the |
| 13 | +program at runtime. |
| 14 | + |
| 15 | +This RFC proposes a change to the algorithm that `CLIPPY_CONF_DIR` uses, a deprecation of `CLIPPY_CONF_DIR` in lieu of |
| 16 | +`CLIPPY_CONF_PATH` (allowing for custom-named Clippy configuration files), allowing for several configuration files |
| 17 | +when on a workspace. |
| 18 | + |
| 19 | +# Motivation |
| 20 | + |
| 21 | +High-stakes projects need a guarantee of stabilization in every aspect of their toolchain. Including configuration |
| 22 | +options and default values. One of these high-stakes projects is Rust-for-Linux, which has communicated with the |
| 23 | +Clippy team before about stabilizing a way to pass configuration values to Clippy. |
| 24 | + |
| 25 | +It’s our duty as maintainers to make sure that user’s workflows don’t break. And that they don’t worry about future |
| 26 | +problems in their pipelines because we haven’t formerly stabilized a configuration file. |
| 27 | + |
| 28 | +[Kernel thread talking about how clippy.toml being unstable is worrisome ](https://lore.kernel.org/all/[email protected]/) |
| 29 | +[Zulip thread](https://rust-lang.zulipchat.com/#narrow/channel/257328-clippy/topic/stablization.20of.20clippy.2Etoml.20a/with/486169617) |
| 30 | + |
| 31 | +# Guide-level explanation |
| 32 | + |
| 33 | +Some Clippy lints can be tweaked via a configuration file commonly named `clippy.toml` or `.clippy.toml`. |
| 34 | + |
| 35 | +Clippy looks for this file in the current directory and all parents up to the root of the workspace if there is one. |
| 36 | + |
| 37 | +For crates in a workspace, the Clippy configuration files would be merged with the ones in their parent directories. |
| 38 | +A Clippy configuration file will always overwrite one in a parent directory |
| 39 | + |
| 40 | +This means, that if your workspace has `clippy.toml` and `crates/my_crate/clippy.toml`, when Clippy analyzes |
| 41 | +`my_crate`, it will merge both versions with `crates/my_crate/clippy.toml` taking prevalence over `clippy.toml` for |
| 42 | +`my_crate`. |
| 43 | + |
| 44 | +This system can be overrun via the `CLIPPY_CONF_PATH` environment variable passed to the `clippy-driver` or |
| 45 | +`cargo clippy` invocation. If this environment variable is declared and points to a directory, Clippy just looks up |
| 46 | +specifically that directory and assumes that there isn’t a Clippy configuration file if it can’t find `clippy.toml` |
| 47 | +or `.clippy.toml`. If `CLIPPY_CONF_PATH` points to a file, that file will be interpreted as `clippy.toml`. |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +The Clippy configuration file follows a subset of the TOML specification. You can declare key-value pairs within it |
| 52 | +in plain text. Here's an example. |
| 53 | + |
| 54 | +```toml |
| 55 | +unstable-conf = false |
| 56 | + |
| 57 | +msrv = "1.76.0" |
| 58 | +avoid-breaking-exported-api = true |
| 59 | +check-private-items = true |
| 60 | +``` |
| 61 | + |
| 62 | +The list of available configurations along their default values is |
| 63 | +[here](https://doc.rust-lang.org/nightly/clippy/lint_configuration.html). |
| 64 | +You can enable unstable features via the `unsable-conf` configuration option. If this configuration is set to `true`, |
| 65 | +Clippy will warn about unknown or renamed keys, and will allow the modification of unstable configuration options. One |
| 66 | +can use `unstable-conf = true` sparingly in their development cycle to check for unknown Rust to check for typos or |
| 67 | +nonexistent configuration options. |
| 68 | + |
| 69 | +While TOML tables (`[table]`) are currently ignored at the time of writing, they could be added in a future version |
| 70 | +for lint-specific configuration or similar. |
| 71 | + |
| 72 | +# Reference-level explanation |
| 73 | + |
| 74 | +Before Clippy initializes, it checks for `CLIPPY_CONF_PATH` environmental variable. If that exists, it checks if its |
| 75 | +a directory (in which it performs the known algorithm, looking for all parents up to `/`, merging them...). If |
| 76 | +`CLIPPY_CONF_PATH` is a file, just read it as we'd do with `clippy.toml`. |
| 77 | + |
| 78 | +`CLIPPY_CONF_PATH` takes prevalence over `CLIPPY_CONF_DIR`, but if the former doesn't exist, look for the latter. If |
| 79 | +none of these exist, look for the usual `clippy.toml`/`.clippy.toml` in the crate's directory. |
| 80 | + |
| 81 | +With merging, we are referring about the configuration keys with more prevalence, completely replacing the less |
| 82 | +prevalent ones. Configuration keys which are not mentioned in the most prevalent one would be taken from less |
| 83 | +prevalent ones. As a practical example, |
| 84 | + |
| 85 | +```toml |
| 86 | +# clippy.toml |
| 87 | +msrv = "1.82.0" |
| 88 | +disallowed-macros = [ { path = "my_crate::bad_macro" } ] |
| 89 | + |
| 90 | +# crates/my_crate/clippy.toml |
| 91 | +msrv = "1.76.0" # This overrides that 1.82.0 for `my_crate` |
| 92 | +# We adopt that disallowed-macros from the outer clippy.toml |
| 93 | +``` |
| 94 | + |
| 95 | +## Stabilization process for keys |
| 96 | + |
| 97 | +Each key needs to be independently stabilized, or it won't be active on configurations without `unstable-conf` enabled. |
| 98 | +This is done via opening a Github PR marking it as no longer unstable, having fixed all issues related to that key |
| 99 | +beforehand. |
| 100 | + |
| 101 | +Things that need to be accounted for before stabilizing a key: |
| 102 | + |
| 103 | +- Are there any lints having problems with this configuration key? |
| 104 | +- Is this key mentioned in any issue? Is it a relevant factor to that issue? |
| 105 | +- Does this key impact unstable features of the compiler, does it rely on behaviour that only happens on nightly? |
| 106 | +- If the key affects macro behaviour, is it well handled? |
| 107 | + |
| 108 | +If necessary, perform some tests on well-known and stable codebases before merging, either via lintcheck, crater or |
| 109 | +manually. |
| 110 | + |
| 111 | +One can use @rust-rfcbot's capabilities to facilitate registering concerns and closing them with |
| 112 | +`@rust-rfcbot concern <concern name>`. When all concerns are fixed, a team member will use the `@rust-rfcbot fcp merge` |
| 113 | +command. [Refer to rust-rfcbot's documentation for more information](https://github.com/rust-lang/rfcbot-rs) |
| 114 | +but this should be enough for our usecase. |
| 115 | + |
| 116 | +While a key is not stable, it's marked on documentation as unstable, and it will report an error |
| 117 | + |
| 118 | +# Drawbacks |
| 119 | + |
| 120 | +- There isn't a good way to specify configuration values from the terminal, for the time being. |
| 121 | +- In stable mode, typos and/or deprecated and/or renamed configuration keys being renamed cannot be warned against. |
| 122 | + |
| 123 | +# Prior art |
| 124 | + |
| 125 | +- `rustfmt.toml` |
| 126 | +- `.cargo/config.toml` |
| 127 | +- `rust-toolchain.toml` |
0 commit comments