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

Fix Credo.Check.Refactor.Apply #897

Merged
merged 5 commits into from
Oct 30, 2021
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
30 changes: 20 additions & 10 deletions lib/credo/check/refactor/apply.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,33 @@ defmodule Credo.Check.Refactor.Apply do
end
end

defp issue({:apply, meta, [_fun, args]}, issue_meta) when is_list(args),
do: issue_for(issue_meta, meta[:line])

defp issue({:apply, _meta, [_module, {atom, _meta2, nil}, args]}, _issue_meta)
when is_atom(atom) and is_list(args),
do: nil
defp issue({:apply, meta, [fun, args]}, issue_meta) do
do_issue(:apply2, fun, args, meta, issue_meta)
end

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

defp issue(_ast, _issue_meta), do: nil

defp issue_for(issue_meta, line_no) do
defp do_issue(:apply2, {name, _meta, nil}, args, meta, issue_meta)
when is_atom(name) and is_list(args) do
issue_for(meta, issue_meta)
end

defp do_issue(:apply3, fun, args, meta, issue_meta)
when is_atom(fun) and is_list(args) do
issue_for(meta, issue_meta)
end

defp do_issue(_apply, _fun, _args, _meta, _issue_meta), do: nil

defp issue_for(meta, issue_meta) do
format_issue(
issue_meta,
message: "Avoid `apply/2` and `apply/3` when the number of arguments is known",
line_no: line_no
line_no: meta[:line]
)
end
end
32 changes: 29 additions & 3 deletions test/credo/check/refactor/apply_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule Credo.Check.Refactor.ApplyTest do
|> refute_issues()
end

test "it should NOT report violation for apply/4" do
test "it should NOT report violation for apply/3 when fun is a var" do
"""
defmodule Test do
def some_function(module, fun, arg1, arg2) do
Expand All @@ -42,7 +42,33 @@ defmodule Credo.Check.Refactor.ApplyTest do
|> refute_issues()
end

test "it should report a violation for apply/2" do
test "it should NOT report violation for apply/3 when fun is a function" do
~S"""
defmodule Test do
def some_function(module, fun, arg1, arg2) do
apply(module, String.to_exisiting_atom("pre_#{fun}"), [arg1, arg2])
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> refute_issues()
end

test "it should NOT report violation for apply/3 when args is a var" do
~S"""
defmodule Test do
def some_function(args) when is_list(args) do
apply(Module, :fun, args)
end
end
"""
|> to_source_file
|> run_check(@described_check)
|> refute_issues()
end

test "it should report violation for apply/2" do
"""
defmodule Test do
def some_function(fun, arg1, arg2) do
Expand All @@ -58,7 +84,7 @@ defmodule Credo.Check.Refactor.ApplyTest do
test "it should report a violation for apply/3" do
"""
defmodule Test do
def some_function(module, :fun_name, arg1, arg2) do
def some_function(module, arg1, arg2) do
apply(module, :fun_name, [arg1, arg2])
end
end
Expand Down