M0.2: workspace lints and lints self-test
Denies unwrap/expect/panic/unreachable/todo/unimplemented, indexing and string slicing, missing docs and unsafe code outside tests. clippy.toml allows them in tests. cargo xtask lints-selftest proves both directions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
b10a2a72f9
commit
ec1c72d9ac
7 changed files with 397 additions and 3 deletions
175
Cargo.lock
generated
175
Cargo.lock
generated
|
|
@ -2,6 +2,34 @@
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "equivalent"
|
||||||
|
version = "1.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hashbrown"
|
||||||
|
version = "0.17.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "indexmap"
|
||||||
|
version = "2.14.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855"
|
||||||
|
dependencies = [
|
||||||
|
"equivalent",
|
||||||
|
"hashbrown",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itoa"
|
||||||
|
version = "1.0.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lutra-cli"
|
name = "lutra-cli"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
|
|
@ -50,6 +78,153 @@ version = "0.0.0"
|
||||||
name = "lutra-vfs"
|
name = "lutra-vfs"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "memchr"
|
||||||
|
version = "2.8.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.107"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.47"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde"
|
||||||
|
version = "1.0.229"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
|
||||||
|
dependencies = [
|
||||||
|
"serde_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_core"
|
||||||
|
version = "1.0.229"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
|
||||||
|
dependencies = [
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_derive"
|
||||||
|
version = "1.0.229"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_json"
|
||||||
|
version = "1.0.154"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6"
|
||||||
|
dependencies = [
|
||||||
|
"itoa",
|
||||||
|
"memchr",
|
||||||
|
"serde",
|
||||||
|
"serde_core",
|
||||||
|
"zmij",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_spanned"
|
||||||
|
version = "1.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4e7523beb55eece201a2356bee0bbca0d1ab466c14c07703b2e0ee6d42cb0c2c"
|
||||||
|
dependencies = [
|
||||||
|
"serde_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "3.0.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toml"
|
||||||
|
version = "1.1.8+spec-1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "20489e00e4d8741d6be680764cc12e270655e375a20d1011e844a9c3379e678d"
|
||||||
|
dependencies = [
|
||||||
|
"indexmap",
|
||||||
|
"serde_core",
|
||||||
|
"serde_spanned",
|
||||||
|
"toml_datetime",
|
||||||
|
"toml_parser",
|
||||||
|
"toml_writer",
|
||||||
|
"winnow",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toml_datetime"
|
||||||
|
version = "1.1.2+spec-1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2b86d767906c6c42421dcba507eb9d203e779497710a47782a224bb871653053"
|
||||||
|
dependencies = [
|
||||||
|
"serde_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toml_parser"
|
||||||
|
version = "1.1.5+spec-1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "baa693a8032d7e1cada7d0041e96126df243179ff061456783ac7f12bda4744c"
|
||||||
|
dependencies = [
|
||||||
|
"winnow",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toml_writer"
|
||||||
|
version = "1.1.3+spec-1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "06bdbd8cfc056b8d2e2e85f29b56a3bdbecb527cef81eb39e3e7b98af4652770"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.27"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winnow"
|
||||||
|
version = "1.0.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "23b97319f7b8343df12cc98938e5c3eb436064524c8d2b4e30a1d3a36eecdf81"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "xtask"
|
name = "xtask"
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
|
dependencies = [
|
||||||
|
"serde_json",
|
||||||
|
"toml",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zmij"
|
||||||
|
version = "1.0.23"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
|
||||||
|
|
|
||||||
15
Cargo.toml
15
Cargo.toml
|
|
@ -25,3 +25,18 @@ lutra-mcp = { path = "crates/mcp" }
|
||||||
lutra-testkit = { path = "crates/testkit" }
|
lutra-testkit = { path = "crates/testkit" }
|
||||||
|
|
||||||
[workspace.lints.rust]
|
[workspace.lints.rust]
|
||||||
|
missing_docs = "deny"
|
||||||
|
unsafe_code = "deny"
|
||||||
|
|
||||||
|
[workspace.lints.clippy]
|
||||||
|
unwrap_used = "deny"
|
||||||
|
expect_used = "deny"
|
||||||
|
panic = "deny"
|
||||||
|
unreachable = "deny"
|
||||||
|
todo = "deny"
|
||||||
|
unimplemented = "deny"
|
||||||
|
indexing_slicing = "deny"
|
||||||
|
string_slice = "deny"
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
panic = "unwind"
|
||||||
|
|
|
||||||
4
clippy.toml
Normal file
4
clippy.toml
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
allow-unwrap-in-tests = true
|
||||||
|
allow-expect-in-tests = true
|
||||||
|
allow-panic-in-tests = true
|
||||||
|
allow-indexing-slicing-in-tests = true
|
||||||
|
|
@ -12,6 +12,8 @@ publish.workspace = true
|
||||||
layer = "tool"
|
layer = "tool"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
serde_json = "1.0.154"
|
||||||
|
toml = "1.1.8"
|
||||||
|
|
||||||
[lints]
|
[lints]
|
||||||
workspace = true
|
workspace = true
|
||||||
|
|
|
||||||
146
xtask/src/lints.rs
Normal file
146
xtask/src/lints.rs
Normal file
|
|
@ -0,0 +1,146 @@
|
||||||
|
//! `cargo xtask lints-selftest`: proves that the workspace lint table rejects
|
||||||
|
//! panicking code in shipped code and accepts it in tests.
|
||||||
|
//!
|
||||||
|
//! The check builds two scratch crates under `target/xtask/lints-selftest/`
|
||||||
|
//! that copy `[workspace.lints]` and `clippy.toml` from the repository: one
|
||||||
|
//! calls `unwrap()` in a public function and must fail clippy, the other calls
|
||||||
|
//! it only inside `#[cfg(test)]` and must pass.
|
||||||
|
|
||||||
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
use crate::util::{TaskResult, repo_root};
|
||||||
|
|
||||||
|
/// Source of the scratch crate whose shipped code unwraps.
|
||||||
|
const SHIPPED_SRC: &str =
|
||||||
|
"//! Probe.\n\n/// Probe.\npub fn probe(v: Option<u8>) -> u8 {\n v.unwrap()\n}\n";
|
||||||
|
|
||||||
|
/// Source of the scratch crate that unwraps only in a test.
|
||||||
|
const TESTED_SRC: &str = r#"//! Probe.
|
||||||
|
|
||||||
|
/// Probe.
|
||||||
|
pub fn probe(v: Option<u8>) -> u8 {
|
||||||
|
v.unwrap_or(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn probe_returns_value() {
|
||||||
|
let v: Option<u8> = "1".parse().ok();
|
||||||
|
assert_eq!(super::probe(v), v.unwrap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
|
||||||
|
/// Runs the self-test.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// Fails if the shipped probe passes clippy, if the test probe fails it, or
|
||||||
|
/// if the scratch crates cannot be written or checked.
|
||||||
|
pub fn selftest() -> TaskResult {
|
||||||
|
let root = repo_root()?;
|
||||||
|
let lints =
|
||||||
|
lints_section(&fs::read_to_string(root.join("Cargo.toml")).map_err(|e| e.to_string())?)?;
|
||||||
|
let clippy = fs::read_to_string(root.join("clippy.toml")).map_err(|e| e.to_string())?;
|
||||||
|
let base = root.join("target").join("xtask").join("lints-selftest");
|
||||||
|
|
||||||
|
let shipped = probe(&base, "shipped", SHIPPED_SRC, &lints, &clippy)?;
|
||||||
|
if shipped.0 {
|
||||||
|
return Err("lints-selftest: unwrap() in shipped code passed clippy".into());
|
||||||
|
}
|
||||||
|
if !shipped.1.contains("unwrap_used") {
|
||||||
|
return Err(format!(
|
||||||
|
"lints-selftest: shipped probe failed for another reason:\n{}",
|
||||||
|
shipped.1
|
||||||
|
));
|
||||||
|
}
|
||||||
|
let tested = probe(&base, "tested", TESTED_SRC, &lints, &clippy)?;
|
||||||
|
if !tested.0 {
|
||||||
|
return Err(format!(
|
||||||
|
"lints-selftest: unwrap() in a test was rejected:\n{}",
|
||||||
|
tested.1
|
||||||
|
));
|
||||||
|
}
|
||||||
|
println!("lints-selftest: shipped unwrap() rejected, test unwrap() accepted");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Converts the `[workspace.lints]` table of the root manifest into the
|
||||||
|
/// `[lints.*]` sections of a standalone manifest.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// Fails if the manifest is not valid TOML or has no `[workspace.lints]`.
|
||||||
|
pub fn lints_section(manifest: &str) -> TaskResult<String> {
|
||||||
|
let doc: toml::Table = manifest.parse().map_err(|e| format!("Cargo.toml: {e}"))?;
|
||||||
|
let lints = doc
|
||||||
|
.get("workspace")
|
||||||
|
.and_then(|w| w.get("lints"))
|
||||||
|
.and_then(toml::Value::as_table)
|
||||||
|
.ok_or("Cargo.toml has no [workspace.lints]")?;
|
||||||
|
let mut out = String::new();
|
||||||
|
for (group, table) in lints {
|
||||||
|
out.push_str(&format!("[lints.{group}]\n"));
|
||||||
|
if let Some(table) = table.as_table() {
|
||||||
|
for (name, level) in table {
|
||||||
|
out.push_str(&format!("{name} = {level}\n"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out.push('\n');
|
||||||
|
}
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Writes one scratch crate and runs clippy on all its targets.
|
||||||
|
///
|
||||||
|
/// Returns whether clippy passed and its combined output.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// Fails if the files cannot be written or cargo cannot be started.
|
||||||
|
fn probe(
|
||||||
|
base: &Path,
|
||||||
|
name: &str,
|
||||||
|
src: &str,
|
||||||
|
lints: &str,
|
||||||
|
clippy: &str,
|
||||||
|
) -> TaskResult<(bool, String)> {
|
||||||
|
let dir = base.join(name);
|
||||||
|
fs::create_dir_all(dir.join("src")).map_err(|e| e.to_string())?;
|
||||||
|
let manifest = format!(
|
||||||
|
"[package]\nname = \"lint-probe-{name}\"\nversion = \"0.0.0\"\nedition = \"2024\"\npublish = false\n\n[workspace]\n\n{lints}"
|
||||||
|
);
|
||||||
|
fs::write(dir.join("Cargo.toml"), manifest).map_err(|e| e.to_string())?;
|
||||||
|
fs::write(dir.join("clippy.toml"), clippy).map_err(|e| e.to_string())?;
|
||||||
|
fs::write(dir.join("src").join("lib.rs"), src).map_err(|e| e.to_string())?;
|
||||||
|
let output = Command::new(std::env::var("CARGO").unwrap_or_else(|_| "cargo".into()))
|
||||||
|
.args(["clippy", "--quiet", "--all-targets", "--", "-D", "warnings"])
|
||||||
|
.current_dir(&dir)
|
||||||
|
.env("CARGO_TARGET_DIR", base.join("target"))
|
||||||
|
.output()
|
||||||
|
.map_err(|e| format!("cannot start cargo: {e}"))?;
|
||||||
|
let text = format!(
|
||||||
|
"{}{}",
|
||||||
|
String::from_utf8_lossy(&output.stdout),
|
||||||
|
String::from_utf8_lossy(&output.stderr)
|
||||||
|
);
|
||||||
|
Ok((output.status.success(), text))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::lints_section;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn copies_lint_groups() {
|
||||||
|
let manifest = "[workspace.lints.rust]\nmissing_docs = \"deny\"\n[workspace.lints.clippy]\nunwrap_used = \"deny\"\n";
|
||||||
|
let out = lints_section(manifest).unwrap();
|
||||||
|
assert!(out.contains("[lints.rust]\nmissing_docs = \"deny\""));
|
||||||
|
assert!(out.contains("[lints.clippy]\nunwrap_used = \"deny\""));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rejects_manifest_without_lints() {
|
||||||
|
assert!(lints_section("[workspace]\n").is_err());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,39 @@
|
||||||
//! Project automation for Lutra, run as `cargo xtask <command>`.
|
//! Project automation for Lutra, run as `cargo xtask <command>`.
|
||||||
|
//!
|
||||||
|
//! Every subcommand prints what it checks and exits with status 1 and a
|
||||||
|
//! message when the check fails.
|
||||||
|
|
||||||
/// Entry point; the subcommands arrive with milestone M0.3.
|
mod lints;
|
||||||
fn main() {
|
mod util;
|
||||||
println!("xtask");
|
|
||||||
|
use std::process::ExitCode;
|
||||||
|
|
||||||
|
/// Usage text printed for `help` and unknown subcommands.
|
||||||
|
const USAGE: &str = "usage: cargo xtask <command>
|
||||||
|
|
||||||
|
commands:
|
||||||
|
lints-selftest prove that shipped unwrap() fails clippy and test unwrap() passes";
|
||||||
|
|
||||||
|
/// Dispatches to the subcommand named by the first argument.
|
||||||
|
fn main() -> ExitCode {
|
||||||
|
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||||
|
let Some(command) = args.first() else {
|
||||||
|
println!("{USAGE}");
|
||||||
|
return ExitCode::SUCCESS;
|
||||||
|
};
|
||||||
|
let result = match command.as_str() {
|
||||||
|
"lints-selftest" => lints::selftest(),
|
||||||
|
"help" | "--help" | "-h" => {
|
||||||
|
println!("{USAGE}");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
other => Err(format!("unknown command `{other}`\n\n{USAGE}")),
|
||||||
|
};
|
||||||
|
match result {
|
||||||
|
Ok(()) => ExitCode::SUCCESS,
|
||||||
|
Err(message) => {
|
||||||
|
eprintln!("xtask: {message}");
|
||||||
|
ExitCode::FAILURE
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
19
xtask/src/util.rs
Normal file
19
xtask/src/util.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
//! Shared helpers for xtask subcommands: locating the repository, running
|
||||||
|
//! child processes and listing the files that belong to the repository.
|
||||||
|
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
/// Result type of every xtask step; the error is a message for the console.
|
||||||
|
pub type TaskResult<T = ()> = Result<T, String>;
|
||||||
|
|
||||||
|
/// Returns the repository root, the parent of the `xtask` crate directory.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// Fails if the manifest directory has no parent, which only happens when the
|
||||||
|
/// crate was moved out of the workspace.
|
||||||
|
pub fn repo_root() -> TaskResult<PathBuf> {
|
||||||
|
Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||||
|
.parent()
|
||||||
|
.map(Path::to_path_buf)
|
||||||
|
.ok_or_else(|| "xtask is not inside the workspace".to_string())
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue