From 2b5c700475efe2c19f88f2ee7c8a08da9699b8be Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 23:17:49 +0100 Subject: [PATCH] feat: workspace-lints --fix adds each lint the standard requires that the workspace lacks --- crates/sourceorigin-standard/README.md | 3 +- .../src/rules/tests/workspace_lints.rs | 21 +++++ .../src/rules/workspace_lints.rs | 77 ++++++++++++++----- 3 files changed, 82 insertions(+), 19 deletions(-) diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index 44d45e2..4e9ad2d 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -109,7 +109,8 @@ piqueld = 2 - `cargo xtask standard --fix` first runs [`rules::fix`], which makes the fixes that need no judgement and prints each, such as adding `[lints] workspace = true`, making a workspace lint at `deny` or `forbid` - `warn` (never an `allow`), making a crate inherit the workspace's edition, + `warn` (never an `allow`), adding a required lint the workspace lacks, + making a crate inherit the workspace's edition, lowering a ceiling of `budget.toml` to what the crate holds, or removing a stale deviation or entry of one. It edits each TOML file as a document, so its layout and comments stay. diff --git a/crates/sourceorigin-standard/src/rules/tests/workspace_lints.rs b/crates/sourceorigin-standard/src/rules/tests/workspace_lints.rs index 695323e..606e189 100644 --- a/crates/sourceorigin-standard/src/rules/tests/workspace_lints.rs +++ b/crates/sourceorigin-standard/src/rules/tests/workspace_lints.rs @@ -51,3 +51,24 @@ fn every_required_lint_warns_and_others_warn_or_allow() { ] ); } + +#[test] +fn fix_adds_each_required_lint_the_workspace_lacks() { + let fixture = Fixture::new(); + fixture.add_crate("core", INHERITS).write( + "Cargo.toml", + "[workspace]\nmembers = [\"crates/*\"]\n\n[workspace.lints.rust]\nunsafe_code = \"warn\"\n", + ); + fixture.fix_rule(Rule::WorkspaceLints); + assert_eq!( + fixture.violations(Rule::WorkspaceLints), + Vec::::new() + ); + let manifest = + std::fs::read_to_string(fixture.0.root().join("Cargo.toml")).expect("the manifest is read"); + assert!( + manifest + .contains("\n[workspace.lints.clippy]\nall = { level = \"warn\", priority = -1 }\n"), + "{manifest}" + ); +} diff --git a/crates/sourceorigin-standard/src/rules/workspace_lints.rs b/crates/sourceorigin-standard/src/rules/workspace_lints.rs index b09ac66..a87d7eb 100644 --- a/crates/sourceorigin-standard/src/rules/workspace_lints.rs +++ b/crates/sourceorigin-standard/src/rules/workspace_lints.rs @@ -26,6 +26,10 @@ const REQUIRED: [(&str, &str); 14] = [ ("clippy", "self_named_module_files"), ]; +/// Clippy's groups among them, which sit at priority -1 so single lints +/// can override them. +const GROUPS: [(&str, &str); 2] = [("clippy", "all"), ("clippy", "pedantic")]; + /// A lint the root manifest's `[workspace.lints]` sets. pub(super) struct LintLevel { /// Its tool's table, such as `rust` or `clippy`. @@ -77,7 +81,8 @@ impl Repository { /// Cargo allows only without lint tables of the crate's own; each lint /// the standard requires that the workspace doesn't set to `warn`; and /// each other workspace lint set to neither `warn` nor `allow`. It fixes - /// a missing `[lints]` and a `deny` or `forbid`, never an `allow`. + /// a missing `[lints]`, a `deny` or `forbid`, and a missing required lint, + /// which it adds at `warn`; never an `allow`. pub(super) fn workspace_lints(&self) -> Checked { let crates = self .manifests @@ -124,21 +129,6 @@ impl Repository { message, }) }); - let missing = REQUIRED - .iter() - .filter(|(tool, name)| { - !self - .lint_levels - .iter() - .any(|lint| lint.tool == *tool && lint.name == *name) - }) - .map(|(tool, name)| Finding { - path: ROOT.into(), - line: 1, - message: format!( - "the standard requires `{name} = \"warn\"` in `[workspace.lints.{tool}]`" - ), - }); let inherit = self .manifests .iter() @@ -169,9 +159,60 @@ impl Repository { }, ) }); + let unset = self.unset_lints(); + Checked { + findings: crates.chain(levels).chain(unset.findings).collect(), + fixes: inherit.chain(warn).chain(unset.fixes).collect(), + } + } + + /// Each lint the standard requires that `[workspace.lints]` doesn't + /// set, fixed by adding it at `warn` under its tool's table. + fn unset_lints(&self) -> Checked { + let unset: Vec<(&str, &str)> = REQUIRED + .into_iter() + .filter(|(tool, name)| { + !self + .lint_levels + .iter() + .any(|lint| lint.tool == *tool && lint.name == *name) + }) + .collect(); + let missing = unset.iter().map(|(tool, name)| Finding { + path: ROOT.into(), + line: 1, + message: format!( + "the standard requires `{name} = \"warn\"` in `[workspace.lints.{tool}]`" + ), + }); + let add = unset.iter().map(|&(tool, name)| { + Fix::new( + ROOT, + format!("add `{name} = \"warn\"` to `[workspace.lints.{tool}]`"), + move |document| { + let lints = &mut document["workspace"]["lints"]; + if lints.is_none() { + let mut implicit = toml_edit::Table::new(); + implicit.set_implicit(true); + *lints = toml_edit::Item::Table(implicit); + } + if lints.get(tool).is_none() { + lints[tool] = toml_edit::table(); + } + lints[tool][name] = if GROUPS.contains(&(tool, name)) { + let mut group = toml_edit::InlineTable::new(); + group.insert("level", "warn".into()); + group.insert("priority", (-1).into()); + toml_edit::value(group) + } else { + toml_edit::value("warn") + }; + }, + ) + }); Checked { - findings: crates.chain(levels).chain(missing).collect(), - fixes: inherit.chain(warn).collect(), + findings: missing.collect(), + fixes: add.collect(), } } }