Skip to content

Commit 2b5c700

Browse files
committed
feat: workspace-lints --fix adds each lint the standard requires that the workspace lacks
1 parent f340cfb commit 2b5c700

3 files changed

Lines changed: 82 additions & 19 deletions

File tree

‎crates/sourceorigin-standard/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ piqueld = 2
109109
- `cargo xtask standard --fix` first runs [`rules::fix`], which makes the
110110
fixes that need no judgement and prints each, such as adding
111111
`[lints] workspace = true`, making a workspace lint at `deny` or `forbid`
112-
`warn` (never an `allow`), making a crate inherit the workspace's edition,
112+
`warn` (never an `allow`), adding a required lint the workspace lacks,
113+
making a crate inherit the workspace's edition,
113114
lowering a ceiling of `budget.toml` to what the crate holds, or removing a
114115
stale deviation or entry of one. It edits each
115116
TOML file as a document, so its layout and comments stay.

‎crates/sourceorigin-standard/src/rules/tests/workspace_lints.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,24 @@ fn every_required_lint_warns_and_others_warn_or_allow() {
5151
]
5252
);
5353
}
54+
55+
#[test]
56+
fn fix_adds_each_required_lint_the_workspace_lacks() {
57+
let fixture = Fixture::new();
58+
fixture.add_crate("core", INHERITS).write(
59+
"Cargo.toml",
60+
"[workspace]\nmembers = [\"crates/*\"]\n\n[workspace.lints.rust]\nunsafe_code = \"warn\"\n",
61+
);
62+
fixture.fix_rule(Rule::WorkspaceLints);
63+
assert_eq!(
64+
fixture.violations(Rule::WorkspaceLints),
65+
Vec::<String>::new()
66+
);
67+
let manifest =
68+
std::fs::read_to_string(fixture.0.root().join("Cargo.toml")).expect("the manifest is read");
69+
assert!(
70+
manifest
71+
.contains("\n[workspace.lints.clippy]\nall = { level = \"warn\", priority = -1 }\n"),
72+
"{manifest}"
73+
);
74+
}

‎crates/sourceorigin-standard/src/rules/workspace_lints.rs‎

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ const REQUIRED: [(&str, &str); 14] = [
2626
("clippy", "self_named_module_files"),
2727
];
2828

29+
/// Clippy's groups among them, which sit at priority -1 so single lints
30+
/// can override them.
31+
const GROUPS: [(&str, &str); 2] = [("clippy", "all"), ("clippy", "pedantic")];
32+
2933
/// A lint the root manifest's `[workspace.lints]` sets.
3034
pub(super) struct LintLevel {
3135
/// Its tool's table, such as `rust` or `clippy`.
@@ -77,7 +81,8 @@ impl Repository {
7781
/// Cargo allows only without lint tables of the crate's own; each lint
7882
/// the standard requires that the workspace doesn't set to `warn`; and
7983
/// each other workspace lint set to neither `warn` nor `allow`. It fixes
80-
/// a missing `[lints]` and a `deny` or `forbid`, never an `allow`.
84+
/// a missing `[lints]`, a `deny` or `forbid`, and a missing required lint,
85+
/// which it adds at `warn`; never an `allow`.
8186
pub(super) fn workspace_lints(&self) -> Checked {
8287
let crates = self
8388
.manifests
@@ -124,21 +129,6 @@ impl Repository {
124129
message,
125130
})
126131
});
127-
let missing = REQUIRED
128-
.iter()
129-
.filter(|(tool, name)| {
130-
!self
131-
.lint_levels
132-
.iter()
133-
.any(|lint| lint.tool == *tool && lint.name == *name)
134-
})
135-
.map(|(tool, name)| Finding {
136-
path: ROOT.into(),
137-
line: 1,
138-
message: format!(
139-
"the standard requires `{name} = \"warn\"` in `[workspace.lints.{tool}]`"
140-
),
141-
});
142132
let inherit = self
143133
.manifests
144134
.iter()
@@ -169,9 +159,60 @@ impl Repository {
169159
},
170160
)
171161
});
162+
let unset = self.unset_lints();
163+
Checked {
164+
findings: crates.chain(levels).chain(unset.findings).collect(),
165+
fixes: inherit.chain(warn).chain(unset.fixes).collect(),
166+
}
167+
}
168+
169+
/// Each lint the standard requires that `[workspace.lints]` doesn't
170+
/// set, fixed by adding it at `warn` under its tool's table.
171+
fn unset_lints(&self) -> Checked {
172+
let unset: Vec<(&str, &str)> = REQUIRED
173+
.into_iter()
174+
.filter(|(tool, name)| {
175+
!self
176+
.lint_levels
177+
.iter()
178+
.any(|lint| lint.tool == *tool && lint.name == *name)
179+
})
180+
.collect();
181+
let missing = unset.iter().map(|(tool, name)| Finding {
182+
path: ROOT.into(),
183+
line: 1,
184+
message: format!(
185+
"the standard requires `{name} = \"warn\"` in `[workspace.lints.{tool}]`"
186+
),
187+
});
188+
let add = unset.iter().map(|&(tool, name)| {
189+
Fix::new(
190+
ROOT,
191+
format!("add `{name} = \"warn\"` to `[workspace.lints.{tool}]`"),
192+
move |document| {
193+
let lints = &mut document["workspace"]["lints"];
194+
if lints.is_none() {
195+
let mut implicit = toml_edit::Table::new();
196+
implicit.set_implicit(true);
197+
*lints = toml_edit::Item::Table(implicit);
198+
}
199+
if lints.get(tool).is_none() {
200+
lints[tool] = toml_edit::table();
201+
}
202+
lints[tool][name] = if GROUPS.contains(&(tool, name)) {
203+
let mut group = toml_edit::InlineTable::new();
204+
group.insert("level", "warn".into());
205+
group.insert("priority", (-1).into());
206+
toml_edit::value(group)
207+
} else {
208+
toml_edit::value("warn")
209+
};
210+
},
211+
)
212+
});
172213
Checked {
173-
findings: crates.chain(levels).chain(missing).collect(),
174-
fixes: inherit.chain(warn).collect(),
214+
findings: missing.collect(),
215+
fixes: add.collect(),
175216
}
176217
}
177218
}

0 commit comments

Comments
 (0)