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

False positive in Credo.Check.Refactor.Apply #1152

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading