double underscore no longer counts as a leading underscore

Discussed in the user-group briefly. Symbols like __index technically
begin with an underscore but probably don't want to opt out of unused
variable checking.
This commit is contained in:
XeroOl 2025-07-22 20:28:24 -05:00
parent 4f3b89d7a9
commit 9be6628bf0

View File

@ -92,19 +92,21 @@ You can read more about how to add lints in docs/linting.md"
:type :definition :type :definition
:impl (fn [server file symbol definition] :impl (fn [server file symbol definition]
"local variable that is defined but not used" "local variable that is defined but not used"
(if (not (or (= "_" (: (tostring symbol) :sub 1 1)) (let [symname (tostring symbol)]
(= "_" (: (tostring symbol) :sub -1 -1)) (if (not (or (and (= "_" (symname:sub 1 1))
(accumulate [reference false (not= "__" (symname:sub 1 2)))
_ ref (ipairs definition.referenced-by) (= "_" (symname:sub -1 -1))
&until reference] (accumulate [reference false
(or (= ref.ref-type :read) _ ref (ipairs definition.referenced-by)
(= ref.ref-type :mutate))))) &until reference]
{:range (message.ast->range server file symbol) (or (= ref.ref-type :read)
:message (.. "unused definition: " (tostring symbol)) (= ref.ref-type :mutate)))))
:severity message.severity.WARN {:range (message.ast->range server file symbol)
:fix #{:title (.. "Replace " (tostring symbol) " with _" (tostring symbol)) :message (.. "unused definition: " symname)
:changes [{:range (message.ast->range server file symbol) :severity message.severity.WARN
:newText (.. "_" (tostring symbol))}]}}))}) :fix #{:title (.. "Replace " symname " with _" symname)
:changes [{:range (message.ast->range server file symbol)
:newText (.. "_" symname)}]}})))})
;; this is way too specific; it's also safe to do this inside an `if` or `case` ;; this is way too specific; it's also safe to do this inside an `if` or `case`
(fn in-or? [calls symbol] (fn in-or? [calls symbol]
@ -793,7 +795,7 @@ You can read more about how to add lints in docs/linting.md"
(add-lint :nested-associative-operator (add-lint :nested-associative-operator
{:what-it-does {:what-it-does
"Identifies forms that could be written in a flattr way, like `(and foo (and bar baz))`." "Identifies forms that could be written in a flatter way, like `(and foo (and bar baz))`."
:why-care? :why-care?
"Collapsing nested forms reduces unnecessary nesting and makes code more readable and idiomatic." "Collapsing nested forms reduces unnecessary nesting and makes code more readable and idiomatic."
:example :example