Skip to content

Commit

Permalink
Avoid false-positives of unused-public-var for functions used in :gen…
Browse files Browse the repository at this point in the history
…-class that starts with `-` as convention
  • Loading branch information
ericdallo committed Jan 8, 2023
1 parent 9d91649 commit fcb281b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- General
- Bump clj-kondo to `2022.12.11-20221220.093423-5`
- Avoid false-positives of unused-public-var for functions used in :gen-class that starts with `-` as convention.

- Editor
- Fix add missing import code action when there are multiple options. #1422
Expand Down
21 changes: 16 additions & 5 deletions lib/src/clojure_lsp/feature/diagnostics.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
[clj-kondo.impl.config :as kondo.config]
[clojure-lsp.dep-graph :as dep-graph]
[clojure-lsp.logger :as logger]
[clojure-lsp.parser :as parser]
[clojure-lsp.queries :as q]
[clojure-lsp.refactor.edit :as edit]
[clojure-lsp.settings :as settings]
[clojure-lsp.shared :as shared]
[clojure.core.async :as async]))
[clojure.core.async :as async]
[clojure.string :as string]
[rewrite-clj.zip :as z])
(:gen-class))

(set! *warn-on-reflection* true)

Expand Down Expand Up @@ -52,15 +57,21 @@
(format "Unused public var '%s/%s'" (:ns element) (:name element)))
:type :clojure-lsp/unused-public-var}))

(defn ^:private exclude-public-diagnostic-definition? [kondo-config definition]
(defn ^:private exclude-public-diagnostic-definition? [db kondo-config definition]
(let [kondo-config (kondo-config-for-ns kondo-config (:ns definition) (:filename definition))
excluded-syms-regex (get-in kondo-config [:linters :clojure-lsp/unused-public-var :exclude-regex] #{})
excluded-defined-by-syms-regex (get-in kondo-config [:linters :clojure-lsp/unused-public-var :exclude-when-defined-by-regex] #{})
fqsn (symbol (-> definition :ns str) (-> definition :name str))]
fqsn (symbol (-> definition :ns str) (-> definition :name str))
starts-with-dash? (string/starts-with? (:name definition) "-")]
(or (q/exclude-public-definition? kondo-config definition)
(some #(re-matches (re-pattern (str %)) (str fqsn)) excluded-syms-regex)
(some #(re-matches (re-pattern (str %)) (str (:defined-by definition))) excluded-defined-by-syms-regex)
(:export definition))))
(:export definition)
(when starts-with-dash?
;; check if if namespace has :gen-class
(some-> (parser/zloc-of-file db (:uri definition))
edit/find-namespace
(z/find-next-value z/next :gen-class))))))

(defn ^:private kondo-finding->diagnostic
[{:keys [type message level row col end-row] :as finding}]
Expand Down Expand Up @@ -171,7 +182,7 @@
(publish-all-diagnostics!* components (map empty-diagnostics-of-uri uris)))

(defn ^:private unused-public-vars [narrowed-db project-db kondo-config]
(let [exclude-def? (partial exclude-public-diagnostic-definition? kondo-config)
(let [exclude-def? (partial exclude-public-diagnostic-definition? project-db kondo-config)
var-definitions (->> (q/find-all-var-definitions narrowed-db)
(remove exclude-def?))
var-nses (set (map :ns var-definitions)) ;; optimization to limit usages to internal namespaces, or in the case of a single file, to its namespaces
Expand Down
10 changes: 9 additions & 1 deletion lib/test/clojure_lsp/feature/diagnostics_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

(deftest lint-project-public-vars
(h/load-code-and-locs "(ns some-ns) (defn foo [a b] (+ a b))")
(h/load-code-and-locs "(ns some-ns) (defn -main [& _args] 1)" (h/file-uri "file:///b.clj"))
(h/load-code-and-locs "(ns some-ns (:gen-class)) (defn -main [& _args] 1) (defn -foo [] 1)" (h/file-uri "file:///b.clj"))
(h/load-code-and-locs (h/code "(ns some-ns (:require [re-frame.core :as r]))"
"(r/reg-event-fx :some/thing (fn []))"
"(r/reg-event-fx :otherthing (fn []))") (h/file-uri "file:///c.cljs"))
Expand Down Expand Up @@ -175,6 +175,14 @@
(h/db)
{:reg-finding! #(swap! findings conj %)
:config {}})
(h/assert-submaps [] @findings))
(testing "var with dash and :gen-class on ns is excluded"
(reset! findings [])
(f.diagnostic/custom-lint-uris!
[(h/file-uri "file:///b.clj")]
(h/db)
{:reg-finding! #(swap! findings conj %)
:config {}})
(h/assert-submaps [] @findings)))

(deftest lint-clj-kondo-findings
Expand Down

0 comments on commit fcb281b

Please sign in to comment.