Repository: rrrene/credo (elixir)

Issue:
False positive in `Credo.Check.Refactor.Apply`
### Environment

* Credo version (`mix credo -v`): 1.7.7
* Erlang/Elixir version (`elixir -v`): 1.16.3
* Operating system: Linux

With the following snippet of code I get a `Credo.Check.Refactor.Apply` warning in functuion `foo2` (wrong) while no warning for function `foo1` (correct).

```elixir
defmodule A do
  def foo1(args) do
    apply(Bar, :baz, [1 | args])
  end

  def foo2(args) do
    apply(Bar, :baz, [1, 2 | args])
  end
end
```




Pull request:
False positive in Credo.Check.Refactor.Apply
Fixes #1151 

Patch:
diff --git a/lib/credo/check/refactor/apply.ex b/lib/credo/check/refactor/apply.ex
index a4c0dba0..ad43e824 100644
--- a/lib/credo/check/refactor/apply.ex
+++ b/lib/credo/check/refactor/apply.ex
@@ -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


Test patch:
diff --git a/test/credo/check/refactor/apply_test.exs b/test/credo/check/refactor/apply_test.exs
index 6748be73..032f3f65 100644
--- a/test/credo/check/refactor/apply_test.exs
+++ b/test/credo/check/refactor/apply_test.exs
@@ -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
@@ -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
     """