Skip to content

Commit 0b3dc13

Browse files
committed
Avoid warnings in suite that are now caught by the type system
1 parent 62d3e61 commit 0b3dc13

File tree

4 files changed

+25
-41
lines changed

4 files changed

+25
-41
lines changed

lib/elixir/lib/kernel/special_forms.ex

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,31 +1593,23 @@ defmodule Kernel.SpecialForms do
15931593
15941594
Let's give it a try on IEx:
15951595
1596-
iex> opts = %{width: 10, height: 15}
1597-
iex> with {:ok, width} <- Map.fetch(opts, :width),
1598-
...> {:ok, height} <- Map.fetch(opts, :height) do
1596+
iex> opts = %{"width" => 10, "height" => 15}
1597+
iex> with {:ok, width} <- Map.fetch(opts, "width"),
1598+
...> {:ok, height} <- Map.fetch(opts, "height") do
15991599
...> {:ok, width * height}
16001600
...> end
16011601
{:ok, 150}
16021602
16031603
If all clauses match, the `do` block is executed, returning its result.
16041604
Otherwise the chain is aborted and the non-matched value is returned:
16051605
1606-
iex> opts = %{width: 10}
1607-
iex> with {:ok, width} <- Map.fetch(opts, :width),
1608-
...> {:ok, height} <- Map.fetch(opts, :height) do
1606+
iex> opts = %{"width" => 10}
1607+
iex> with {:ok, width} <- Map.fetch(opts, "width"),
1608+
...> {:ok, height} <- Map.fetch(opts, "height") do
16091609
...> {:ok, width * height}
16101610
...> end
16111611
:error
16121612
1613-
Guards can be used in patterns as well:
1614-
1615-
iex> users = %{"melany" => "guest", "bob" => :admin}
1616-
iex> with {:ok, role} when not is_binary(role) <- Map.fetch(users, "bob") do
1617-
...> {:ok, to_string(role)}
1618-
...> end
1619-
{:ok, "admin"}
1620-
16211613
As in `for/1`, variables bound inside `with/1` won't be accessible
16221614
outside of `with/1`.
16231615
@@ -1661,22 +1653,18 @@ defmodule Kernel.SpecialForms do
16611653
An `else` option can be given to modify what is being returned from
16621654
`with` in the case of a failed match:
16631655
1664-
iex> opts = %{width: 10}
1665-
iex> with {:ok, width} <- Map.fetch(opts, :width),
1666-
...> {:ok, height} <- Map.fetch(opts, :height) do
1667-
...> {:ok, width * height}
1668-
...> else
1669-
...> :error ->
1670-
...> {:error, :wrong_data}
1671-
...>
1672-
...> _other_error ->
1673-
...> :unexpected_error
1674-
...> end
1675-
{:error, :wrong_data}
1656+
with {:ok, content} <- File.read(path),
1657+
:ok <- File.write(path, [content, "!"]) do
1658+
:ok
1659+
else
1660+
{:error, reason} ->
1661+
Logger.error("could not append ! to \#{path} with reason: \#{reason}")
1662+
:error
1663+
end
16761664
16771665
The `else` block works like a `case`: it can have multiple clauses,
1678-
and the first match will be used. Variables bound inside `with` (such as
1679-
`width` in this example) are not available in the `else` block.
1666+
and the first match will be used. Variables bound inside `with`
1667+
(such as `content` in this example) are not available in the `else` block.
16801668
16811669
If an `else` block is used and there are no matching clauses, a `WithClauseError`
16821670
exception is raised.

lib/elixir/lib/map.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ defmodule Map do
301301
302302
iex> Map.fetch(%{a: 1}, :a)
303303
{:ok, 1}
304-
iex> Map.fetch(%{a: 1}, :b)
304+
iex> Map.fetch(%{"foo" => "bar"}, "unknown")
305305
:error
306306
307307
"""
@@ -443,8 +443,8 @@ defmodule Map do
443443
iex> Map.replace!(%{a: 1, b: 2}, :a, 3)
444444
%{a: 3, b: 2}
445445
446-
iex> Map.replace!(%{a: 1}, :b, 2)
447-
** (KeyError) key :b not found in:
446+
iex> Map.replace!(%{"foo" => "bar"}, "unknown", "new_bar")
447+
** (KeyError) key "unknown" not found in:
448448
...
449449
450450
"""

lib/elixir/test/elixir/macro_test.exs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ defmodule MacroTest do
867867
end
868868

869869
test "with/1 (all clauses match)" do
870-
opts = %{width: 10, height: 15}
870+
opts = Process.get(:unused, %{width: 10, height: 15})
871871

872872
{result, formatted} =
873873
dbg_format(
@@ -900,7 +900,7 @@ defmodule MacroTest do
900900
end
901901

902902
test "with/1 (no else)" do
903-
opts = %{width: 10}
903+
opts = Process.get(:unused, %{width: 10})
904904

905905
{result, formatted} =
906906
dbg_format(
@@ -928,7 +928,7 @@ defmodule MacroTest do
928928
end
929929

930930
test "with/1 (else clause)" do
931-
opts = %{width: 10}
931+
opts = Process.get(:unused, %{width: 10})
932932

933933
{result, formatted} =
934934
dbg_format(
@@ -959,7 +959,7 @@ defmodule MacroTest do
959959
end
960960

961961
test "with/1 (guard)" do
962-
opts = %{width: 10, height: 0.0}
962+
opts = Process.get(:unused, %{width: 10, height: 0.0})
963963

964964
{result, formatted} =
965965
dbg_format(
@@ -990,7 +990,7 @@ defmodule MacroTest do
990990
end
991991

992992
test "with/1 (guard in else)" do
993-
opts = %{}
993+
opts = Process.get(:unused, %{})
994994

995995
{result, _formatted} =
996996
dbg_format(

lib/elixir/test/elixir/map_test.exs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,13 @@ defmodule MapTest do
221221
end
222222

223223
test "replace!/3" do
224-
map = %{c: 3, b: 2, a: 1}
224+
map = Process.get(:unused, %{c: 3, b: 2, a: 1})
225225
assert Map.replace!(map, :b, 10) == %{c: 3, b: 10, a: 1}
226226
assert Map.replace!(map, :a, 1) == map
227227

228228
assert_raise KeyError, ~r/key :x not found in:\n\n %{.*a: 1.*}/, fn ->
229229
Map.replace!(map, :x, 10)
230230
end
231-
232-
assert_raise KeyError, "key :x not found in:\n\n %{}\n", fn ->
233-
Map.replace!(%{}, :x, 10)
234-
end
235231
end
236232

237233
test "intersect/2" do

0 commit comments

Comments
 (0)