Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/sourceorigin-standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 21 additions & 0 deletions crates/sourceorigin-standard/src/rules/tests/workspace_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>::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}"
);
}
77 changes: 59 additions & 18 deletions crates/sourceorigin-standard/src/rules/workspace_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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(),
}
}
}