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

Macroexpand hook with # functions reader macro #1779

Closed
markgdawson opened this issue Aug 17, 2022 · 1 comment
Closed

Macroexpand hook with # functions reader macro #1779

markgdawson opened this issue Aug 17, 2022 · 1 comment
Projects

Comments

@markgdawson
Copy link

markgdawson commented Aug 17, 2022

version

I'm running clj-kondo 2022.08.03 on the JVM in Linux.

problem

I'm used the macroexpand hook to expand the try-let macro. My macro implementation is mostly good enough and is working for me in most cases. But clj-kondo produces errors when there is a function defined by #(...) in the expansion of the macro. It seems to me that clj-kondo is failing to recognise reader macro functions when using the macroexpand hook.

repro

EDIT: here is a minimal project which this repro in runnable form: https://github.com/markgdawson/clj-kondo-reader-macro-repro.
Run with clj -M:clj-kondo.

(ns core
  (:require [try-let :refer [try-let]]))

(try-let [a 10]
         #(+ a 2)
         (catch Throwable e e))

In this case I get clj-kondo error on this macro saying unused-binding a.

For completeness, I use try-let in deps.edn as: try-let/try-let {:mvn/version "1.3.1"}.

The warning goes away with:

(try-let [a 10]
         a
         #(+ a 2)
         (catch Throwable e e))

and with

(try-let [a 10]
         (fn [] (+ a 2))
         (catch Throwable e e))

config
./.clj-kondo/configs/try-let/config.edn

{:hooks
 {:macroexpand {try-let/try-let clj-kondo.try-let.try-let/try-let}}}

./.clj-kondo/configs/try-let/clj_kondo/try_let/try_let.clj

(ns clj-kondo.try-let.try-let)

(defn expression-type [body]
  (if (and (seq? body)
           (#{'catch 'finally} (first body)))
    :catches
    :body))

(defmacro try-let
  [bindings & body]
  (let [{:keys [body catches]} (group-by expression-type body)]
    `(try (let [~@bindings] ~@body)
          ~@catches)))

expected behavior

I expected clj-kondo not to throw errors for valid/correct code expansions which use the # function reader macro.

markgdawson added a commit to markgdawson/clj-kondo-reader-macro-repro that referenced this issue Aug 17, 2022
@borkdude
Copy link
Member

I can see the error here locally too, thanks for making such a good repro.

@borkdude borkdude added this to Needs triage in clj-kondo via automation Aug 17, 2022
@borkdude borkdude moved this from Needs triage to High priority (next release) in clj-kondo Aug 17, 2022
clj-kondo automation moved this from High priority (next release) to Done Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
clj-kondo
  
Done
Development

No branches or pull requests

2 participants