From 66076aafe37fb47dfa166aff53cd27d011dccc16 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 21:27:24 +0100 Subject: [PATCH] feat: rustfmt-config makes rustfmt.toml set the standard's edition, newlines and shorthand --- crates/sourceorigin-standard/README.md | 1 + crates/sourceorigin-standard/src/rules/mod.rs | 9 ++ .../src/rules/rustfmt_config.rs | 22 +++++ .../src/rules/settings.rs | 87 +++++++++++++++++++ .../src/rules/tests/mod.rs | 7 +- .../src/rules/tests/rustfmt_config.rs | 38 ++++++++ .../src/rules/toml_file.rs | 14 +++ 7 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 crates/sourceorigin-standard/src/rules/rustfmt_config.rs create mode 100644 crates/sourceorigin-standard/src/rules/settings.rs create mode 100644 crates/sourceorigin-standard/src/rules/tests/rustfmt_config.rs diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index ff76ab8..78d9c6a 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -43,6 +43,7 @@ A repository adopts the standard in four steps: - `agent-files`: `AGENTS.md` sits at the root, and no `CLAUDE.md` exists. Each skill is `.agents/skills//SKILL.md`, its frontmatter's `name` is ``, and `.claude/skills` is a symlink to `../.agents/skills`. - `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`. Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and diff --git a/crates/sourceorigin-standard/src/rules/mod.rs b/crates/sourceorigin-standard/src/rules/mod.rs index 2a703b5..256928d 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -17,6 +17,8 @@ mod level; mod manifest; mod modules; mod report; +mod rustfmt_config; +mod settings; mod source; #[cfg(test)] mod tests; @@ -42,6 +44,7 @@ use layout::CargoConfig; use manifest::{Manifest, RootManifest}; use modules::Modules; pub use report::{Report, RuleReport}; +use settings::Settings; use source::{Source, Unparsed}; use text_file::TextFile; use toml_file::TomlFile; @@ -151,6 +154,9 @@ rules! { /// `rust-toolchain.toml` pins `channel = "stable"` and installs at /// least the `rustfmt` and `clippy` components. Toolchain = "toolchain" => toolchain, + /// `rustfmt.toml` sets `edition = "2024"`, `newline_style = "Unix"` and + /// `use_field_init_shorthand = true`. + RustfmtConfig = "rustfmt-config" => rustfmt_config, } /// Why the rules couldn't be checked. @@ -297,6 +303,8 @@ struct Repository { root_manifest: TomlFile, /// `rust-toolchain.toml`. toolchain: Option>, + /// `rustfmt.toml`, empty when missing. + rustfmt: TomlFile, /// `.cargo/config.toml`. cargo_config: Option>, /// The manifests of the workspace's crates. @@ -334,6 +342,7 @@ impl Repository { root_manifest, cargo_config: TomlFile::load(root, layout::CARGO_CONFIG)?, toolchain: TomlFile::load(root, toolchain::PATH)?, + rustfmt: TomlFile::load_or_empty(root, rustfmt_config::PATH)?, modules: Modules::of(&metadata, &sources), budget: Budget::load(root)?, commented: Commented::load_all(root)?, diff --git a/crates/sourceorigin-standard/src/rules/rustfmt_config.rs b/crates/sourceorigin-standard/src/rules/rustfmt_config.rs new file mode 100644 index 0000000..9292011 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/rustfmt_config.rs @@ -0,0 +1,22 @@ +//! `rustfmt-config`: rustfmt formats every repository alike. + +use super::Repository; +use super::fix::Checked; +use super::settings::Value; + +/// rustfmt's configuration, from the repository's root. +pub(super) const PATH: &str = "rustfmt.toml"; + +/// The settings it requires. +const REQUIRED: [(&str, Value); 3] = [ + ("edition", Value::Str("2024")), + ("newline_style", Value::Str("Unix")), + ("use_field_init_shorthand", Value::Bool(true)), +]; + +impl Repository { + /// Each required setting `rustfmt.toml` lacks or sets otherwise. + pub(super) fn rustfmt_config(&self) -> Checked { + self.rustfmt.settings_are(&REQUIRED) + } +} diff --git a/crates/sourceorigin-standard/src/rules/settings.rs b/crates/sourceorigin-standard/src/rules/settings.rs new file mode 100644 index 0000000..d32bdc5 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/settings.rs @@ -0,0 +1,87 @@ +//! Flat TOML files of `key = value` settings, such as `rustfmt.toml`, and +//! the values the rules require of them. + +use std::collections::BTreeMap; + +use toml::Spanned; + +use super::Finding; +use super::fix::{Checked, Fix}; +use super::toml_file::TomlFile; + +/// A flat TOML file's settings. +pub(super) type Settings = BTreeMap, toml::Value>; + +/// A value a setting must have. +#[derive(Clone, Copy)] +pub(super) enum Value { + Str(&'static str), + Bool(bool), +} + +impl Value { + fn is(self, value: &toml::Value) -> bool { + match self { + Self::Str(wanted) => value.as_str() == Some(wanted), + Self::Bool(wanted) => value.as_bool() == Some(wanted), + } + } + + /// The value, for a TOML document. + fn toml(self) -> toml_edit::Value { + match self { + Self::Str(wanted) => wanted.into(), + Self::Bool(wanted) => wanted.into(), + } + } + + /// The value as TOML writes it. + fn literal(self) -> String { + match self { + Self::Str(wanted) => format!("\"{wanted}\""), + Self::Bool(wanted) => wanted.to_string(), + } + } +} + +impl TomlFile { + /// The setting `key`, with its line. + fn setting(&self, key: &str) -> Option<(usize, &toml::Value)> { + self.value + .iter() + .find(|(name, _)| name.get_ref() == key) + .map(|(name, value)| (self.line(name), value)) + } + + /// Each of `required` the file doesn't set to its value, fixed by + /// setting it. + pub(super) fn settings_are(&self, required: &[(&'static str, Value)]) -> Checked { + let mut checked = Checked::default(); + for &(key, wanted) in required { + let setting = format!("{key} = {}", wanted.literal()); + let (line, message) = match self.setting(key) { + Some((_, value)) if wanted.is(value) => continue, + Some((line, _)) => (line, format!("`{key}` has another value: set `{setting}`")), + None => (1, format!("`{key}` is unset: add `{setting}`")), + }; + checked.findings.push(Finding { + path: self.path.clone(), + line, + message, + }); + checked.fixes.push(Fix::new( + self.path.clone(), + format!("set `{setting}`"), + move |document| { + let new = wanted.toml(); + if document.contains_key(key) { + Fix::set(&mut document[key], new); + } else { + document[key] = toml_edit::Item::Value(new); + } + }, + )); + } + checked + } +} diff --git a/crates/sourceorigin-standard/src/rules/tests/mod.rs b/crates/sourceorigin-standard/src/rules/tests/mod.rs index a1f95e8..1002452 100644 --- a/crates/sourceorigin-standard/src/rules/tests/mod.rs +++ b/crates/sourceorigin-standard/src/rules/tests/mod.rs @@ -11,6 +11,7 @@ mod framework; mod generated_lints; mod layout; mod report; +mod rustfmt_config; mod toolchain; mod unsafe_modules; mod workspace_lints; @@ -106,7 +107,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); 2] = [ +const CONFIG: [(&str, &str); 3] = [ ( ".cargo/config.toml", "[alias]\nxtask = \"run --locked --package xtask --\"\n", @@ -115,6 +116,10 @@ const CONFIG: [(&str, &str); 2] = [ "rust-toolchain.toml", "[toolchain]\nchannel = \"stable\"\ncomponents = [\"rustfmt\", \"clippy\"]\n", ), + ( + "rustfmt.toml", + "edition = \"2024\"\nnewline_style = \"Unix\"\nuse_field_init_shorthand = true\n", + ), ]; /// A manifest tail inheriting the workspace's lints. diff --git a/crates/sourceorigin-standard/src/rules/tests/rustfmt_config.rs b/crates/sourceorigin-standard/src/rules/tests/rustfmt_config.rs new file mode 100644 index 0000000..9f8747b --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/tests/rustfmt_config.rs @@ -0,0 +1,38 @@ +use super::{Fixture, INHERITS}; +use crate::rules::Rule; + +#[test] +fn rustfmt_sets_the_standard_settings() { + let fixture = Fixture::new(); + fixture.add_crate("core", INHERITS).write( + "rustfmt.toml", + "edition = \"2021\"\nnewline_style = \"Unix\"\nmax_width = 120\n", + ); + assert_eq!( + fixture.violations(Rule::RustfmtConfig), + [ + "rustfmt.toml:1: `edition` has another value: set `edition = \"2024\"` \ + (rustfmt-config)", + "rustfmt.toml:1: `use_field_init_shorthand` is unset: add \ + `use_field_init_shorthand = true` (rustfmt-config)", + ] + ); + fixture.fix_rule(Rule::RustfmtConfig); + assert_eq!( + fixture.violations(Rule::RustfmtConfig), + Vec::::new() + ); +} + +#[test] +fn fix_writes_a_missing_rustfmt_toml() { + let fixture = Fixture::new(); + fixture + .add_crate("core", INHERITS) + .remove("rustfmt.toml") + .fix_rule(Rule::RustfmtConfig); + assert_eq!( + fixture.violations(Rule::RustfmtConfig), + Vec::::new() + ); +} diff --git a/crates/sourceorigin-standard/src/rules/toml_file.rs b/crates/sourceorigin-standard/src/rules/toml_file.rs index 5f2af8f..7dd5b68 100644 --- a/crates/sourceorigin-standard/src/rules/toml_file.rs +++ b/crates/sourceorigin-standard/src/rules/toml_file.rs @@ -28,6 +28,20 @@ impl TomlFile { } } + /// The file at `path`, from `root`, or an empty one when there is + /// none, which a fix writes. + pub(super) fn load_or_empty(root: &Path, path: impl Into) -> Result + where + T: Default, + { + let path = path.into(); + Ok(Self::load(root, &path)?.unwrap_or_else(|| Self { + path, + text: String::new(), + value: T::default(), + })) + } + /// The file at `path`, from `root`, which must exist. pub(super) fn require(root: &Path, path: impl Into) -> Result { let path = path.into();