Skip to content

crates style

bondiano edited this page Nov 12, 2026 · 1 revision

Rust house style

Active contributors: Vassiliy, bondiano

Purpose

sych-style checks one repository convention that rustfmt and Clippy do not express: a standalone if followed by another statement needs a separating blank line. It parses Rust with syn rather than matching source text. The rule and fixer are in crates/sych-style/src/lib.rs; this is a development tool, not the reviewer’s pinned linter integration.

AST rule

The IfEnds visitor inspects adjacent statement pairs in every parsed block. It checks the first statement only when it is Stmt::Expr(Expr::If(..), ..), finds the full expression’s ending line, and reports that 1-based closing line if:

  • the immediate next source line is not empty after trimming whitespace; and
  • the following statement does not begin on the same line.

This separates a complete if/else if/else chain from whatever follows it. Nested blocks are visited recursively. An if let is still an Expr::If and follows the same rule (crates/sych-style/src/lib.rs).

if missing {
    return Err(err);
}

let value = read()?;

There is no violation when the if is the last statement before the enclosing }, when a blank line already follows it, or between branches of one else chain. An expression inside let value = if … is not a standalone if statement. Statements sharing a source line are explicitly exempt. A comment immediately on the next line is not a blank line, so separation is required before that comment when another statement follows.

check_source parses a whole file, visits it, and sorts/deduplicates violation line numbers. Invalid Rust is a parse error, not a skipped file. This is syntax inspection; it does not run the crate or expand macros (crates/sych-style/src/lib.rs).

Fix and file traversal

fix_source(source, lines) inserts a newline after each reported line without changing the existing text. run(roots, fix) discovers files, sorts their paths, reads/parses them, and either returns Violation { path, line } values or writes the fix.

Directory roots are traversed recursively for .rs files, skipping directories named target and .git. An explicit file root is read directly. Filesystem/read/parse/write errors include the path and stop the operation; the runner does not collect every error before failing. Earlier files may already have been fixed, so this is not an all-files transaction (crates/sych-style/src/lib.rs).

The binary in crates/sych-style/bin/sych-style.rs recognizes --fix; other arguments are roots, and no roots defaults to crates. Its exit codes are:

Code Meaning
0 No violations, or requested fixes completed
1 Check mode found violations; prints path, line, rule name and count
2 Runner error such as unreadable or unparsable input

Successful fixes are not returned as remaining violations. The library does not invoke rustfmt; just style-fix runs the fixer and then cargo fmt --all (justfile).

Running and changing the rule

# Check only this rule
cargo run --locked -p sych-style -- crates

# Insert separators, then run rustfmt
just style-fix

# Run the inline rule tests
cargo test --locked -p sych-style

just lint runs formatting checks, cargo-machete and Clippy before this checker; just check includes that lint recipe (justfile).

For a rule change, start with IfEnds::visit_block and check_source; add passing and failing examples to the inline tests, then verify fix_source makes the failing examples clean. Keep CLI diagnostics and exit behavior in the thin binary. Span line positions depend on proc-macro2’s span-locations feature in crates/sych-style/Cargo.toml; its cargo-machete exception is intentional.

Key source files

File Role
crates/sych-style/src/lib.rs AST visitor, check/fix helpers, traversal and inline tests
crates/sych-style/bin/sych-style.rs Argument handling, diagnostics and exit codes
crates/sych-style/Cargo.toml Binary path, parser and span-location dependencies
crates/sych-style/README.md Rule examples and commands
justfile Lint and fix integration

Scope and maintenance snapshot

Scope: tracked crates/sych-style/ files at revision 00dc7637f89a47ee6fd4713204f8331418428370. There are 4 files, including 2 Rust implementation files and no separate integration-test files. 1/2 (50.0%) Rust files contains a #[test] or #[tokio::test…] attribute: the library’s inline tests. This test-bearing-file ratio is not line coverage; the shared justfile is outside the count.

All-history git log origin/main --format='%an' -- crates/sych-style yields 2 distinct nonbot author display names, deduplicated exactly without alias merging or co-author trailers. The byline shows the most recent distinct names as first names/usernames, not an ownership assignment.

See Patterns and conventions for the wider Rust check policy.

Clone this wiki locally