MatijaL
Function which calculates time from now to end date - strange warning from VSCode
Hello,
I created a function which takes timedate as parameter in NaiveDateTime format. This date is an end date and the function has to calculate how much time is left from now to this end date.
def time_remaining(ends_at) when not is_nil(ends_at) do
days_remaining = NaiveDateTime.diff(ends_at, NaiveDateTime.utc_now(), :day)
if days_remaining < 1 do
difference = NaiveDateTime.diff(ends_at, NaiveDateTime.utc_now(), :second)
hours = div(difference, 3600)
minutes = div(difference, 60) - (hours * 60)
seconds = rem(difference, 60)
"#{hours}h : #{minutes}min : #{seconds}s Remaining"
else
"#{days_remaining} Days Remaining"
end
end
The functions works as expected but VSCode is giving me this warning:
Guard test
_@46 ::
{'safe',
binary() |
maybe_improper_list(binary() |
maybe_improper_list(any(),
binary() | []) |
byte(),
binary() | [])} =:=
'nil' can never succeed
Can someone please help me locate the problem?
Marked As Solved
Also Liked
sodapopcan
What version of Elixir are you using? I get no warnings when I compile in 1.14.1.
Regardless, since ends_at must be a NaiveDateTime, it’s better to be explicit about this in the function head as opposed to just checking that it isn’t nil:
def time_remaining(%NaiveDateTime{} = ends_at) do
...
end
or if you prefer when you can do the more verbose:
def time_remaining(ends_at) when is_struct(ends_at, NaiveDateTime) do
...
end
MatijaL
Deleting .elixir_ls folder solved it, thanks for taking the time to help me out here.
sbuttgereit
I haven’t seen this type of Dialyzer warning on a guard call before; but I have seen similar warnings for function parameters when Dialyzer thinks there’s no way for a given pattern match to possibly match a condition… which I sometimes do for a catch all function.
For example if I write something like the below, with no other calls to func_a/1:
def entry_func() do
%{test: "Hi!"}
|> func_a()
end
def func_a(%{} = param) do
# do something
end
def func_a(_) do
raise "Bad Param"
end
I will, with some regularity, see the ElixirLS Dialyzer warning that the second function head for func_a will never run… which looks like it could be similar to what you’re seeing. In this case Dialyzer isn’t exactly wrong… there’s no way for the second func_a/1 function head to match. But it’s not exactly helpful either because I may know now that in the future I might call it from somewhere else where the second function head is helpful.
Anyway, I can’t say for sure this is what you’re seeing, but it looks similar.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









