Skip to content

Commit 56bc730

Browse files
authored
Avoid using Keyword.pop/2 in ExUnit.Diff.struct_module/2 (#15489)
`struct_module` is being called for both structs and maps. `diff_quoted_struct` uses the return of `struct_module` to determine when to call `diff_quoted_struct` or `diff_map`.
1 parent fd1109e commit 56bc730

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

lib/ex_unit/lib/ex_unit/diff.ex

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -765,16 +765,20 @@ defmodule ExUnit.Diff do
765765
end
766766
end
767767

768+
# if kw represents a struct, this returns the result of `struct.__info__(:struct)`, otherwise nil
768769
defp struct_module(kw) do
769-
{struct, struct_kw} = Keyword.pop(kw, :__struct__)
770-
771-
info =
772-
is_atom(struct) and struct != nil and
773-
Code.ensure_loaded?(struct) and function_exported?(struct, :__info__, 1) and
774-
struct.__info__(:struct)
770+
case Enum.split_with(kw, fn {k, _} -> k == :__struct__ end) do
771+
{[{_, struct} | _], struct_kw} when is_atom(struct) and struct != nil ->
772+
info =
773+
Code.ensure_loaded?(struct) and function_exported?(struct, :__info__, 1) and
774+
struct.__info__(:struct)
775+
776+
if info && Enum.all?(struct_kw, fn {k, _} -> Enum.any?(info, &(&1.field == k)) end) do
777+
struct
778+
end
775779

776-
if info && Enum.all?(struct_kw, fn {k, _} -> Enum.any?(info, &(&1.field == k)) end) do
777-
struct
780+
_ ->
781+
nil
778782
end
779783
end
780784

0 commit comments

Comments
 (0)