-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Closed
Labels
Description
Environment
- Elixir & Erlang/OTP versions (elixir --version): 1.11.2 / 23.1.1
- Operating system: Linux 5.9.3-arch1-1 If-statement in body causes warning message #1 SMP PREEMPT Sun, 01 Nov 2020 12:58:59 +0000 x86_64 unknown unknown GNU/Linux
Current behavior
We have a function for validating a certain struct, but our doctest seems to generate a warning. The warning itself is obvious, but the test is also to show it's wrong.
defmodule Foo do
defstruct [:bar]
@doc """
Returns if the argument is a valid Foo struct or not.
iex> Foo.is_valid?(%{})
false
iex> Foo.is_valid?(%Foo{})
false
iex> Foo.is_valid?(%Foo{bar: "baz"})
true
"""
@spec is_valid?(any()) :: boolean()
defguard is_valid?(term)
when is_struct(term, __MODULE__) and is_map_key(term, :bar) and not is_nil(term.bar)
end
I think that the code and tests are valid, but the doctest generates this warning:
warning: undefined field "bar" in expression:
# (for doctest at) lib/foo.ex:7
arg0.bar
expected one of the following fields:
where "arg0" was given the type map() in:
# (for doctest at) lib/foo.ex:7
{arg0} = {%{}}
Conflict found at
(for doctest at) lib/foo.ex:7: FooTest."doctest Foo.is_valid?/1 (1)"/1
It will show the error for any type other than %Foo{}
.
Expected behavior
No warnings when running the doctest.