Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle case where guard does not produce variable, but a . expr #295

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions lib/elixir_sense/core/guard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ defmodule ElixirSense.Core.Guard do
{guard_predicate, _, params} = node, acc ->
case guard_predicate_type(guard_predicate, params, state) do
{type, binding} ->
{var, _, nil} = binding
# If we found the predicate type, we can prematurely exit traversing the subtree
{[], [{var, type} | acc]}
case binding do
# If we found the predicate type, we can prematurely exit traversing the subtree
{var, _, nil} ->
{[], [{var, type} | acc]}

_ ->
{node, acc}

end

nil ->
{node, acc}
Expand All @@ -61,7 +67,7 @@ defmodule ElixirSense.Core.Guard do
end

defp guard_predicate_type(p, params, _)
when p in [:is_number, :is_float, :is_integer, :round, :trunc, :div, :rem, :abs],
when p in [:is_number, :is_float, :is_integer, :round, :trunc, :div, :rem, :abs, :floor, :ceil],
do: {:number, hd(params)}

defp guard_predicate_type(p, params, _) when p in [:is_binary, :binary_part],
Expand Down
18 changes: 17 additions & 1 deletion test/elixir_sense/core/metadata_builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
&1,
abc,
cde = 1,
record_env()
record_env()
]
record_env()
"""
Expand Down Expand Up @@ -1214,6 +1214,22 @@ defmodule ElixirSense.Core.MetadataBuilderTest do
%VarInfo{name: :abc, positions: [{2, 11}, {3, 10}]}
] = state |> get_line_vars(3)
end

test "guards referencing `map.field` don't have match errors" do
logs =
ExUnit.CaptureLog.capture_log(fn ->
"""
defmodule My do
def foo(abc) when is_atom(abc.foo) do
record_env()
end
end
"""
|> string_to_state
end)

refute String.contains?(logs, "MatchError")
end
end

@tag requires_source: true
Expand Down
Loading