-
Notifications
You must be signed in to change notification settings - Fork 957
Expand file tree
/
Copy pathenv_linter.lean
More file actions
267 lines (204 loc) · 7.61 KB
/
Copy pathenv_linter.lean
File metadata and controls
267 lines (204 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import Lean
open Lean Linter EnvLinter Meta
/-! ## Dummy env linter for testing -/
/-- A dummy linter that flags any declaration whose last name component starts with "bad". -/
@[builtin_env_linter]
public meta def dummyBadName : EnvLinter where
test declName := do
if let .str _ s := declName then
if s.startsWith "bad" then
return some m!"declaration name starts with 'bad'"
return none
noErrorsFound := "no bad names found"
errorsFound := "found bad names"
/-! ## Test: extension registration -/
def testExtContains (name : Name) : CoreM Bool := do
let ext := envLinterExt.getState (← getEnv)
return ext.contains name
/-- info: true -/
#guard_msgs in
#eval testExtContains `dummyBadName
/-- info: false -/
#guard_msgs in
#eval testExtContains `nonexistentLinter
/-! ## Test: getEnvLinter resolves the linter -/
def testResolveLinter : CoreM String := do
let some (declName, _) := (envLinterExt.getState (← getEnv)).find? `dummyBadName
| throwError "not found"
let linter ← getEnvLinter `dummyBadName declName
return toString linter.name
/-- info: "dummyBadName" -/
#guard_msgs in
#eval testResolveLinter
/-! ## Test: dummy linter detects bad names -/
def badDef : Nat := 42
def goodDef : Nat := 42
def testLinterDetects (declName : Name) : MetaM Bool := do
let some (linterDeclName, _) := (envLinterExt.getState (← getEnv)).find? `dummyBadName
| throwError "not found"
let linter ← getEnvLinter `dummyBadName linterDeclName
return (← linter.test declName).isSome
/-- info: true -/
#guard_msgs in
#eval testLinterDetects `badDef
/-- info: false -/
#guard_msgs in
#eval testLinterDetects `goodDef
/-! ## Test: builtin_nolint -/
@[builtin_nolint dummyBadName]
def badButNolinted : Nat := 99
def testShouldBeLinted (linter decl : Name) : CoreM Bool := do
shouldBeLinted linter decl
/-- info: false -/
#guard_msgs in
#eval testShouldBeLinted `dummyBadName `badButNolinted
/-- info: true -/
#guard_msgs in
#eval testShouldBeLinted `dummyBadName `badDef
/-! ## Test: builtin_env_linter extra -/
@[builtin_env_linter extra]
public meta def dummyExtraLinter : EnvLinter where
test _ := return none
noErrorsFound := "ok"
errorsFound := "err"
-- The extension stores (declName, isDefault). Extra means isDefault = false.
def testIsDefault (name : Name) : CoreM (Option Bool) := do
let ext := envLinterExt.getState (← getEnv)
match ext.find? name with
| some (_, isDefault) => return some isDefault
| none => return none
/-- info: some false -/
#guard_msgs in
#eval testIsDefault `dummyExtraLinter
/-- info: some true -/
#guard_msgs in
#eval testIsDefault `dummyBadName
/-! ## Test: duplicate linter name is rejected -/
/--
error: invalid attribute `builtin_env_linter`, linter `dummyBadName` has already been declared
-/
#guard_msgs in
@[builtin_env_linter] public meta def Other.dummyBadName : EnvLinter :=
{ test := fun _ => return none, noErrorsFound := "", errorsFound := "" }
/-! ## Test: isAutoDecl -/
/-- info: true -/
#guard_msgs in
#eval isAutoDecl `Nat.casesOn
/-- info: true -/
#guard_msgs in
#eval isAutoDecl `Nat.recOn
/-- info: false -/
#guard_msgs in
#eval isAutoDecl `Nat.add
/-! ## Test: getChecks -/
-- Default mode: only isDefault=true linters
def testGetChecksDefault : CoreM (Array Name) := do
let checks ← getChecks (scope := .default) (runOnly := none)
return checks.map (·.name)
-- dummyBadName and checkUnivs are default, dummyExtraLinter is not
/-- info: #[`checkUnivs, `dummyBadName] -/
#guard_msgs in
#eval testGetChecksDefault
-- Extra mode: default linters together with non-default ones
def testGetChecksExtra : CoreM (Array Name) := do
let checks ← getChecks (scope := .extra) (runOnly := none)
return checks.map (·.name)
/-- info: #[`checkUnivs, `dummyBadName, `dummyExtraLinter] -/
#guard_msgs in
#eval testGetChecksExtra
-- All mode: all linters
def testGetChecksAll : CoreM (Array Name) := do
let checks ← getChecks (scope := .all) (runOnly := none)
return checks.map (·.name)
/-- info: #[`checkUnivs, `dummyBadName, `dummyExtraLinter] -/
#guard_msgs in
#eval testGetChecksAll
-- runOnly: only specified linters
def testGetChecksRunOnly : CoreM (Array Name) := do
let checks ← getChecks (runOnly := some [`dummyExtraLinter])
return checks.map (·.name)
/-- info: #[`dummyExtraLinter] -/
#guard_msgs in
#eval testGetChecksRunOnly
/-! ## Test: getDeclsInCurrModule -/
def testDeclsInCurrModule : CoreM Bool := do
let decls ← getDeclsInCurrModule
-- Our test declarations should be in the current module
return decls.contains `badDef && decls.contains `goodDef && decls.contains `badButNolinted
/-- info: true -/
#guard_msgs in
#eval testDeclsInCurrModule
/-! ## Test: lintCore -/
-- lintCore should find badDef but not goodDef or badButNolinted
def testLintCore : CoreM (Array (Name × Nat)) := do
let linters ← getChecks (scope := .default) (runOnly := none)
let results ← lintCore #[`badDef, `goodDef, `badButNolinted] linters
return results.map fun (linter, msgs) => (linter.name, msgs.size)
/-- info: #[(`checkUnivs, 0), (`dummyBadName, 1)] -/
#guard_msgs in
#eval testLintCore
-- Verify which declaration was flagged
def testLintCoreDetail : CoreM (Array Name) := do
let linters ← getChecks (scope := .default) (runOnly := none)
let results ← lintCore #[`badDef, `goodDef, `badButNolinted] linters
let mut flagged := #[]
for (_, msgs) in results do
for (name, _) in msgs.toArray do
flagged := flagged.push name
return flagged
/-- info: #[`badDef] -/
#guard_msgs in
#eval testLintCoreDetail
/-! ## Test: formatLinterResults -/
def testFormatResults : CoreM Format := do
let linters ← getChecks (scope := .default) (runOnly := none)
let results ← lintCore #[`badDef, `goodDef] linters
let msg ← formatLinterResults results #[`badDef, `goodDef]
(groupByFilename := false) (whereDesc := "in test") (scope := .all)
(verbose := .medium) (numLinters := linters.size)
return (← msg.format)
/--
info: -- Found 1 error in 2 declarations (plus 0 automatically generated ones) in test with 2 linters
/- The `dummyBadName` linter reports:
found bad names -/
#check badDef /- declaration name starts with 'bad' -/
-/
#guard_msgs in
#eval testFormatResults
-- No errors case
def testFormatResultsClean : CoreM Format := do
let linters ← getChecks (scope := .default) (runOnly := none)
let results ← lintCore #[`goodDef] linters
let msg ← formatLinterResults results #[`goodDef]
(groupByFilename := false) (whereDesc := "in test") (scope := .all)
(verbose := .medium) (numLinters := linters.size)
return (← msg.format)
/--
info: -- Found 0 errors in 1 declarations (plus 0 automatically generated ones) in test with 2 linters
-/
#guard_msgs in
#eval testFormatResultsClean
/-! ## Test: checkUnivs -/
-- Good: each universe parameter occurs alone somewhere
universe u v in
def goodUnivs (α : Type u) (β : Type v) : Type (max u v) := α × β
-- Good: one universe dominates the other (max u v where u occurs alone)
universe u v in
def goodUnivsDominated (α : Type u) (β : Type (max u v)) : Type (max u v) := α × β
-- Bad: neither u nor v occur alone
universe u v in
def badUnivs (α : Type (max u v)) : Type (max u v) := α
def testCheckUnivs (declName : Name) : MetaM Bool := do
let some (linterDeclName, _) := (envLinterExt.getState (← getEnv)).find? `checkUnivs
| throwError "not found"
let linter ← getEnvLinter `checkUnivs linterDeclName
return (← linter.test declName).isSome
/-- info: false -/
#guard_msgs in
#eval testCheckUnivs `goodUnivs
/-- info: false -/
#guard_msgs in
#eval testCheckUnivs `goodUnivsDominated
/-- info: true -/
#guard_msgs in
#eval testCheckUnivs `badUnivs