-
-
Notifications
You must be signed in to change notification settings - Fork 290
/
usages.clj
321 lines (311 loc) · 16.6 KB
/
usages.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
(ns clj-kondo.impl.analyzer.usages
{:no-doc true}
(:refer-clojure :exclude [ns-name])
(:require
[clj-kondo.impl.analysis :as analysis]
[clj-kondo.impl.analyzer.common :as common]
[clj-kondo.impl.findings :as findings]
[clj-kondo.impl.metadata :as meta]
[clj-kondo.impl.namespace :as namespace]
[clj-kondo.impl.utils :as utils :refer [tag one-of symbol-from-token kw->sym assoc-some
symbol-token?]]
[clojure.string :as str])
(:import [clj_kondo.impl.rewrite_clj.node.seq NamespacedMapNode]))
(set! *warn-on-reflection* true)
(defn ^:private resolve-keyword [ctx expr current-ns]
(let [aliased? (:namespaced? expr)
token (if (symbol-token? expr)
(symbol-from-token expr)
(:k expr))
prefix (:prefix expr)
name-sym (some-> token name symbol)
alias-or-ns (some-> token namespace symbol)
ns-sym (cond
(and aliased? alias-or-ns)
(-> (namespace/get-namespace ctx (:base-lang ctx) (:lang ctx) current-ns)
(get-in [:aliases alias-or-ns] :clj-kondo/unknown-namespace))
aliased?
current-ns
(and prefix
(= '_ alias-or-ns))
nil
(and prefix
(not alias-or-ns))
prefix
:else
alias-or-ns)
alias (when (and aliased? (not= :clj-kondo/unknown-namespace ns-sym)) alias-or-ns)]
{:name name-sym
:ns ns-sym
:namespace-from-prefix (and prefix
(not alias-or-ns)
(not (:namespaced? expr)))
:alias alias}))
(defn analyze-keyword
([ctx expr] (analyze-keyword ctx expr {}))
([ctx expr opts]
(let [ns (:ns ctx)
ns-name (:name ns)
keyword-val (:k expr)]
(when (:analyze-keywords? ctx)
(let [{:keys [:destructuring-expr :keys-destructuring?
:keys-destructuring-ns-modifier?]} opts
current-ns (some-> ns-name symbol)
destructuring (when destructuring-expr (resolve-keyword ctx destructuring-expr
current-ns))
resolved (resolve-keyword ctx expr current-ns)]
(analysis/reg-keyword-usage!
ctx
(:filename ctx)
(assoc-some (meta expr)
:context (utils/deep-merge
(:context ctx)
(:context expr))
:reg (:reg expr)
:keys-destructuring keys-destructuring?
:keys-destructuring-ns-modifier keys-destructuring-ns-modifier?
:auto-resolved (:namespaced? expr)
:namespace-from-prefix (when (:namespace-from-prefix resolved) true)
:name (:name resolved)
:alias (when-not (:alias destructuring) (:alias resolved))
:ns (or (:ns destructuring) (:ns resolved))))))
(when (and keyword-val (:namespaced? expr) (namespace keyword-val))
(let [symbol-val (kw->sym keyword-val)
{resolved-ns :ns}
(namespace/resolve-name ctx false ns-name symbol-val nil)]
(if resolved-ns
(namespace/reg-used-namespace! ctx
(-> ns :name)
resolved-ns)
(namespace/reg-unresolved-namespace! ctx ns-name
(with-meta (symbol (namespace symbol-val))
(meta expr)))))))))
(defn analyze-namespaced-map [ctx ^NamespacedMapNode expr]
(let [children (:children expr)
m (first children)
;; reset location of inner map to location of outer expression
m (vary-meta m merge (meta expr))
ns-name (-> ctx :ns :name)
the-ns (namespace/get-namespace ctx (:base-lang ctx) (:lang ctx) ns-name)
ns-keyword (-> expr :ns :k)
ns-sym (kw->sym ns-keyword)
aliased? (:aliased? expr)
resolved-ns (when aliased? (get (:qualify-ns the-ns) ns-sym))
resolved (or resolved-ns ns-sym)]
(when resolved-ns
(when-let [resolved-ns (get (:qualify-ns the-ns) ns-sym)]
(namespace/reg-used-alias! ctx ns-name ns-sym)
(namespace/reg-used-namespace! ctx
ns-name
resolved-ns)))
(let [children (:children m)
keys (take-nth 2 children)
vals (take-nth 2 (rest children))
keys (map (fn [child]
(assoc child :prefix resolved)) keys)
children (interleave keys vals)
m (assoc m :children children)]
(when-let [f (:analyze-expression** ctx)]
(f ctx m)))))
(defn analyze-usages2
([ctx expr] (analyze-usages2 ctx expr {}))
([ctx expr {:keys [:quote? :syntax-quote?] :as opts}]
(let [ns (:ns ctx)
dependencies (:dependencies ctx)
syntax-quote-level (or (:syntax-quote-level ctx) 0)
ns-name (:name ns)
t (tag expr)
quote? (or quote?
(= :quote t))
;; nested syntax quotes are treated as normal quoted expressions by clj-kondo
syntax-quote-tag? (= :syntax-quote t)
unquote-tag? (one-of t [:unquote :unquote-splicing])
new-syntax-quote-level (cond syntax-quote-tag? (inc syntax-quote-level)
unquote-tag? (dec syntax-quote-level)
:else syntax-quote-level)
syntax-quote? (or syntax-quote? syntax-quote-tag?)
ctx (assoc ctx :syntax-quote-level new-syntax-quote-level)
ctx (if syntax-quote-tag?
(update ctx :callstack #(cons [:syntax-quote] %))
ctx)]
(if (and (= 1 syntax-quote-level) unquote-tag?)
(common/analyze-expression** ctx expr)
(if quote?
(do (when (:k expr)
(analyze-keyword ctx expr opts))
(doall (mapcat
#(analyze-usages2 ctx %
(assoc opts :quote? quote? :syntax-quote? syntax-quote?))
(:children expr))))
(let [syntax-quote?
(or syntax-quote?
(= :syntax-quote t))]
(meta/lift-meta-content2 ctx expr true)
(case t
:token
(if-let [symbol-val (symbol-from-token expr)]
(let [simple? (simple-symbol? symbol-val)
symbol-val (if simple?
(namespace/normalize-sym-name ctx symbol-val)
symbol-val)
expr-meta (meta expr)]
(if-let [b (when (and simple? (not syntax-quote?))
(or (get (:bindings ctx) symbol-val)
(get (:bindings ctx)
(str/replace (str symbol-val) #"\**$" ""))))]
(namespace/reg-used-binding! ctx
(-> ns :name)
b
(when (:analyze-locals? ctx)
(assoc-some expr-meta
:name-row (:row expr-meta)
:name-col (:col expr-meta)
:name-end-row (:end-row expr-meta)
:name-end-col (:end-col expr-meta)
:name symbol-val
:filename (:filename ctx)
:str (:string-value expr))))
(let [{resolved-ns :ns
resolved-name :name
resolved-alias :alias
unresolved? :unresolved?
clojure-excluded? :clojure-excluded?
interop? :interop?
resolved-core? :resolved-core?
:as _m}
(let [v (namespace/resolve-name ctx false ns-name symbol-val expr)]
(when-not syntax-quote?
(when-let [n (:unresolved-ns v)]
(namespace/reg-unresolved-namespace!
ctx ns-name
(with-meta n
(meta expr)))))
(if (:unresolved? v)
(let [symbol-str (str symbol-val)]
(if (and (not= "." symbol-str)
(str/ends-with? symbol-str "."))
(namespace/resolve-name ctx true ns-name
(symbol (subs symbol-str
0 (dec (count symbol-str))))
expr)
v))
v))
m (meta expr)
row (:row m)
col (:col m)
end-row (:end-row m)
end-col (:end-col m)]
(when resolved-ns
;; this causes the namespace data to be loaded from cache
(swap! (:used-namespaces ctx) update (:base-lang ctx) conj resolved-ns)
(namespace/reg-used-namespace! ctx
ns-name
resolved-ns)
(when (:analyze-var-usages? ctx)
(let [usage {:type :use
:name (with-meta
resolved-name
m)
:resolved-ns resolved-ns
:ns ns-name
:alias resolved-alias
:defmethod (:defmethod ctx)
:dispatch-val-str (:dispatch-val-str ctx)
:unresolved? unresolved?
:allow-forward-reference? (:in-comment ctx)
:clojure-excluded? clojure-excluded?
:row row
:end-row end-row
:col col
:end-col end-col
:base-lang (:base-lang ctx)
:lang (:lang ctx)
:top-ns (:top-ns ctx)
:filename (:filename ctx)
:unresolved-symbol-disabled?
(or syntax-quote?
;; e.g. usage of clojure.core,
;; clojure.string, etc in (:require [...])
(= symbol-val (get (:qualify-ns ns)
symbol-val)))
:private-access? (or syntax-quote?
(:private-access? ctx))
:callstack (:callstack ctx)
:config (:config ctx)
:in-def (:in-def ctx)
:context (:context ctx)
:simple? simple?
:interop? interop?
;; save some memory
:expr (when-not dependencies expr)
:resolved-core? resolved-core?
:condition (:condition expr)}]
(namespace/reg-var-usage! ctx ns-name
usage)
(utils/reg-call ctx usage (:id expr))
nil)))
nil))
;; this is a symbol, either binding or var reference
(when-let [idx (:idx ctx)]
(let [len (:len ctx)]
(when (< idx (dec len))
(let [parent-call (first (:callstack ctx))
core? (one-of (first parent-call) [clojure.core cljs.core])
core-sym (when core?
(second parent-call))
generated? (:clj-kondo.impl/generated expr)
redundant?
(and (not generated?)
core?
(not (:clj-kondo.impl/generated (meta parent-call)))
(one-of core-sym [do fn defn defn-
let when-let loop binding with-open
doseq try when when-not when-first
when-some future]))]
(when redundant?
(findings/reg-finding! ctx (assoc (meta expr)
:type :unused-value
:message (str "Unused value: "
(str expr))
:filename (:filename ctx)))))))))
(do
;; this everything but a symbol token, including keywords
(when-let [idx (:idx ctx)]
(let [len (:len ctx)]
(when (< idx (dec len))
(let [parent-call (first (:callstack ctx))
parent-call-ns (first parent-call)
core? (one-of parent-call-ns [clojure.core cljs.core])
test? (when-not core?
(one-of parent-call-ns [clojure.test cljs.test]))
core-sym (second parent-call)
generated? (:clj-kondo.impl/generated expr)
redundant?
(and (not generated?)
(or core? test?)
(not (:clj-kondo.impl/generated (meta parent-call)))
(if core?
(one-of core-sym [do fn defn defn-
let when-let loop binding with-open
doseq try when when-not when-first
when-some future])
(when test?
(one-of core-sym [deftest]))))]
(when redundant?
(findings/reg-finding! ctx (assoc (meta expr)
:type :unused-value
:message (str "Unused value: "
(str expr))
:filename (:filename ctx))))))))
(when (:k expr)
(analyze-keyword ctx expr opts))))
:reader-macro
(doall (mapcat
#(analyze-usages2 ctx %
(assoc opts :quote? quote? :syntax-quote? syntax-quote?))
(rest (:children expr))))
;; catch-call
(doall (mapcat
#(analyze-usages2 ctx %
(assoc opts :quote? quote? :syntax-quote? syntax-quote?))
(:children expr))))))))))