Skip to content

Commit bdd569e

Browse files
committed
Use guards as helpers instead of hardcoding constants
1 parent adcb7c6 commit bdd569e

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

lib/elixir/lib/module/types/apply.ex

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,13 @@ defmodule Module.Types.Apply do
321321

322322
def remote_domain(:erlang, name, [_left, _right], _expected, _meta, stack, context)
323323
when name in [:>=, :"=<", :>, :<, :min, :max] do
324-
skip? = stack.mode == :infer
325-
{{:ordered_compare, name, skip?}, [term(), term()], context}
324+
{{:ordered_compare, name, is_warning(stack)}, [term(), term()], context}
326325
end
327326

328327
def remote_domain(:erlang, name, [_left, _right] = args, _expected, _meta, stack, context)
329328
when name in [:==, :"/=", :"=:=", :"=/="] do
330-
skip? = stack.mode == :infer or Macro.quoted_literal?(args)
331-
{{:compare, name, skip?}, [term(), term()], context}
329+
check? = is_warning(stack) and not Macro.quoted_literal?(args)
330+
{{:compare, name, check?}, [term(), term()], context}
332331
end
333332

334333
def remote_domain(mod, fun, args, expected, meta, stack, context) do
@@ -419,7 +418,7 @@ defmodule Module.Types.Apply do
419418
end
420419
end
421420

422-
defp remote_apply({:ordered_compare, name, skip?}, [left, right], stack) do
421+
defp remote_apply({:ordered_compare, name, check?}, [left, right], stack) do
423422
result =
424423
if name in [:min, :max] do
425424
union(left, right)
@@ -428,7 +427,7 @@ defmodule Module.Types.Apply do
428427
end
429428

430429
cond do
431-
skip? ->
430+
not check? ->
432431
{:ok, result}
433432

434433
match?({false, _}, map_fetch(left, :__struct__)) or
@@ -446,11 +445,11 @@ defmodule Module.Types.Apply do
446445
end
447446
end
448447

449-
defp remote_apply({:compare, name, skip?}, [left, right], stack) do
448+
defp remote_apply({:compare, name, check?}, [left, right], stack) do
450449
result = return(boolean(), [left, right], stack)
451450

452451
cond do
453-
skip? ->
452+
not check? ->
454453
{:ok, result}
455454

456455
name in [:==, :"/="] and number_type?(left) and number_type?(right) ->
@@ -522,6 +521,7 @@ defmodule Module.Types.Apply do
522521

523522
defp export(module, fun, arity, meta, %{cache: cache} = stack, context) do
524523
cond do
524+
# Cache == nil implies the mode is traversal
525525
cache == nil or stack.mode == :traversal ->
526526
{:none, context}
527527

@@ -630,7 +630,7 @@ defmodule Module.Types.Apply do
630630
{{false, :none}, List.duplicate(term(), arity), context}
631631

632632
{kind, info, context} ->
633-
update_used? = stack.mode not in [:traversal, :infer] and kind == :defp
633+
update_used? = is_warning(stack) and kind == :defp
634634

635635
if stack.mode == :traversal or info == :none do
636636
{{update_used?, :none}, List.duplicate(term(), arity), context}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,7 @@ defmodule Module.Types.Expr do
280280
{head_type, context} = of_expr(head, @pending, head, stack, context)
281281

282282
context =
283-
if stack.mode in [:infer, :traversal] do
284-
context
285-
else
283+
if is_warning(stack) do
286284
case truthness(head_type) do
287285
:always_true when not last? ->
288286
warning = {:badcond, "always match", head_type, head, context}
@@ -295,6 +293,8 @@ defmodule Module.Types.Expr do
295293
_ ->
296294
context
297295
end
296+
else
297+
context
298298
end
299299

300300
{body_type, context} = of_expr(body, expected, expr, stack, context)

lib/elixir/lib/module/types/helpers.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ defmodule Module.Types.Helpers do
44

55
## AST helpers
66

7+
@doc """
8+
Returns true if the mode cares about warnings.
9+
"""
10+
defguard is_warning(stack) when stack.mode not in [:traversal, :infer]
11+
712
@doc """
813
Guard function to check if an AST node is a variable.
914
"""

0 commit comments

Comments
 (0)