kibit icon indicating copy to clipboard operation
kibit copied to clipboard

Macro invocation is treated as 'Unneeded anonymous function'

Open joelittlejohn opened this issue 11 years ago • 4 comments

I see this output from kibit:

[null] Consider:
  warn
instead of:
  #(warn %)

However taking kibit's advice results in the following compile error:

Can't take value of a macro: #'clojure.tools.logging/warn

I guess the unneeded functions section of misc.clj needs to understand the difference between a function invocation and a macro invocation.

joelittlejohn avatar Sep 10 '12 12:09 joelittlejohn

Another problem (similar) is that Java constructs are considered to be first-class functions, which they are not. So kibit currently recommends this:

[null] Consider:
  Boolean.
instead of:
  #(Boolean. %)

But this is not correct, because Boolean. is not a function. It must be wrapped in a lambda to be passed around as a function. Similarly java interop calls like .toString are also not functions. So the following examples don't work:

(apply .toString [123])

(map Boolean. ["true" "false"])

but these do work:

(apply #(.toString %) [123])

(map #(Boolean. %) ["true" "false"])

joelittlejohn avatar Nov 26 '12 13:11 joelittlejohn

This is a fundamental limitation of kibit. I don't think there's a way to work around this but I'd be happy to hear ideas.

danielcompton avatar Nov 09 '14 19:11 danielcompton

@danielcompton, I don't currently follow how the lack of macro expansion (that I think you're referencing from the readme) means that this cannot be solved. Would it not be possible to e.g. change the rule so that any function name that begins or ends with . is not considered eligible for this rule?

Sorry if I've missed what you're getting at here.

Cheers

joelittlejohn avatar Nov 10 '14 22:11 joelittlejohn

Gotcha. We can't cover the general case of all macros but it should be possible to exclude sugared Java interop.

danielcompton avatar Nov 11 '14 00:11 danielcompton