From 412baf8473ed0a60dda383cd9cfb7c7a33744bec Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 21:28:12 +0100 Subject: [PATCH] feat: clippy-config lets tests expect, panic and print, and never unwrap --- crates/sourceorigin-standard/README.md | 1 + .../src/rules/clippy_config.rs | 43 +++++++++++++++++++ crates/sourceorigin-standard/src/rules/mod.rs | 8 ++++ .../src/rules/settings.rs | 2 +- .../src/rules/tests/clippy_config.rs | 23 ++++++++++ .../src/rules/tests/mod.rs | 7 ++- 6 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 crates/sourceorigin-standard/src/rules/clippy_config.rs create mode 100644 crates/sourceorigin-standard/src/rules/tests/clippy_config.rs diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index 78d9c6a..c7ca7f8 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -44,6 +44,7 @@ A repository adopts the standard in four steps: - `agents-md`: AGENTS.md names every workspace crate, by its name in backticks or its directory, and holds at most 150 lines. - `toolchain`: `rust-toolchain.toml` pins `channel = "stable"` and installs at least the `rustfmt` and `clippy` components. - `rustfmt-config`: `rustfmt.toml` sets `edition = "2024"`, `newline_style = "Unix"` and `use_field_init_shorthand = true`. +- `clippy-config`: `clippy.toml` sets `allow-expect-in-tests`, `allow-panic-in-tests` and `allow-dbg-in-tests` to `true`, and never `allow-unwrap-in-tests`. Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and diff --git a/crates/sourceorigin-standard/src/rules/clippy_config.rs b/crates/sourceorigin-standard/src/rules/clippy_config.rs new file mode 100644 index 0000000..17326d9 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/clippy_config.rs @@ -0,0 +1,43 @@ +//! `clippy-config`: tests may expect, panic and print, but never unwrap. + +use super::fix::{Checked, Fix}; +use super::settings::Value; +use super::{Finding, Repository}; + +/// Clippy's configuration, from the repository's root. +pub(super) const PATH: &str = "clippy.toml"; + +/// The settings it requires. +const REQUIRED: [(&str, Value); 3] = [ + ("allow-expect-in-tests", Value::Bool(true)), + ("allow-panic-in-tests", Value::Bool(true)), + ("allow-dbg-in-tests", Value::Bool(true)), +]; + +/// The setting it may not turn on. +const UNWRAP: &str = "allow-unwrap-in-tests"; + +impl Repository { + /// Each required setting `clippy.toml` lacks or sets otherwise, and + /// `allow-unwrap-in-tests = true`, which the fix removes. + pub(super) fn clippy_config(&self) -> Checked { + let mut checked = self.clippy.settings_are(&REQUIRED); + if let Some((line, value)) = self.clippy.setting(UNWRAP) + && value.as_bool() != Some(false) + { + checked.findings.push(Finding { + path: PATH.into(), + line, + message: format!( + "`{UNWRAP}` lets tests unwrap: remove it, and `expect` what should hold" + ), + }); + checked + .fixes + .push(Fix::new(PATH, format!("remove `{UNWRAP}`"), |document| { + document.remove(UNWRAP); + })); + } + checked + } +} diff --git a/crates/sourceorigin-standard/src/rules/mod.rs b/crates/sourceorigin-standard/src/rules/mod.rs index 256928d..0566708 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -8,6 +8,7 @@ mod agent_files; mod agents_md; mod budget; mod cargo_locked; +mod clippy_config; mod comments; mod config_comments; mod fix; @@ -157,6 +158,10 @@ rules! { /// `rustfmt.toml` sets `edition = "2024"`, `newline_style = "Unix"` and /// `use_field_init_shorthand = true`. RustfmtConfig = "rustfmt-config" => rustfmt_config, + /// `clippy.toml` sets `allow-expect-in-tests`, `allow-panic-in-tests` + /// and `allow-dbg-in-tests` to `true`, and never + /// `allow-unwrap-in-tests`. + ClippyConfig = "clippy-config" => clippy_config, } /// Why the rules couldn't be checked. @@ -305,6 +310,8 @@ struct Repository { toolchain: Option>, /// `rustfmt.toml`, empty when missing. rustfmt: TomlFile, + /// `clippy.toml`, empty when missing. + clippy: TomlFile, /// `.cargo/config.toml`. cargo_config: Option>, /// The manifests of the workspace's crates. @@ -343,6 +350,7 @@ impl Repository { cargo_config: TomlFile::load(root, layout::CARGO_CONFIG)?, toolchain: TomlFile::load(root, toolchain::PATH)?, rustfmt: TomlFile::load_or_empty(root, rustfmt_config::PATH)?, + clippy: TomlFile::load_or_empty(root, clippy_config::PATH)?, modules: Modules::of(&metadata, &sources), budget: Budget::load(root)?, commented: Commented::load_all(root)?, diff --git a/crates/sourceorigin-standard/src/rules/settings.rs b/crates/sourceorigin-standard/src/rules/settings.rs index d32bdc5..a4d8b06 100644 --- a/crates/sourceorigin-standard/src/rules/settings.rs +++ b/crates/sourceorigin-standard/src/rules/settings.rs @@ -46,7 +46,7 @@ impl Value { impl TomlFile { /// The setting `key`, with its line. - fn setting(&self, key: &str) -> Option<(usize, &toml::Value)> { + pub(super) fn setting(&self, key: &str) -> Option<(usize, &toml::Value)> { self.value .iter() .find(|(name, _)| name.get_ref() == key) diff --git a/crates/sourceorigin-standard/src/rules/tests/clippy_config.rs b/crates/sourceorigin-standard/src/rules/tests/clippy_config.rs new file mode 100644 index 0000000..8ba9edd --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/tests/clippy_config.rs @@ -0,0 +1,23 @@ +use super::{Fixture, INHERITS}; +use crate::rules::Rule; + +#[test] +fn tests_may_expect_panic_and_print_but_never_unwrap() { + let fixture = Fixture::new(); + fixture.add_crate("core", INHERITS).write( + "clippy.toml", + "allow-expect-in-tests = true\nallow-panic-in-tests = false\nallow-dbg-in-tests = true\n\ + allow-unwrap-in-tests = true\ndoc-valid-idents = [\"DirkEngine\", \"..\"]\n", + ); + assert_eq!( + fixture.violations(Rule::ClippyConfig), + [ + "clippy.toml:2: `allow-panic-in-tests` has another value: set \ + `allow-panic-in-tests = true` (clippy-config)", + "clippy.toml:4: `allow-unwrap-in-tests` lets tests unwrap: remove it, and `expect` \ + what should hold (clippy-config)", + ] + ); + fixture.fix_rule(Rule::ClippyConfig); + assert_eq!(fixture.violations(Rule::ClippyConfig), Vec::::new()); +} diff --git a/crates/sourceorigin-standard/src/rules/tests/mod.rs b/crates/sourceorigin-standard/src/rules/tests/mod.rs index 1002452..221b9dc 100644 --- a/crates/sourceorigin-standard/src/rules/tests/mod.rs +++ b/crates/sourceorigin-standard/src/rules/tests/mod.rs @@ -4,6 +4,7 @@ mod agent_files; mod agents_md; mod budget; mod cargo_locked; +mod clippy_config; mod comments; mod config_comments; mod fix; @@ -107,7 +108,7 @@ self_named_module_files = \"warn\" /// The configuration files every fixture holds, as the standard has them, /// so a rule's fixes touch only what a test writes. -const CONFIG: [(&str, &str); 3] = [ +const CONFIG: [(&str, &str); 4] = [ ( ".cargo/config.toml", "[alias]\nxtask = \"run --locked --package xtask --\"\n", @@ -120,6 +121,10 @@ const CONFIG: [(&str, &str); 3] = [ "rustfmt.toml", "edition = \"2024\"\nnewline_style = \"Unix\"\nuse_field_init_shorthand = true\n", ), + ( + "clippy.toml", + "allow-expect-in-tests = true\nallow-panic-in-tests = true\nallow-dbg-in-tests = true\n", + ), ]; /// A manifest tail inheriting the workspace's lints.