jonase/eastwood

False positive "Namespace is never used"

emlyn opened this issue · 7 comments

emlyn commented

When importing a class defined in another namespace with defrecord, it is necessary to first require the namespace so that the class is compiled before it can be imported (see https://clojuredocs.org/clojure.core/defrecord#example-542692d2c026201cdc326f87), but eastwood warns that the required namespace is never used.

emlyn commented

Actually a workaround for this is to set :load-ns true on the defrecord so that the require is not needed.

Any updates or should it be closed?

There were some improvements made to Eastwood on its reporting of unused namespaces, but I do not recall whether those improvements fix this case, or not.

Here is a commit with the last of those changes to Eastwood that I am aware of. Those changes have been part of Eastwood since release version 0.2.5, so if someone could retest the problem case with Eastwood 0.2.5 or later and report here, that would be good to know. 9c2a32f

Note: I would not be surprised if the problem case reported here is not improved by the changes I mentioned.

As of the "0.3.13" version, this is still an issue.

I added a draft PR with a testcase: #351.

Are there any plans to address this issue?

vemv commented

(I'm merely an occasional contributor)

Looks like implementing a solution would entail creating a function akin to protocols-used, but for classes (namely those coming from defrecord + deftype) instead:

used-protocols (protocols-used asts)

Here is a sample classes-used implementation that worked for me just now, against your test case:

(defn classes-used [asts]
  (->> asts
       (mapcat ast/nodes)
       (keep :o-tag) ;; other "interesting" keys that can include a used class include: `:tag`, `:val`, `:result`
       set))

I don't have enough tools.analyzer expertise to tell which of the keys I have observed to be "interesting" (:o-tag, :tag, :val, :result) would be the most suitable for properly fixing this linter.

I'd encourage you to open a PR with a fix, spending a little more time in research than I did. Currently I lack the time/capability of opening proper PRs against Eastwood.

Cheers - V