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
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
<!-- rules:end -->

Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and
Expand Down
43 changes: 43 additions & 0 deletions crates/sourceorigin-standard/src/rules/clippy_config.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
8 changes: 8 additions & 0 deletions crates/sourceorigin-standard/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -305,6 +310,8 @@ struct Repository {
toolchain: Option<TomlFile<ToolchainFile>>,
/// `rustfmt.toml`, empty when missing.
rustfmt: TomlFile<Settings>,
/// `clippy.toml`, empty when missing.
clippy: TomlFile<Settings>,
/// `.cargo/config.toml`.
cargo_config: Option<TomlFile<CargoConfig>>,
/// The manifests of the workspace's crates.
Expand Down Expand Up @@ -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)?,
Expand Down
2 changes: 1 addition & 1 deletion crates/sourceorigin-standard/src/rules/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Value {

impl TomlFile<Settings> {
/// 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)
Expand Down
23 changes: 23 additions & 0 deletions crates/sourceorigin-standard/src/rules/tests/clippy_config.rs
Original file line number Diff line number Diff line change
@@ -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::<String>::new());
}
7 changes: 6 additions & 1 deletion crates/sourceorigin-standard/src/rules/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand All @@ -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.
Expand Down