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

Expand fn literals at threading macroexpand time #2282

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tomdl89
Copy link
Contributor

@tomdl89 tomdl89 commented Feb 18, 2024


This PR takes the functionality from #2211 which wasn't implemented in #2243.
Namely the work to thread into fn literals (#() forms) so invalid names and unused values can be detected.

Before this PR, the following code would not raise a linting error:

(-> :foo #(name %))

But after it, it raises a "Function name must be simple symbol but got: :foo" error.
This makes sense because the macroexpansion of the above form looks like so:

(macroexpand '(-> :foo #(name %)))
;; => (fn* :foo [p1__23143#] (name p1__23143#))

Also, before this PR, the following code would not raise a linting error:

(->> :foo #(name %))

But after it, it raises an "Unused value" error.
This makes sense because the macroexpansion of the above form looks like so:

(macroexpand '(->> :foo #(name %)))
;; => (fn* [p1__23147#] (name p1__23147#) :foo)

The PR is split into two commits, which should be squashed:

  1. Expand fn literals at threading macroexpand time
    This is the functional changes:
    a. Add separate treatment for :fn forms vs other non-lists in expand-> and expand->>
    b. Move % anaphoric argument handling earlier in analyze-fn
    c. Add fn* to the list of core-syms checked by analyze-usages2 and lint-var-usage (as this is what fn literals expand to)
  2. Move expand-fn code & add tests
    I think it's clearer to move the expand-fn code and its dependencies as a separate step, to show that nothing in it is changed. Added tests & updated the changelog.

So values can be threaded in and checked.
Tests and moving of expand-fn code in separate commit.
@borkdude
Copy link
Member

It seems kind of arbitrary to expand a fn literal specifically in -> or ->> (only such that it gives a better message in some cases). I'll have to think a bit more about this.

@tomdl89
Copy link
Contributor Author

tomdl89 commented Feb 26, 2024

Thanks for taking a look @borkdude. Fair point that it's arbitrary to expand fn literals only in -> and ->>. I've worked on a new approach which expands the literals earlier in the call stack: before the enclosing list is processed. This means values can be added to the fn* body by any macro which takes the fn literal as a direct child, which covers both these threading macros, and any others like cond->. Let me know if that's what you were thinking of: master...tomdl89:clj-kondo:fn-literal-earlier-expansion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants