|
| 1 | +/- |
| 2 | +Copyright (c) 2020 Floris van Doorn. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Floris van Doorn, Robert Y. Lewis, Gabriel Ebner |
| 5 | +-/ |
| 6 | + |
| 7 | +import Lean |
| 8 | +import Std.Util.TermUnsafe |
| 9 | +import Std.Lean.NameMapAttribute |
| 10 | +open Lean Meta |
| 11 | + |
| 12 | +namespace Std.Tactic.Lint |
| 13 | + |
| 14 | +/-! |
| 15 | +# Basic linter types and attributes |
| 16 | +
|
| 17 | +This file defines the basic types and attributes used by the linting |
| 18 | +framework. A linter essentially consists of a function |
| 19 | +`(declaration : Name) → MetaM (Option MessageData)`, this function together with some |
| 20 | +metadata is stored in the `Linter` structure. We define two attributes: |
| 21 | +
|
| 22 | + * `@[stdLinter]` applies to a declaration of type `Linter` and adds it to the default linter set. |
| 23 | +
|
| 24 | + * `@[nolint linterName]` omits the tagged declaration from being checked by |
| 25 | + the linter with name `linterName`. |
| 26 | +-/ |
| 27 | + |
| 28 | +syntax (name := nolint) "nolint" (ppSpace ident)+ : attr |
| 29 | + |
| 30 | +-- Defines the user attribute `nolint` for skipping `#lint` |
| 31 | +initialize nolintAttr : NameMapAttribute (Array Name) ← |
| 32 | + registerNameMapAttribute { |
| 33 | + name := `nolint |
| 34 | + descr := "Do not report this declaration in any of the tests of `#lint`" |
| 35 | + add := fun _decl stx => |
| 36 | + match stx with |
| 37 | + -- TODO: validate linter names |
| 38 | + | `(attr|nolint $[$ids]*) => pure $ ids.map (·.getId.eraseMacroScopes) |
| 39 | + | _ => throwError "unexpected nolint syntax {stx}" |
| 40 | + } |
| 41 | + |
| 42 | +/-- Returns true if `decl` should be checked |
| 43 | +using `linter`, i.e., if there is no `nolint` attribute. -/ |
| 44 | +def shouldBeLinted [Monad m] [MonadEnv m] (linter : Name) (decl : Name) : m Bool := |
| 45 | + return !((nolintAttr.find? (← getEnv) decl).getD {}).contains linter |
| 46 | + |
| 47 | +/-- |
| 48 | +Returns true if `decl` is an automatically generated declaration. |
| 49 | +
|
| 50 | +Also returns true if `decl` is an internal name or created during macro |
| 51 | +expansion. |
| 52 | +-/ |
| 53 | +def isAutoDecl (decl : Name) : CoreM Bool := do |
| 54 | + if decl.hasMacroScopes then return true |
| 55 | + if decl.isInternal then return true |
| 56 | + if let Name.str n s := decl then |
| 57 | + if s.startsWith "proof_" || s.startsWith "match_" then return true |
| 58 | + if (← getEnv).isConstructor n && ["injEq", "inj", "sizeOf_spec"].any (· == s) then |
| 59 | + return true |
| 60 | + if let ConstantInfo.inductInfo _ := (← getEnv).find? n then |
| 61 | + if [casesOnSuffix, recOnSuffix, brecOnSuffix, binductionOnSuffix, belowSuffix, |
| 62 | + "ndrec", "ndrecOn", "noConfusionType", "noConfusion"].any (· == s) then |
| 63 | + return true |
| 64 | + pure false |
| 65 | + |
| 66 | +/-- |
| 67 | +A linting test for the `#lint` command. |
| 68 | +
|
| 69 | +`test` defines a test to perform on every declaration. It should never fail. Returning `none` |
| 70 | +signifies a passing test. Returning `some msg` reports a failing test with error `msg`. |
| 71 | +
|
| 72 | +`noErrorsFound` is the message printed when all tests are negative, and `errorsFound` is printed |
| 73 | +when at least one test is positive. |
| 74 | +
|
| 75 | +If `isFast` is false, this test will be omitted from `#lint-`. |
| 76 | +-/ |
| 77 | +structure Linter where |
| 78 | + test : Name → MetaM (Option MessageData) |
| 79 | + noErrorsFound : MessageData |
| 80 | + errorsFound : MessageData |
| 81 | + isFast := true |
| 82 | + |
| 83 | +structure NamedLinter extends Linter where |
| 84 | + declName : Name |
| 85 | + |
| 86 | +def NamedLinter.name (l : NamedLinter) : Name := l.declName.updatePrefix Name.anonymous |
| 87 | + |
| 88 | +def getLinter (declName : Name) : CoreM NamedLinter := unsafe |
| 89 | + return { ← evalConstCheck Linter ``Linter declName with declName } |
| 90 | + |
| 91 | +/-- Takes a list of names that resolve to declarations of type `linter`, |
| 92 | +and produces a list of linters. -/ |
| 93 | +def getLinters (l : List Name) : CoreM (List NamedLinter) := |
| 94 | + l.mapM getLinter |
| 95 | + |
| 96 | +-- Defines the user attribute `stdLinter` for adding a linter to the default set. |
| 97 | +initialize stdLinterAttr : TagAttribute ← |
| 98 | + registerTagAttribute |
| 99 | + (name := `stdLinter) |
| 100 | + (descr := "Use this declaration as a linting test in #lint") |
| 101 | + (validate := fun name => do |
| 102 | + let constInfo ← getConstInfo name |
| 103 | + unless ← (isDefEq constInfo.type (mkConst ``Linter)).run' do |
| 104 | + throwError "must have type Linter, got {constInfo.type}") |
0 commit comments