Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive Namespace is never user with namespaced keywords #211

Closed
brabster opened this issue Nov 28, 2016 · 4 comments
Closed

False positive Namespace is never user with namespaced keywords #211

brabster opened this issue Nov 28, 2016 · 4 comments

Comments

@brabster
Copy link

The unused-namespaces linter doesn't recognise when a namespace is imported and its alias is used with a namespaced keyword, like so:

(ns foo
  (:require [bar.baz :as baz))

(def x {::baz/wibble ""})
@dtenny
Copy link

dtenny commented Jul 24, 2017

Just a note that this is a significant problem as more namespace aliased keywords are added to code for clojure.spec declarations.

@jafingerhut
Copy link
Collaborator

As Eastwood is implemented right now, using tools.reader to read the source code, that library already converts ::baz/wibble to :bar.baz/wibble before the rest of Eastwood starts its analysis.

What if soeone did this?

(ns foo
  (:require [bar.baz :as baz]))

(def x (:bar.baz/wibble ""})

Would you want to have no unused-namespace warning, even though the alias baz was never mentioned in the namespace's code, i.e. only the full namespace name bar.baz was ever used in keywords? That I believe I see a way to implement without huge changes in Eastwood.

@dtenny
Copy link

dtenny commented Oct 11, 2017

In this case the namespace might reasonably be said to be used even if not by the alias, and even though it's a keyword which doesn't require the namespace to exist before use. So I wouldn't report it as an unused namespace in your example. My goal was just to find :require declarations for namespaces that aren't used, so that can keep code pruned to reasonable sets of stuff we actually seem to be using.

I'm not sure if that fits what you thought could be easily implemented or not, hope it helps.

@jafingerhut
Copy link
Collaborator

The commit mentioned above looks for all uses of keywords in a namespace, after any ::<namespace-alias>/<keyword-name> occurrences have been resolved to the full namespace name, and considers a namespace as used if it appears in any keyword reference.

Note that since this is done after macros are expanded, if a keyword appears in a macro expansion, it can cause a namespace to be considered used that way, even if the keyword with that namespace is in a macro definition in a different namespace. A bit indirect, but the only down-side in this case is that no warning is issued for that namespace being unused.

If you want to try it out to see if it fixes the issue for you before the next Eastwood release, you can check out these instructions for locally installing an unreleased version of Eastwood from source code: https://github.com/jonase/eastwood#for-eastwood-developers

@jafingerhut jafingerhut added this to the Eastwood 0.2.5 milestone Oct 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants