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

Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and
Expand All @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions crates/sourceorigin-standard/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Listed>,
/// The crates whose code outside tests uses no `json!`.
pub(crate) no_json_macro: Vec<Listed>,
/// The `[comments]` table.
pub(crate) comments: Comments,
}
Expand Down Expand Up @@ -223,6 +225,7 @@ struct File {
deviations: BTreeMap<Spanned<Rule>, Written>,
check_changed: CheckChanged,
unsafe_modules: Vec<Spanned<String>>,
no_json_macro: Vec<Spanned<String>>,
comments: Comments,
}

Expand Down Expand Up @@ -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,
})
}
Expand Down
5 changes: 5 additions & 0 deletions crates/sourceorigin-standard/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
67 changes: 67 additions & 0 deletions crates/sourceorigin-standard/src/rules/no_json_macro.rs
Original file line number Diff line number Diff line change
@@ -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<Finding>,
}

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<Finding> {
let listed = &self.config.no_json_macro;
let mut findings: Vec<Finding> = 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
}
}
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/src/rules/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions crates/sourceorigin-standard/src/rules/tests/no_json_macro.rs
Original file line number Diff line number Diff line change
@@ -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(),
]
);
}