From 1e4857a4d95adc3088544f754c7130404811970c Mon Sep 17 00:00:00 2001 From: Urgau Date: Thu, 16 May 2024 13:22:17 +0200 Subject: [PATCH] Allow lint config to have extra custom configs And report the unused manifest key warning for every key that we do not use, which is currently every of them. --- crates/cargo-util-schemas/src/manifest/mod.rs | 9 +++++++++ src/cargo/util/toml/mod.rs | 10 +++++++++- tests/testsuite/lints_table.rs | 4 ++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/crates/cargo-util-schemas/src/manifest/mod.rs b/crates/cargo-util-schemas/src/manifest/mod.rs index 375d0de45..25df09ba6 100644 --- a/crates/cargo-util-schemas/src/manifest/mod.rs +++ b/crates/cargo-util-schemas/src/manifest/mod.rs @@ -1503,6 +1503,13 @@ impl TomlLint { Self::Config(config) => config.priority, } } + + pub fn config(&self) -> Option<&toml::Table> { + match self { + Self::Level(_) => None, + Self::Config(config) => Some(&config.config), + } + } } #[derive(Serialize, Deserialize, Debug, Clone)] @@ -1511,6 +1518,8 @@ pub struct TomlLintConfig { pub level: TomlLintLevel, #[serde(default)] pub priority: i8, + #[serde(flatten)] + pub config: toml::Table, } #[derive(Serialize, Deserialize, Debug, Copy, Clone, Eq, PartialEq)] diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 813868ac3..16dd2b4eb 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -2265,7 +2265,7 @@ supported tools: {}", if tool == "cargo" && !gctx.cli_unstable().cargo_lints { warn_for_cargo_lint_feature(gctx, warnings); } - for name in lints.keys() { + for (name, config) in lints { if let Some((prefix, suffix)) = name.split_once("::") { if tool == prefix { anyhow::bail!( @@ -2278,6 +2278,14 @@ supported tools: {}", } else { anyhow::bail!("`lints.{tool}.{name}` is not a valid lint name") } + } else if let Some(config) = config.config() { + for config_name in config.keys() { + // manually report unused manifest key warning since we collect all the "extra" + // keys and values inside the config table + let message = + format!("unused manifest key: `lints.{tool}.{name}.{config_name}`"); + warnings.push(message); + } } } } diff --git a/tests/testsuite/lints_table.rs b/tests/testsuite/lints_table.rs index cb376b182..93f44b5e8 100644 --- a/tests/testsuite/lints_table.rs +++ b/tests/testsuite/lints_table.rs @@ -172,8 +172,8 @@ fn warn_on_unused_key() { foo.cargo("check") .with_stderr( "\ -[WARNING] [CWD]/Cargo.toml: unused manifest key: lints.rust.rust-2018-idioms.unused -[WARNING] [CWD]/Cargo.toml: unused manifest key: workspace.lints.rust.rust-2018-idioms.unused +[WARNING][..]unused manifest key: `lints.rust.rust-2018-idioms.unused` +[WARNING][..]unused manifest key: `lints.rust.rust-2018-idioms.unused` [CHECKING] foo v0.0.1 ([CWD]) [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..]s ",