Skip to content

Commit

Permalink
False positive in Credo.Check.Refactor.Apply
Browse files Browse the repository at this point in the history
  • Loading branch information
albertored committed Sep 6, 2024
1 parent 1deaa53 commit 014bb5a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
11 changes: 8 additions & 3 deletions lib/credo/check/refactor/apply.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,21 @@ defmodule Credo.Check.Refactor.Apply do
defp issue({:apply, _meta, [{:__MODULE__, _, _}, _fun, _args]}, _issue_meta), do: nil

defp issue({:apply, meta, [fun, args]}, issue_meta) do
do_issue(:apply2, fun, args, meta, issue_meta)
issue(:apply2, fun, args, meta, issue_meta)
end

defp issue({:apply, meta, [_module, fun, args]}, issue_meta) do
do_issue(:apply3, fun, args, meta, issue_meta)
issue(:apply3, fun, args, meta, issue_meta)
end

defp issue(_ast, _issue_meta), do: nil

defp do_issue(_apply, _fun, [{:|, _, _}], _meta, _issue_meta), do: nil
defp issue(tag, fun, args, meta, issue_meta) do
args = if(is_list(args), do: Enum.reverse(args), else: args)
do_issue(tag, fun, args, meta, issue_meta)
end

defp do_issue(_apply, _fun, [{:|, _, _} | _], _meta, _issue_meta), do: nil

defp do_issue(:apply2, {name, _meta, nil}, args, meta, issue_meta)
when is_atom(name) and is_list(args) do
Expand Down
24 changes: 23 additions & 1 deletion test/credo/check/refactor/apply_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ defmodule Credo.Check.Refactor.ApplyTest do
|> to_source_file
|> run_check(@described_check)
|> refute_issues()

"""
defmodule Test do
def some_function(fun, args) do
apply(fun, [:foo, :bar | args])
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> refute_issues()
end

test "it should NOT report violation for apply/3" do
Expand Down Expand Up @@ -76,7 +87,18 @@ defmodule Credo.Check.Refactor.ApplyTest do
"""
defmodule Test do
def some_function(module, fun, args) do
apply(fun, [:foo | args])
apply(module, fun, [:foo | args])
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> refute_issues()

"""
defmodule Test do
def some_function(module, fun, args) do
apply(module, fun, [:foo, :bar | args])
end
end
"""
Expand Down

0 comments on commit 014bb5a

Please sign in to comment.