Skip to content
Cintu07Public

About

finds the code that disagrees with the rest of your Go codebase. Infers lock and nil-check rules from the code itself, engler-style.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

heresy

heresy

heresy reads a Go codebase, works out the rules its code already follows, and reports the few places that break them. You write no rules. If 346 functions reach Unlock() on every path after Lock(), the one early return that skips it is the bug.

$ heresy ./minio
heresy ./minio: 655 files, 94 beliefs inferred, 1 broken at 1 sites (245ms)

every path after Lock() reaches Unlock()
  kept at 346 of 347 sites, z=+6.0
  cmd/batch-handlers.go:966  returns with ri.mu held (Lock at line 942)

That finding is real. updateAfter takes ri.mu, switches on the job type, and the default: branch returns errInvalidArgument with the lock still held. BatchJobRequest.Type() returns "unknown" for a job with none of its three kinds set, and from then on every call that takes ri.mu blocks forever. Validation rejects such a job earlier and minio is archived, so it is latent. It is also the shape of bug that review keeps missing: 346 siblings got it right and one path forgot.

Install

go install github.com/Cintu07/heresy@latest
heresy ./path/to/repo

Standard library only. It exits 1 when a belief breaks, so it drops into CI as is.

How it works

The method is Engler et al., Bugs as Deviant Behavior (SOSP 2001). Code states its rules by repeating them. Count how often a rule holds, and the exceptions to one kept hundreds of times are almost always mistakes.

Locks. heresy learns acquire/release pairs from the corpus itself. A method counts as a release when the code defers it after the acquire, which is how Lock/Unlock, RLock/RUnlock and Start/Stop show up with no list to maintain. For each acquire it walks the paths that follow, through branches, loops, switches and selects, and flags a return, a continue, or the end of a function reached with the lock held.

Nil checks. For every v := f(), heresy records whether v meets a nil comparison or a dereference first. When nearly every caller of f checks first, the caller that dereferences first gets flagged.

Each belief is scored by how far its agreement rate sits above 90%, in standard errors. A rule kept 1,194 times and broken once outranks one kept 6 times and broken once, and anything under z=2 stays quiet.

False positives it already handles

Every row is a case an earlier version flagged in a real repo and got wrong. Each has a regression test in testdata/.

Case Found in
Relock in a function that deferred the unlock, or was called holding the lock nats-server raft.go, jetstream_cluster.go, client.go
Loop body that releases before it acquires nats-server client.go
Returning the locked value to the caller prometheus tsdb/head_append.go
Release through a method value, unlock := mu.RUnlock vault builtin/credential/approle
Loop that locks a different value each iteration syncthing lib/rc
Helper that unlocks for its caller, closeLocked() vitess tabletserver/schema
Nil-safe method that checks its own receiver, len(v) as the guard nats-server
Nil check that lives in the caller minio metrics-v2.go, now under z=2

Results

Shallow clones from 2026-09-28, default flags, _test.go files excluded.

Repo Files Beliefs Broken
vitess 1978 185 0
gitea 2085 152 0
vault 1554 135 0
consul 1559 108 0
minio 655 94 1
go-ethereum 973 92 0
tailscale 1521 87 0
nomad 1328 81 0
containerd 932 80 0
nats-server 128 49 0
prometheus 454 34 0
etcd 686 29 0
syncthing 407 21 0
caddy 221 20 0
traefik 499 18 0
moby 310 2 0

15,290 files and 1,187 beliefs, one finding, and the finding is a real bug. Each repo takes about a second.

Flags

-min 5      sites that must keep a rule before it counts
-rate 0.9   share of sites that must keep it
-z 2        hide broken rules scoring below this
-tests      include _test.go files
-json       print the report as JSON
-top 20     print at most this many broken rules

Limits

heresy reads syntax only, so it runs on any checkout without building it or fetching a module. The price is that methods are keyed by name, and two unrelated types with a Lock() share statistics. Paths are followed inside one function. When a lock goes somewhere heresy cannot see, the walk stops, and the site counts for neither side. The nil checker is the weaker of the two, and most of what it finds lands below z=2.

About

finds the code that disagrees with the rest of your Go codebase. Infers lock and nil-check rules from the code itself, engler-style.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages