Allow trailing underscores to bypass unused local warnings.

This commit is contained in:
Phil Hagelberg 2025-01-05 12:40:25 -08:00
parent 3979ab4c3c
commit e5dc04764c
3 changed files with 18 additions and 8 deletions

View File

@ -13,6 +13,9 @@ $ make
```
will create a bleeding-edge latest git `fennel-ls` binary for you.
Run `make install PREFIX=$HOME` to put it in `~/bin` or `sudo make install` for
a systemwide install.
#### Arch Linux
I think `fennel-ls` and `fennel-ls-git` may be in the AUR.
@ -128,10 +131,20 @@ Goto Reference | [X] | [ ] | [ ] | [ ] | [ ] |
Fennel-ls can report all fennel compiler errors, plus a few custom lints.
Unused locals will be flagged unless they begin or end with an underscore. If
you have a local that is unused in your code but necessary for pattern matching
purposes, it's recommended to put an underscore at the end. For example:
```fennel
(case [1 1 2 3 5 8]
[a_ a_] (print "First two elements are equal"))
```
## CLI Usage
```sh
fennel-ls --lint my-file.fnl f2.fnl # prints diagnostics for the files given
```
This will analyze the given files, and print out all compiler errors and lints, without launching a server.
This will analyze the given files, and print out all compiler errors and lints,
without launching a server. A successful exit code indicates no problems found.

View File

@ -32,6 +32,7 @@ the `file.diagnostics` field, filling it with diagnostics."
(λ unused-definition [server file symbol definition]
"local variable that is defined but not used"
(if (not (or (= "_" (: (tostring symbol) :sub 1 1))
(= "_" (: (tostring symbol) :sub -1 -1))
(accumulate [reference false
_ ref (ipairs definition.referenced-by)
&until reference]
@ -249,11 +250,5 @@ the `file.diagnostics` field, filling it with diagnostics."
(if lints.unknown-module-field
(unknown-module-field server file))))
;; (if lints.unnecessary-values
;; (unnecessary-values file)))
;; (if lints.unnecessary-do)
;; (unnecessary-do file)))
;; (if lints.unnecessary-unary-op))
;; (unnecessary-values file)))
{: add-lint-diagnostics}

View File

@ -55,6 +55,8 @@
[{:code 301
:range {:start {:character 9 :line 0}
:end {:character 10 :line 0}}}])
(check "(case [1 1 2 3 5 8] [a a] (print :first-two-equal))" [{:code 301}])
(assert-ok "(case [1 1 2 3 5 8] [a_ a_] (print :first-two-equal))")
;; setting a var without reading
(check "(var x 1) (set x 2) (set [x] [3])"
[{:code 301
@ -248,7 +250,7 @@
;; duplicate keys in kv table
;; (tset <sym> <any>) --> (set (. <sym> <any>)) (might be wanted for compat?)
;; {&as x} and [&as x] pattern with no other matches
;; Unused variables / fields (maybe difficult)
;; Unused fields (maybe difficult)
;; discarding results to various calls, such as unpack, values, etc
;; `pairs` or `ipairs` call in a (for) binding table
;; steal as many lints as possible from cargo