exercism / elixir-analyzer

GNU Affero General Public License v3.0
30 stars 32 forks source link

Suggests that arty zero anon functions are converted to short form. #282

Closed chriseyre2000 closed 2 years ago

chriseyre2000 commented 2 years ago

Here is the sample code (from Community Garden):

  def start() do
    Agent.start(fn -> %{} end)
  end

This is the warning generated:

When passing an existing function as an argument, make sure to capture it instead of creating a new anonymous function.

# preferred: using capture notation

&%{}/0

# discouraged: creating new anonymous functions

fn -> %{}() end
jiegillet commented 2 years ago

I think I get what's going on

quote do
  %{}
end
# => {:%{}, [], []}

In the AST, %{} looks like a function. <<>> and {} do the same, and we have a way of dealing with those

https://github.com/exercism/elixir-analyzer/blob/57a3a64f697924d3b4bcdb928c3635f1e7ad3685/lib/elixir_analyzer/exercise_test/common_checks/function_capture.ex#L44

It should be an easy fix.