clj-kondo

https://github.com/clj-kondo/clj-kondo
borkdude 2020-08-20T09:51:08.000800Z

Nice trick, you can tweak linter settings per language in a .cljc file by using conditionals:

(ns clj-kondo-cljs-macros.ns2.ns
  #?(:clj {:clj-kondo/config '{:linters {:unused-binding {:level :off}}}}))

(defn foo [x y]
  #?(:cljs x))

💯 2
borkdude 2020-08-20T12:35:47.001400Z

If anyone feels like adding clj-kondo to this page: https://analysis-tools.dev/tag/clojure, go ahead

2020-08-20T16:22:59.003700Z

Are these warnings wrong? If so, I can create an issue.

2020-08-20T16:23:01.004100Z

I get the following warnings:

src/main_ns.cljc:3:14: warning: namespace required-ns is required but never used
src/main_ns.cljc:5:17: warning: Unused private var main-ns/my-fun
The macro expansion has a use of my-fun and a use of the r alias, so I don’t think there should be any warnings.

borkdude 2020-08-20T16:24:16.004500Z

Can you please post the code as text? This is easier for me to try out

2020-08-20T16:24:25.004700Z

Sure, give me a sec.

2020-08-20T16:25:27.005800Z

Either look at https://github.com/simon-katz/nomis-clj-kondo-cljc-macros-2 Or:

(ns required-ns)

(defn foo [] 42)

2020-08-20T16:25:40.006200Z

(ns main-ns
  #?(:cljs (:require-macros [main-ns :refer [my-macro]]))
  (:require [required-ns :as r]))

(defn ^:private my-fun [x]
  (inc x))

#?(:clj
   (defmacro my-macro []
     `(my-fun (r/foo))))

borkdude 2020-08-20T16:26:03.006600Z

I already see the problem. You are only using required-ns in Clojure. So why not wrap that in a #?(:clj ...)

borkdude 2020-08-20T16:26:38.007400Z

No wait, let me take another look ;)

2020-08-20T16:27:02.007800Z

The expansion of the macro uses the req…. Oh, OK, I’ll wait 🙂

2020-08-20T16:28:57.009Z

If I run (my-macro), I get a result of 43 in both CLJ and CLJS, so I’m pretty sure that both my-fun and r/foo are used in both CLJ and CLJS.

borkdude 2020-08-20T16:34:10.010400Z

@nomiskatz It's an interesting edge case fore sure. You can fix it with a local config:

(ns main-ns
  {:clj-kondo/config '{:linters {:unused-private-var {:exclude [main-ns/my-fun]}
                                 :unused-namespace {:exclude [required-ns]}}}}
  #?(:cljs (:require-macros [main-ns :refer [my-macro]]))
  (:require [required-ns :as r]))
And feel free to post this edge case as an issue.

borkdude 2020-08-20T16:34:38.010900Z

I'm not sure if this is fixable since it depends on runtime usage of the macro if the namespace + private var will be really used.

2020-08-20T16:35:55.012Z

@borkdude Thanks for confirming, and for the workaround. I’ll post an issue, but I understand why it might be a tough one.

2020-08-20T17:09:03.012200Z

(Done: https://github.com/borkdude/clj-kondo/issues/961)