rint.git / README.md
README.mdpreviewREADME.mdsource188 lines · 11.8 kB · raw
1# rint
2
3A linter, written in Rust, that stops Rust code from eroding one change at a time. Every change
4looks fine on its own; the damage is the slope. rint scores a whole tree and, given a git base,
5fails when a score gets **worse**, rather than enforcing a fixed limit that can be sat under forever
6(complexity 9 passes a limit of 10 every time; it cannot pass a ratchet).
7
8The signals come from SlopCodeBench (arXiv 2603.24755). Test code is never scored as product code.
9`HANDOFF.md` has the research and the decisions this is built on.
10
11## Use
12
13Everything runs through mise. It installs buck2, reindeer and rust-script; the nix devshell adds the
14C compiler mise cannot bring, so on a bare machine prefix each command with `nix develop -c`.
15
16```
17mise trust && mise install        the toolchain
18mise tasks                        what can be run
19mise run test                     every test, through buck2
20mise run rint -- check            score this tree (or: -- check --path ~/some-repo)
21mise run rint -- diff HEAD~5      fail if anything got worse since HEAD~5
22mise run dogfood                  rint on its own source
23```
24
25The binary takes the same arguments directly (`buck2 build //crates/rint-cli:rint`, or `cargo rint`
26once `cargo-rint` is on `PATH`):
27
28```
29rint check                   score the tree; a report, exit 0 unless rint.toml sets deny or limits
30rint check --base <rev>      the same, then compare with the git revision
31rint diff <rev>              the comparison only
32rint erosion | concentration | clones | wrappers | single-use | dispatch
33     | allows | tests | branches | casts | lets | graph        one signal each
34global flags:  -C <dir>  --config <file>  --format text|json
35```
36
37Exit codes: **0** clean, **1** a regression or a violation, **2** a usage or internal error. The base
38revision is read with git without checking it out, so a dirty work tree is fine.
39
40## Install
41
42rint is released for Linux (x86_64 and aarch64, static musl), macOS (Apple silicon and Intel) and Windows
43(x86_64) as signed packslip archives, each holding `rint` and `cargo-rint`. In a repository's `mise.toml`:
44
45```toml
46[tools]
47"packslip:github.com/jevgineer/rint" = { version = "0.1.0", pubkey = "RWRBRJ8J7sFC5gpZdjHhDlYlF4P0YsYpi1c5AwnlNsoSYiYyCCR3Alnq", allow_unlogged = true }
48```
49
50`allow_unlogged` is needed because the release is signed with a key and not recorded in a public
51transparency log; `pubkey` is `tools/release.pub`. `mise run release:build` and
52`mise run release:sign` make a release, and `mise run release:refresh` signs the list of releases again
53before it expires; none publishes anything.
54
55The repository is public, but GitHub answers 404 to everyone except the jevgineer account for
56every jevgineer repository (measured 2026-11-02: `api.github.com/repos/jevgineer/rint` is 404
57anonymously and with another account's token, 200 with jevgineer's). Until that lifts, mise
58needs that account's token in `GITHUB_TOKEN` to find the release.
59
60The same release is served without a token by the code host, which reads GitHub with its own
61token: `"packslip:code.lmjtfy.fun/rint"` in place of `"packslip:github.com/jevgineer/rint"`,
62same version and `pubkey` (the list is `https://code.lmjtfy.fun/.well-known/packslip/rint.json`).
63Only pinned versions resolve there while the account is hidden: GitHub lists the account's
64releases as empty, so `latest` is 404. And the hidden account's API quota is 60 requests an hour
65(measured 2026-11-07), of which the code host uses at most 30; when they are spent it answers
66`502 GitHub did not answer` for every release file until the hour's reset.
67
68## What it measures
69
70Complexity is computed twice, **cyclomatic** (the paper's measure, threshold 10) and **cognitive**
71(SonarSource's, threshold 15), and every score is reported and ratcheted separately for each.
72
73| Signal | What it finds | Ratchet |
74|---|---|---|
75| erosion | share of complexity mass (`complexity * sqrt(sloc)`) in functions above the threshold | rise beyond tolerance |
76| concentration | Gini of mass across functions, and the top-20% share | rise beyond tolerance |
77| function growth | a function's cyclomatic, cognitive or nesting grew above a floor, or a new one starts above the threshold | any |
78| clones | normalised-token windows repeated across functions | clone ratio rises |
79| wrappers | private fns whose whole body forwards their arguments to one call | any new |
80| single-use | private fns with exactly one call site (name-based); a fn on a call cycle, directly or through other fns, is skipped | any new, as a **warning** |
81| dispatch | `match` / `if` chains comparing one value to four or more string literals | any new |
82| branches | sibling `match` arms / `if` branches with token-identical bodies (names and literals kept, so mergeable with `\|`); bodies differing only in a name belong to `clones` | any new |
83| allows | `#[allow(..)]` attributes, except those whose lints are all in `[allows] permitted` | any new |
84| casts | `.clone()` or `as` heavy functions, `Rc<RefCell<..>>`, `Box<dyn Any>` | any new |
85| lets | `let x = ..;` read once, in the very next statement | any new, as a **warning** |
86| graph | module cycles and propagation cost per crate (`use` edges) | new cycle, cost rises |
87| tests | test inventory; public functions no test mentions | informational, never fails |
88
89Test code is a `#[cfg(test)]` item, a `#[test]` / `#[bench]` / `#[tokio::test]` function, or any file
90under `tests/`, `benches/`, `examples/` (configurable). It is read only to count references to
91product names.
92
93**Flat matches.** A `match` with no catch-all arm (`_` or a lowercase binding), no guards, and no
94branching inside any arm counts 1 in cyclomatic, not one per arm: the "modified cyclomatic complexity"
95of Lizard (`-m`) and SciTools Understand. Without a catch-all the compiler checks exhaustiveness, so
96adding a variant to a data enum is not new logic. Cognitive already scores any `match` as +1. A match
97that fails the test counts per arm as before; `[complexity] flat_match = "per_arm"` turns this off.
98
99**Warnings.** `single-use` and `lets` are reported by `check` and `diff` (text and JSON both carry a
100`severity` of `warning`) but never fail the ratchet or a plain check. rint judges them by shape alone: a
101`let` can be required by the borrow checker (E0499, E0502) and syn cannot see that, and single-use
102helpers are often exactly what the growth gate asked a long function to be split into.
103
104## Configuration
105
106`rint.toml` at the scanned root (or `--config`). Every threshold, with its default, is listed in this
107repository's own `rint.toml`. Unknown keys are an error. The numbers are starting guesses.
108
109- `deny = [..]` names signals whose findings fail a plain `check`; it also makes a warning signal an
110  error again, so `deny = ["lets"]` opts a repository back in.
111- `warn = [..]` is the list of warning signals, default `["single-use", "lets"]`; `warn = []` makes
112  every signal an error.
113- `[allows] permitted = ["refining_impl_trait", ..]`: an `#[allow(..)]` whose lints are all listed is
114  not a finding. Names match exactly as written, so `clippy::too_many_arguments` and
115  `too_many_arguments` are separate entries.
116- `[complexity] flat_match = "one" | "per_arm"`, default `"one"`.
117
118## Clippy preset
119
120`clippy-preset/` ships a `clippy.toml` and a lint list for what Clippy can enforce per function, so
121rint does not reimplement it. See [clippy-preset/](clippy-preset/README.md).
122
123## First runs
124
125Read-only runs of the binary built from this tree, 2026-11-01 (rint reads files and `git show`; it
126wrote nothing in either repository). `check` is the plain report; `diff HEAD~5` is the ratchet against
127five commits back, and exits 1 in both repositories because both grew.
128
129| | `~/bootspan` | `~/jevboard` | `~/rint` (itself) |
130|---|---|---|---|
131| functions / sloc / files | 134 / 1,259 / 12 | 3,528 / 36,768 / 330 | 167 / 2,022 / 25 |
132| erosion, cyclomatic / cognitive | 0.000 / 0.000 | 0.349 / 0.450 | 0.280 / 0.266 |
133| high functions (above 10 / above 15) | 0 / 0 | 119 / 80 | 8 / 4 |
134| largest function | 9 / 7 | 60 / 82 | 16 / 31 |
135| Gini of mass, cyclomatic / cognitive | 0.490 / 0.716 | 0.676 / 0.857 | 0.607 / 0.765 |
136| top-20% share | 0.530 / 0.731 | 0.726 / 0.895 | 0.660 / 0.793 |
137| clone ratio | 0.011 | 0.062 (238 regions) | 0.011 |
138| wrappers / single-use / dispatch | 4 / 35 / 0 | 21 / 341 / 15 | 0 / 39 / 0 |
139| branches / allows / casts / lets | 1 / 0 / 0 / 42 | 93 / 12 / 60 / 1,165 | 8 / 0 / 0 / 63 |
140| module cycles | 0 | 17 | 0 |
141
142**What the ratchet found.** In bootspan, `diff HEAD~5` fails on three new single-use helpers
143(`number_json`, `text_json`, `container_json`), while erosion fell from 0.069 to 0.000 over the same
144five commits: the shape of a function being split up. In jevboard it fails on 54 items: ten functions
145that grew (`resolve` in `packages/node/src/entities.rs` went from cyclomatic 8 to 18; `Because::fmt` 13
146to 15), four new functions that start above the threshold (`Facts::join_said`, cognitive 28), a new
147`#[allow(clippy::too_many_arguments)]`, a new five-arm string `match` in `packages/records`, a
148function with eight `.clone()` calls, two new module cycles in `packages/config` and `packages/prefs`,
149the `prefs` propagation cost rising 0.654 to 0.680, and 27 single-use `let`s.
150
151**On itself.** Both complexity measures concentrate in a few functions (`Walker::visit_expr`,
152cyclomatic 16; `signals::clones::find`; `signals::scc::components`, cognitive 31), and the exec and
153collect modules have several two-arm matches with equal bodies. Those are real findings and are left
154as found; `rint diff` on this repository is the check that they do not get worse.
155
156**How much to trust it.** The thresholds are untuned guesses, and these runs show which signals are
157noisy. `lets` (1,165 in jevboard) and `single-use` (341) fire on a great deal of ordinary, readable
158code, so as plain counts they are not informative; they are useful only as the ratchet's "new since the
159base". The module-cycle finding counts a parent module and its child importing from each other, which
160is common in Rust and real, but a lower-severity cycle than two siblings. Two defects were found and
161fixed by these runs: files pulled in by `#[cfg(test)] mod tests;` (also with `#[path]`) were being
162scored as product code, and an unchanged erosion printed as `-0.000`.
163
164## Layout
165
166| Path | What |
167|---|---|
168| `crates/rint-core` | the engine: parse with syn, measure, score, compare |
169| `crates/rint-cli` | the command line (library `rint_cli` and the `rint` binary) |
170| `crates/cargo-rint` | the `cargo rint` forwarder |
171| `clippy-preset/` | `clippy.toml` and the recommended deny list |
172| `rint.toml` | rint's own configuration |
173| `tools/` | repository scripts (rust-script) |
174| `platforms/`, `toolchains/`, `third-party/`, `.buckconfig` | buck2 build setup, shared with the estate's other repositories |
175| `HANDOFF.md` | the research and decisions |
176
177## Outstanding
178
179- **dylint lints** (a later, optional phase; not built): the checks that need types, such as a
180  `.clone()` that exists only to satisfy the borrow checker, a `Result` that can never be `Err`, a
181  needless `as` cast. They need a pinned nightly and compiling code, which is why the syn core does
182  not try.
183- **Dependency entropy**, the paper's third graph metric: only cycles and propagation cost are built.
184- **Thresholds are untuned.** They are guesses; tuning needs real before/after pairs of agent-written
185  changes, which this repository does not have.
186- **Extra defensive checks** (a `Result`/`Option` that is never `Err`/`None`) need types: dylint.
187- **Name-based approximations**: single-use, the untested-public list and recursion detection match
188  by name, because syn does no name resolution. Names defined more than once are skipped.