I have a question about unused vars warnings from custom clj-kondo hooks. Specifically I am struggling with clojure-lsp reporting unused vars from a macro hook I am writing:
(defn test-unused [{:keys [node]}]
{:node (vary-meta
(api/list-node [(api/token-node 'do)
(api/list-node [(api/token-node 'def)
(api/token-node 'SomethingUnused)
(api/token-node nil)])])
assoc :clj-kondo/ignore [:unused-binding
:clojure-lsp/unused-public-var])})
Here is a custom hook which reports that a (def SomethingUnused nil) def is created. But I want to silence the unused var warning I get. This doesn't seem to work, resulting in the following warning:
Diagnostics:
1. Unused public var 'io.julienvincent.kondo-test/SomethingUnused' [clojure-lsp/unused-public-var]
Anyone know how I can get this to work?the clojure-lsp linting rule is outside of the control of clj-kondo
perhaps you can just generate a fake usage instead
But.. isn't is using clj-kondo to power it?
yes, but clojure-lsp doesn't know anything about the :clj-kondo/ignore thing you add to the do expression
Yea I tried that too:
(defn test-unused [{:keys [node]}]
{:node (vary-meta
(api/list-node [(api/token-node 'do)
(api/list-node [(api/token-node 'def)
(api/token-node 'SomethingUnused)
(api/token-node nil)])
(api/token-node 'SomethingUnused)])
assoc :clj-kondo/ignore [:unused-binding
:clojure-lsp/unused-public-var])})
It didn't workwell, that should work I think. let's see.
> yes, but clojure-lsp doesn't know anything about the :clj-kondo/ignore thing you add to the do expression Hmm but this works normally?
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(def thing nil)
How does that mechanism work then? Did clojure-lsp re-implement this or something?I guess I don't know how this all glues together then haha
that's a good question, I forgot that that example worked. FWIW, I don't use clojure-lsp unused public var myself
let's quickly look at how those ignores are communicated to clojure-lsp
> FWIW, I don't use clojure-lsp unused public var myself I.e you're turning it off somehow and using clj-kondos native ones instead? I'm trying to make a public lib and don't want people to have to do that ideally for this use-case
I understand. @ericdallo do you remember how this works?
clojure-lsp had to re-implement this logic https://github.com/clojure-lsp/clojure-lsp/blob/f555e4b89a1a3aa8aa9f66809f0d5e4dc034c61d/lib/src/clojure_lsp/feature/diagnostics/built_in.clj#L38, but
#_{:clojure-lsp/ignore [:clojure-lsp/unused-public-var]}
;; or
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
should worklet me test
ah ok. so clojure-lsp is looking for a literal uneval there. adding it via metadata on a hook doesn't work in this case then.
but emitting the extra usage should work
possibly it's ignored by clojure-lsp since it doesn't have location metadata or so
Is that something that can be added in the hook?
you could copy it from another node
e.g. from the top level node
(with-meta (api/token-node 'Something) (meta node))if that works, it will pop up as a reference in lsp too
trying
Works!
Amazing, thanks
👍