Skip to content

Commit 45f65c8

Browse files
thorimurmarcelolynchclaude
authored
feat: don't consider private names autogenerated in isAutoDecl (#1831)
Co-authored-by: Marcelo Lynch <marcelomlynch@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 76e1c11 commit 45f65c8

5 files changed

Lines changed: 41 additions & 9 deletions

File tree

Batteries/Tactic/Lint/Basic.lean

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,15 @@ metadata is stored in the `Linter` structure. We define two attributes:
3131
the linter with name `linterName`.
3232
-/
3333

34-
-- TODO: do not consider private names autogenerated (batteries#1831)
3534
/--
3635
Returns true if `decl` is an automatically generated declaration.
3736
38-
Also returns true if `decl` is an internal name or created during macro
39-
expansion.
37+
Also returns true if `decl` is an internal name or created during macro expansion (after stripping
38+
private mangling; i.e., private names are not necessarily considered autogenerated).
4039
-/
4140
def _root_.Lean.Environment.isAutoDecl (env : Environment) (decl : Name) : Bool := Id.run do
42-
if decl.hasMacroScopes then return true
43-
if decl.isInternal then return true
41+
let declUserName := privateToUserName decl
42+
if declUserName.hasMacroScopes || declUserName.isInternal then return true
4443
if isReservedName env decl then return true
4544
if let Name.str n s := decl then
4645
if env.isAutoDecl n then return true

Batteries/Tactic/Lint/Misc.lean

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ their value, and allow arguments starting with `_` to be unused. -/
4444
| _ => return none
4545
if val.hasSorry || ty.hasSorry then return none
4646
forallTelescope ty fun args ty => do
47+
-- Exempt decls generated by `proof_wanted`/`def_wanted` (not imported here),
48+
-- which are of the form e.g. `(binders) → ProofWanted P` and have trivial content
49+
if ty.isAppOfArity' `ProofWanted 1 || ty.isAppOfArity' `DefWanted 1 then return none
4750
let mut e := (mkAppN val args).headBeta
4851
let ldecls ← args.mapM getFVarLocalDecl
4952
e := mkApp e ty

BatteriesTest/lint_docBlame.lean

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1+
module
2+
13
import Batteries.Tactic.Lint
24

35
set_option linter.missingDocs false
46

5-
/-- A docstring is needed here -/
7+
public section
8+
9+
-- A docstring is needed here
610
structure AtLeastThirtySeven where
7-
/-- and here -/
11+
-- and here
812
val : Nat := 1
913
-- but not here
1014
prop : 37 ≤ val
1115

12-
-- or here
16+
-- or here (due to being a theorem)
1317
theorem AtLeastThirtySeven.lt (x : AtLeastThirtySeven) : 36 < x.val := x.prop
1418

19+
def foo_bad := 3 -- Needs a docstring
20+
private def foo_ok := 7 -- Doesn't (due to being private)
21+
22+
/--
23+
error: /- The `docBlame` linter reports:
24+
DEFINITIONS ARE MISSING DOCUMENTATION STRINGS:
25+
This linter can be disabled with `@[nolint docBlame]`. -/
26+
#check AtLeastThirtySeven /- inductive missing documentation string -/
27+
#check AtLeastThirtySeven.val /- definition missing documentation string -/
28+
#check foo_bad /- definition missing documentation string -/
29+
-/
30+
#guard_msgs in
1531
#lint- only docBlame

BatteriesTest/lint_simpNF_respectTransparency.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private theorem Bundle.of_op {X : Type} [MyClass X] :
3636
@MyClass.op (Bundle.of X).carrier (Bundle.of X).inst = @MyClass.op X _ := rfl
3737

3838
/--
39-
error: -- Found 1 error in 0 declarations (plus 31 automatically generated ones) in the current file with 1 linters
39+
error: -- Found 1 error in 12 declarations (plus 19 automatically generated ones) in the current file with 1 linters
4040
4141
/- The `simpNF` linter reports:
4242
SOME SIMP LEMMAS ARE NOT IN SIMP-NORMAL FORM.

BatteriesTest/lintunused.lean

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
module
2+
13
import Batteries.Tactic.Lint
4+
import Batteries.Util.ProofWanted
5+
6+
public section
27

38
-- should be ignored as the proof contains sorry
49
/-- warning: declaration uses `sorry` -/
@@ -33,6 +38,13 @@ theorem foo7_bad [Mul Nat] [inst : Add Nat] {h : 1 = 1} ⦃h' : 0 = 0⦄ : True
3338
set_option linter.unusedVariables false in
3439
@[deprecated foo6_ok (since := "today")] theorem foo6_ok' (h : Nat) : True := trivial
3540

41+
-- private theorems are still linted
42+
private theorem foo8_bad [Mul Nat] : True := trivial
43+
44+
-- `proof_wanted`/`def_wanted` is ignored
45+
proof_wanted wanted (h : Bool) : True
46+
def_wanted wanted' (h : Bool) : Bool
47+
3648
/--
3749
error: /- The `unusedArguments` linter reports:
3850
UNUSED ARGUMENTS.
@@ -50,6 +62,8 @@ This linter can be disabled with `@[nolint unusedArguments]`. -/
5062
argument 2: [inst : Add Nat]
5163
argument 3: {h : 1 = 1}
5264
argument 4: ⦃h' : 0 = 0⦄ -/
65+
#check @foo8_bad /- 1 unused argument:
66+
argument 1: [Mul Nat] -/
5367
-/
5468
#guard_msgs in
5569
#lint- only unusedArguments

0 commit comments

Comments
 (0)