Skip to content

Commit f440374

Browse files
committed
Raise when Access syntax used on non-keywords
1 parent 66964cb commit f440374

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

lib/elixir/lib/access.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ defmodule Access do
5353
:maps.find(key, map)
5454
end
5555

56-
def fetch(list, key) when is_list(list) do
56+
def fetch(list, key) when is_list(list) and is_atom(key) do
5757
case :lists.keyfind(key, 1, list) do
5858
{^key, value} -> {:ok, value}
5959
false -> :error

lib/elixir/test/elixir/access_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ defmodule AccessTest do
3636
assert Access.fetch([foo: :bar], :foo) == {:ok, :bar}
3737
assert Access.fetch([foo: :bar], :bar) == :error
3838

39+
assert_raise FunctionClauseError, fn ->
40+
Access.fetch([{"foo", :bar}], "foo")
41+
end
42+
3943
assert Access.get([foo: :bar], :foo) == :bar
4044
assert Access.get_and_update([], :foo, fn nil -> {:ok, :baz} end) == {:ok, [foo: :baz]}
4145
assert Access.get_and_update([foo: :bar], :foo, fn :bar -> {:ok, :baz} end) == {:ok, [foo: :baz]}

0 commit comments

Comments
 (0)