Find what the type checker cannot: lemmas whose names say the opposite of their statements, docstrings that describe the wrong property or point at declarations that no longer exist, deprecations that lead to other deprecations, things stated twice, and private code nothing uses.
Lean checks that every proof is correct. It does not check that foo_left is about the left,
that a lemma called _iff is an ↔, or that the name in a docstring still exists. Those mistakes
compile, pass CI, and send the next person looking for a lemma to the wrong one. lean-linter
reads a compiled library and reports them.
It works on any Lean 4 project, depends only on Lean, and runs on all of Mathlib (330,000 declarations) in about six minutes.
Run on Mathlib and on Lean's own library in October 2026, it found, among others:
HoCat.homEquivLeft_symm_applystated a fact abouthomEquivRight: a copy of the lemma above it that kept its statement, so the simp lemma forhomEquivLeftdid not exist.AntitoneOn.monotoneconcludesAntitone, andPresheafOfModules.mono_iff_surjectivestates thatfis mono iff it is injective on every object.DFinsupp.coe_strictMonoandFinsupp.coe_strictMonoare copies ofcoe_mono. They conclude plainMonotone, so the strict lemma their names promise does not exist.reflexivePair_map_leftandreflexivePair_map_righthave their names swapped.- Three instances in the
CommMonCatnamespace state that the forgetful functor ofMonCatcreates limits. They were copied fromMonCat, so theCommMonCatinstances are missing. - In Lean core,
Std.Roc.lower_le_of_mem(and itsRoo,Roitwins) concludesr.lower < a, andList.findIdx?_eq_fst_find?_zipIdxuses.2, the second component.
In a Lake project, add the dependency to lakefile.toml:
[[require]]
name = "lean-linter"
git = "https://github.com/keithadler/lean-linter"
rev = "v0.5.4"Then build your library and lint it:
lake build
lake exe lean-linter MyLibraryTo lint a project you don't want to change, Mathlib say, build lean-linter with the same Lean
version as the project, then run it from the project's directory so it finds the project's
.olean files:
git clone https://github.com/keithadler/lean-linter && cd lean-linter
cp ../mathlib4/lean-toolchain . && lake build
cd ../mathlib4 && lake env ../lean-linter/.lake/build/bin/lean-linter MathlibOne finding per line: check, declaration, Module:line, and what is wrong.
mislabel Fixtures.fooLeft_eq LeanLinterTest.Fixtures:13 the name says `Fixtures.fooLeft` but the statement uses `Fixtures.fooRight` and never `fooLeft`
name-shape Fixtures.le_self_succ_lt LeanLinterTest.Fixtures:32 the name ends in `_lt` (strict), but the conclusion is a `≤`
doc-refs Fixtures.docref_demo LeanLinterTest.Fixtures:44 the docstring mentions `Fixtures.no_such_lemma_here`, which does not exist
deprecation Fixtures.dep_old LeanLinterTest.Fixtures:51 deprecated in favor of `Fixtures.dep_mid`, which is itself deprecated
The real output separates fields with tabs. A summary with one count per check goes to
standard error, so lean-linter MyLib > findings.tsv keeps the two apart. With --json, each
finding is one JSON object per line instead:
{"check":"deprecation","declaration":"Fixtures.dep_old","line":51,"message":"deprecated in favor of `Fixtures.dep_mid`, which is itself deprecated","module":"LeanLinterTest.Fixtures"}| Check | Reports | Example |
|---|---|---|
mislabel |
The name names a constant, but the statement uses only its opposite (Left/Right, fst/snd, inl/inr, Monotone/Antitone, map/comap, ...). |
fooLeft_eq : fooRight n = n + 1 |
namespace |
In the namespace of a type, but stated about a sibling of that type (UInt8/UInt16, WithTop/WithBot, CommMonCat/MonCat). |
Box16.x_self (b : Box8) : ... |
name-shape |
The last word of the name promises a conclusion the statement does not have: _iff with no ↔, _lt concluding ≤, _le concluding <, _injective concluding Surjective. |
F_injective : Function.Surjective F |
doc-mislabel |
The docstring names one property of a family ("injective", "monotone", "open"), the conclusion another. | "F is injective." on a surjectivity lemma |
doc-refs |
A name in backticks in a docstring matches no declaration, module, tactic, attribute or option. | `Nat.not_a_lemma` after a rename |
deprecation |
Deprecated in favor of a declaration that does not exist or is itself deprecated. Lean already warns about this when the alias is declared, so what this finds are the places someone silenced that warning on purpose (set_option linter.deprecated.deprecatedTarget false), usually in the middle of a rename. |
old → mid → new, with mid deprecated |
duplicate |
Two theorems with the same statement (up to the names of variables and universes), each proved without the other. | the same lemma proved in two files |
duplicate-sym |
The same, with one stated backwards: a = b against b = a. |
x + 0 = x and x = x + 0 |
duplicate-instance |
Two instances of the same type. | two Pointed Nat instances |
dead-private |
A private theorem or definition that nothing refers to. | a helper left behind by a refactor |
vacuous |
A statement that says nothing: conclusion True, a hypothesis False, or two hypotheses p and ¬p. Both sides of an = being the same term is reported too, since it is usually an abbreviation-unfolding lemma and sometimes a lemma that proves nothing. |
foo (h : p) (h' : ¬p) : 1 = 2 |
universe |
A public theorem or definition with a binder α : Type and no universe parameters, so it cannot be used for a type in a higher universe. A file with eight or more is reported once. |
{R : Type} where its neighbors say Type* |
op-mismatch (experimental) |
The name says add, mul, sub, div, sum or prod, but the statement uses none of it and uses a confusable one instead. Mathlib's naming is loose enough that most hits are legitimate. |
foo_mul_comm : a + b = b + a |
doc-iff (experimental) |
The docstring says "if and only if", but the statement is not an ↔. Many hits are docstrings written for the iff version of a lemma that give one direction. |
"n is positive iff nonzero" on 0 < n → n ≠ 0 |
trust |
What the declaration asks you to believe besides Lean's kernel: sorry, a user axiom, native_decide, unsafe, implemented_by, extern, an opaque constant. |
theorem t : 3 + 3 = 6 := sorry |
diamond |
Two instances of one type that are not definitionally equal, so instance search picks one by import order. | two Wit Bool instances giving different values |
proof-clone |
Two theorems with different statements and names, whose compiled proofs apply the same five or more lemmas in the same order: an argument copied and edited, for Array and for Vector, or for a statement and the same with one more hypothesis. Tactic-written terms are mostly too different to match. |
any_eq' for Array and for Vector |
proof-variant |
The same, where the names differ in one word (sum_push and prod_push, Array.any_eq' and Vector.any_eq'): a family written on purpose, listed apart so proof-clone stays the short list. |
max_add_left and max_add_right |
lean-linter --list prints the checks; --checks mislabel,doc-refs runs only some.
Only declarations written by hand are checked. Equation lemmas, matchers, recursors, auxiliary
proofs and anything else Lean generates are skipped, and so are deprecated declarations. A theorem
proved from the other one (an alias, a .symm) is a deliberate second name and is never reported
as a duplicate.
lean-linter [options] Module...
--checks a,b,... run only these checks (see --list)
--root Prefix report on modules under Prefix (repeatable); the default is the first
component of each module given, e.g. Mathlib for Mathlib.Order.Basic
--json print findings as JSON lines instead of tab-separated text
--list list the checks and exit
--version print the version and exit
The exit status is 0 if nothing was found, 1 if something was, and 2 on a usage error.
The checks compare names and docstrings with statements, and English is looser than Lean, so each finding is something to read rather than a verdict.
-
mislabel,namespace,name-shape,doc-mislabel,duplicate-instanceare usually right. The exceptions are words with a second meaning:_oddnaming the parity of an index, "limit" meaning a limit ordinal,predmeaningn - 1. -
vacuousfound nothing wrong in Mathlib or Lean core: its few hits are tactic-internal lemmas. That is the point of running it, on a formalization someone else wrote and you want to trust. -
trustlists a project's to-do list (sorry, axioms,native_decide). On Mathlib it reports onlyopaqueandunsafedeclarations, which are meta-programming code, and nosorry, axiom ornative_decide. -
diamondfound no instance diamonds in Mathlib: every same-typed pair it compared is definitionally equal. -
proof-clone,proof-variantare exact about what they compare, which is the lemmas a proof applies and their order, not whether two arguments are the same. On Lean coreproof-clonelists about 110 groups andproof-variantabout 140, nearly all of them real shared arguments: the variants are a family on purpose, and the clones are where a shared lemma would help. Tactic-written terms mostly differ from one another and are not matched. -
universeis usually right once tactic, meta-programming and private code is left out. Some small-category statements needTypeon purpose. -
op-mismatch,doc-iffare experimental. On Mathlibop-mismatchgives about sixty hits of which a handful are real name errors, anddoc-iffmostly flags docstrings that describe an iff the lemma is one half of. -
duplicate,duplicate-symare exact, but some duplication is on purpose: a second name for dot notation or for people searching under another name. The usual fix is to make one analiasof the other, which this check then accepts. -
doc-refs,dead-privatefind real rot, mixed with things they cannot see through: notation written in backticks, and lemmas used by a tactic that looks them up by name (aesoprules,simprocs) without any term mentioning them. -
deprecationonly reports aliases whose authors switched off Lean's own warning, so on a library that heeds that warning it is nearly silent, and on Mathlib every hit was a rename in progress.
On Mathlib a full run gives a few dozen findings for the first group and hundreds for the others. Lean core gives a handful for the first group.
lean-linter imports the modules you name, the way lean does when it elaborates a file that
imports them. It then reads the resulting environment: every declaration's statement, proof,
docstring, source position and attributes. Nothing is parsed or elaborated again, so the checks
see exactly what Lean compiled, including declarations generated by attributes such as
to_additive and simps. It needs the library to be built first.
Because it reads .olean files, lean-linter has to be built with the same Lean version as the
library it reads. As a dependency it is built with your project's toolchain automatically; on its
own, copy the project's lean-toolchain before building, as in the quick start. Development
happens on Lean v4.36.0.
| Library | Declarations | Time |
|---|---|---|
Lean core (Init, Std, Lean) |
99,000 | ~10 s |
| Mathlib | 330,000 | ~6 min |
Run lean-linter on pull requests and keep only what is new by comparing against a saved baseline:
- name: Lint
run: |
lake exe lean-linter MyLibrary > findings.tsv || true
cut -f1,2 findings.tsv | sort > now.txt
sort lint-baseline.txt | comm -13 - now.txt > new.txt
if [ -s new.txt ]; then cat new.txt; exit 1; filake build # the library and the lean-linter executable
./test/run.sh # every check against test/LeanLinterTest/Fixtures.leantest/LeanLinterTest/Fixtures.lean has one example of each problem and a correct twin of each.
test/run.sh checks that the linter reports exactly the examples, as listed in
test/expected.tsv.
To add a check, write def myCheck : Check in a new file under LeanLinter/Checks/. Its
module docstring should explain what it reports, how it decides, and its known false positives.
Then add it to allChecks in LeanLinter.lean, and give it an example and a correct twin in the
fixtures.
Apache License 2.0. See LICENSE.