Fork me on GitHub
#calva
<
2023-09-21
>
Ingy döt Net18:09:13

I'm seeing this unused public var highlighting in calva. It probably comes from the lsp. Apparently you can use undeclared vars in defmacro because this code works fine. But some part of the machinery here thinks you can't.

pez18:09:03

I think some part of the machinery just advices you to not do that. Clojure code is read top to bottom. But, yes, maybe a better error message would be good for this case.

Ingy döt Net19:09:26

Clojure is read top to bottom but the macro is not expanded there so there's not a problem.

pez19:09:06

Humans read Clojure top to bottom is what I meant. But yes, much because the Clojure reader does it. I’d say it’s convention to order the code like that even in cases where the compiler doesn’t need it.

Thierry10:09:40

@U05H8N9V0HZ in .clj-kondo/config.edn merge this {:linters {:clojure-lsp/unused-public-var {:level :off}}} and the blue squiglies will no longer bother you ;)

pez10:09:54

That’s not the issue here, though, is it? The argument made is that the var is used. (I disagree slightly, but anyway). What say @UKFSJSM38 and @U04V15CAJ?

borkdude10:09:54

unused public var is a linter implemented by clojure-lsp, so I'll defer to @UKFSJSM38 :)

Thierry10:09:11

Well it's not really used is it? Its referred to in the macro but not initialized?

borkdude10:09:37

you might just want to turn around the declaration of the function and the macro

1
borkdude10:09:55

FWIW I also have this linter disabled, I find the reference counter next to my vars enough information

borkdude11:09:59

somehow I can't even enable it anymore locally for testing out this case 😱

borkdude14:09:01

Somehow it started working again and this example works for me:

(defmacro foo []
  `(stringify-keys))

(defn stringify-keys [m]
  (reduce-kv (fn [m k v]
               (assoc! m (str k) v)) (transient {}) m))

borkdude14:09:18

when I remove the macro body, stringify-keys becomes unused

ericdallo14:09:06

me too, a minimal repro would be nie @U05H8N9V0HZ

Ingy döt Net14:09:47

The :use seems to be involved.

Ingy döt Net14:09:08

Could't repro without that.

borkdude15:09:11

hm yeah, :use doesn't work very well with clj-kondo

Ingy döt Net15:09:03

spooky action at a distance? 🙂

borkdude15:09:20

(conf)use(ing)

borkdude15:09:07

clj-kondo is a static analyzer which very much relies on the ns form, sometimes use works well, but sometimes it doesn't...

borkdude15:09:21

use also doesn't exist in ClojureScript btw, it's probably best to just avoid it when you can

borkdude15:09:13

Here is a clojure community guide with some recommendations https://github.com/bbatsov/clojure-style-guide#prefer-require-over-use

Ingy döt Net15:09:27

I only use it for dev libs I intend to remove later. The yellow highlighting is a nice reminder to remove it.