Skip to content

Commit

Permalink
Use guards as helpers instead of hardcoding constants
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jan 25, 2025
1 parent adcb7c6 commit bdd569e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
18 changes: 9 additions & 9 deletions lib/elixir/lib/module/types/apply.ex
Original file line number Diff line number Diff line change
Expand Up @@ -321,14 +321,13 @@ defmodule Module.Types.Apply do

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

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

def remote_domain(mod, fun, args, expected, meta, stack, context) do
Expand Down Expand Up @@ -419,7 +418,7 @@ defmodule Module.Types.Apply do
end
end

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

cond do
skip? ->
not check? ->
{:ok, result}

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

defp remote_apply({:compare, name, skip?}, [left, right], stack) do
defp remote_apply({:compare, name, check?}, [left, right], stack) do
result = return(boolean(), [left, right], stack)

cond do
skip? ->
not check? ->
{:ok, result}

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

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

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

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

if stack.mode == :traversal or info == :none do
{{update_used?, :none}, List.duplicate(term(), arity), context}
Expand Down
6 changes: 3 additions & 3 deletions lib/elixir/lib/module/types/expr.ex
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,7 @@ defmodule Module.Types.Expr do
{head_type, context} = of_expr(head, @pending, head, stack, context)

context =
if stack.mode in [:infer, :traversal] do
context
else
if is_warning(stack) do
case truthness(head_type) do
:always_true when not last? ->
warning = {:badcond, "always match", head_type, head, context}
Expand All @@ -295,6 +293,8 @@ defmodule Module.Types.Expr do
_ ->
context
end
else
context
end

{body_type, context} = of_expr(body, expected, expr, stack, context)
Expand Down
5 changes: 5 additions & 0 deletions lib/elixir/lib/module/types/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ defmodule Module.Types.Helpers do

## AST helpers

@doc """
Returns true if the mode cares about warnings.
"""
defguard is_warning(stack) when stack.mode not in [:traversal, :infer]

@doc """
Guard function to check if an AST node is a variable.
"""
Expand Down

0 comments on commit bdd569e

Please # to comment.