From e5dc04764c2c48fdd98c5a68b4d602f6bdd914e3 Mon Sep 17 00:00:00 2001 From: Phil Hagelberg Date: Sun, 5 Jan 2025 12:40:25 -0800 Subject: [PATCH] Allow trailing underscores to bypass unused local warnings. --- docs/manual.md | 15 ++++++++++++++- src/fennel-ls/lint.fnl | 7 +------ test/lint.fnl | 4 +++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/docs/manual.md b/docs/manual.md index 759fc03..0870418 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -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. diff --git a/src/fennel-ls/lint.fnl b/src/fennel-ls/lint.fnl index 0efde9a..e4167da 100644 --- a/src/fennel-ls/lint.fnl +++ b/src/fennel-ls/lint.fnl @@ -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} diff --git a/test/lint.fnl b/test/lint.fnl index 50aebdf..49058a9 100644 --- a/test/lint.fnl +++ b/test/lint.fnl @@ -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 ) --> (set (. )) (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