diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index 8f17679..3d795c8 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -40,6 +40,7 @@ A repository adopts the standard in four steps: - `childless-mod-rs`: A module is `foo/mod.rs` only when its directory holds another Rust file; otherwise it is `foo.rs`. A `mod.rs` beside an integration test's, benchmark's or example's root, such as `tests/common/mod.rs`, passes. - `inline-tests`: An inline `#[cfg(test)] mod … { }` (or one whose `cfg` requires a feature named for tests) at the top level of a file a target reaches outside test code spans at most 30% of the file's lines; a larger one is a file of its own, `mod tests;`. - `inline-limits`: `Duration::from_*` with a number, or arithmetic on numbers, sits only in a `const` or `static` item, a `const fn` or test code: a limit, timeout or size is a named constant. Test code is what a test attribute such as `#[tokio::test]`, or a `cfg` requiring `test` or a feature named for tests (`test-util`), marks, the files such a module declares, and integration tests and benchmarks. +- `no-json-macro`: Code outside tests, in the crates `standard.toml`'s `no-json-macro` lists, invokes no `json!`: it serializes structs. Each name there is a workspace crate. Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and @@ -64,6 +65,10 @@ other-rule = { reason = "why", paths = ["crates/generated/**"], crates = ["vendo # `build_script_build` for a build script. unsafe-modules = ["my_crate::ffi"] +# The crates whose code outside tests serializes structs instead of invoking +# `json!`. +no-json-macro = ["my_protocol"] + [comments] # The most lines of prose an item's or module's docs may span, code blocks # and a crate root's `//!` docs aside. diff --git a/crates/sourceorigin-standard/src/config/mod.rs b/crates/sourceorigin-standard/src/config/mod.rs index 98a3d8e..01a6816 100644 --- a/crates/sourceorigin-standard/src/config/mod.rs +++ b/crates/sourceorigin-standard/src/config/mod.rs @@ -74,6 +74,8 @@ pub(crate) struct Config { pub(crate) check_changed: CheckChanged, /// The modules that may expect `unsafe_code`, such as `my_crate::ffi`. pub(crate) unsafe_modules: Vec, + /// The crates whose code outside tests uses no `json!`. + pub(crate) no_json_macro: Vec, /// The `[comments]` table. pub(crate) comments: Comments, } @@ -223,6 +225,7 @@ struct File { deviations: BTreeMap, Written>, check_changed: CheckChanged, unsafe_modules: Vec>, + no_json_macro: Vec>, comments: Comments, } @@ -351,6 +354,7 @@ impl Config { deviations, check_changed: file.check_changed, unsafe_modules: listed(file.unsafe_modules), + no_json_macro: listed(file.no_json_macro), comments: file.comments, }) } diff --git a/crates/sourceorigin-standard/src/rules/mod.rs b/crates/sourceorigin-standard/src/rules/mod.rs index 0165842..40d3e99 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -14,6 +14,7 @@ mod inline_limits; mod inline_tests; mod level; mod modules; +mod no_json_macro; mod report; mod source; mod test_code; @@ -136,6 +137,10 @@ rules! { /// feature named for tests (`test-util`), marks, the files such a /// module declares, and integration tests and benchmarks. InlineLimits = "inline-limits" => inline_limits, + /// Code outside tests, in the crates `standard.toml`'s `no-json-macro` + /// lists, invokes no `json!`: it serializes structs. Each name there is + /// a workspace crate. + NoJsonMacro = "no-json-macro" => no_json_macro, } /// Why the rules couldn't be checked. diff --git a/crates/sourceorigin-standard/src/rules/no_json_macro.rs b/crates/sourceorigin-standard/src/rules/no_json_macro.rs new file mode 100644 index 0000000..aaa30ec --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/no_json_macro.rs @@ -0,0 +1,67 @@ +//! `no-json-macro`: the crates `standard.toml` lists build JSON from typed +//! structs, never `json!`. + +use std::path::Path; + +use super::walk::{Inspect, Scope, Walk}; +use super::{Finding, Repository}; +use crate::config; + +/// The `json!` invocations outside test code in one file. +struct JsonMacros<'a> { + path: &'a Path, + findings: Vec, +} + +impl Inspect for JsonMacros<'_> { + fn macro_call(&mut self, call: &syn::Macro, scope: Scope) { + let Some(last) = call.path.segments.last() else { + return; + }; + if scope.test || last.ident != "json" { + return; + } + self.findings.push(Finding { + path: self.path.to_owned(), + line: last.ident.span().start().line, + message: "`json!` builds a document no type checks: serialize a struct that \ + describes it" + .to_owned(), + }); + } +} + +impl Repository { + /// Each `json!` outside test code in a crate `no-json-macro` lists, and + /// each listed name that is no workspace crate. + pub(super) fn no_json_macro(&self) -> Vec { + let listed = &self.config.no_json_macro; + let mut findings: Vec = listed + .iter() + .filter(|entry| !self.metadata.has(&entry.value)) + .map(|entry| Finding { + path: config::PATH.into(), + line: entry.line, + message: format!( + "`{}` is in `no-json-macro`, but is no workspace crate: remove it", + entry.value + ), + }) + .collect(); + for source in &self.sources { + let in_listed = self + .crate_of(&source.path) + .is_some_and(|krate| listed.iter().any(|entry| entry.value == krate)); + if !in_listed { + continue; + } + let mut macros = JsonMacros { + path: &source.path, + findings: Vec::new(), + }; + Walk::file(source, self.modules.is_test(&source.path), &mut macros); + findings.extend(macros.findings); + } + findings + } +} diff --git a/crates/sourceorigin-standard/src/rules/tests/mod.rs b/crates/sourceorigin-standard/src/rules/tests/mod.rs index 1e5cb83..51de029 100644 --- a/crates/sourceorigin-standard/src/rules/tests/mod.rs +++ b/crates/sourceorigin-standard/src/rules/tests/mod.rs @@ -9,6 +9,7 @@ mod framework; mod generated_lints; mod inline_limits; mod inline_tests; +mod no_json_macro; mod report; mod unsafe_modules; mod workspace_lints; diff --git a/crates/sourceorigin-standard/src/rules/tests/no_json_macro.rs b/crates/sourceorigin-standard/src/rules/tests/no_json_macro.rs new file mode 100644 index 0000000..d6176fb --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/tests/no_json_macro.rs @@ -0,0 +1,36 @@ +use super::{Fixture, INHERITS}; +use crate::rules::Rule; + +#[test] +fn listed_crates_use_json_only_in_tests() { + let fixture = Fixture::new(); + fixture + .add_crate("typed", INHERITS) + .add_crate("free", INHERITS) + .write( + "crates/typed/src/lib.rs", + "fn body() -> Value {\n json!({ \"error\": \"invalid\" })\n}\n\ + fn log() {\n println!(\"{}\", serde_json::json!(1));\n}\n\ + #[test]\nfn builds() {\n let _ = json!({});\n}\n", + ) + .write( + "crates/free/src/lib.rs", + "fn body() -> Value {\n json!({})\n}\n", + ) + .write( + "standard.toml", + "no-json-macro = [\n \"typed\",\n \"missing\",\n]\n", + ); + let message = "`json!` builds a document no type checks: serialize a struct that describes \ + it (no-json-macro)"; + assert_eq!( + fixture.violations(Rule::NoJsonMacro), + [ + format!("crates/typed/src/lib.rs:2: {message}"), + format!("crates/typed/src/lib.rs:5: {message}"), + "standard.toml:3: `missing` is in `no-json-macro`, but is no workspace crate: remove \ + it (no-json-macro)" + .to_owned(), + ] + ); +}