Skip to content

Commit 0974e90

Browse files
committed
Skip errors on head of generated clauses, closes #11407
1 parent 3671ce7 commit 0974e90

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

lib/elixir/lib/module/types/expr.ex

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,12 +469,19 @@ defmodule Module.Types.Expr do
469469
end
470470

471471
defp of_clauses(clauses, stack, context) do
472-
reduce_ok(clauses, context, fn {:->, _meta, [head, body]}, context = acc ->
472+
reduce_ok(clauses, context, fn {:->, meta, [head, body]}, context = acc ->
473473
{patterns, guards} = extract_head(head)
474474

475-
with {:ok, _, context} <- Pattern.of_head(patterns, guards, stack, context),
476-
{:ok, _expr_type, context} <- of_expr(body, :dynamic, stack, context),
477-
do: {:ok, keep_warnings(acc, context)}
475+
case Pattern.of_head(patterns, guards, stack, context) do
476+
{:ok, _, context} ->
477+
with {:ok, _expr_type, context} <- of_expr(body, :dynamic, stack, context) do
478+
{:ok, keep_warnings(acc, context)}
479+
end
480+
481+
error ->
482+
# Skip the clause if it the head has an error
483+
if meta[:generated], do: {:ok, acc}, else: error
484+
end
478485
end)
479486
end
480487

lib/elixir/test/elixir/module/types/types_test.exs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ defmodule Module.Types.TypesTest do
1717
end
1818
end
1919

20+
defmacro generated(ast) do
21+
Macro.prewalk(ast, fn node -> Macro.update_meta(node, &([generated: true] ++ &1)) end)
22+
end
23+
2024
def __expr__({patterns, guards, body}) do
2125
with {:ok, _types, context} <-
2226
Pattern.of_head(patterns, guards, TypeHelper.new_stack(), TypeHelper.new_context()),
@@ -213,6 +217,32 @@ defmodule Module.Types.TypesTest do
213217
"""
214218
end
215219

220+
test "warns on guards from cases unless generated" do
221+
string =
222+
warning(
223+
[var],
224+
[is_integer(var)],
225+
case var do
226+
_ when is_binary(var) -> :ok
227+
end
228+
)
229+
230+
assert is_binary(string)
231+
232+
string =
233+
generated(
234+
warning(
235+
[var],
236+
[is_integer(var)],
237+
case var do
238+
_ when is_binary(var) -> :ok
239+
end
240+
)
241+
)
242+
243+
assert string == :none
244+
end
245+
216246
test "only show relevant traces in warning" do
217247
string = warning([x = y, z], [is_integer(x) and is_binary(y) and is_boolean(z)], {x, y, z})
218248

0 commit comments

Comments
 (0)