This check/guard will always yield the same result

Hey,

I am trying to build a Plug pipeline, it’s pretty simple and works great, my function is the last in the chain and I am getting this warning when I am trying to run in an env with empty list as param.

I am getting the env
@unavailable_countries Application.get_env(:app, :unavailable_countries, [])
The warning is present if I explicitly set the env as a [].

  defp is_available?(%{
         city: %{
           country: %{iso_code: iso_code}
         }
       })
       when iso_code in @unavailable_countries do
    false
  end

  defp is_available?(_), do: true

There is another warning for the iso_code that it is unused.

I know I could pass the list to the function and check, but I have other clauses as well with other lists, and then I couldn’t do it in the guard.

Is there a way to go around this?
I know that it will always yield the same result, I understand the warning.

Thanks

Well I can put in [""] as the default for one.

Which warning?

Sorry I put it as the title:
This check/guard will always yield the same result

Yes, the compiler is warning you that the guard will always fail, because no input will ever satisfy it.

1 Like

So ideally should I write the function differently, or for dev environment passing just an empty string in as a possibility is fine?

You can conditionally add the first head:

if @unavailable_countries != [] do
  defp is_available?(%{
         city: %{
           country: %{iso_code: iso_code}
         }
       })
       when iso_code in @unavailable_countries do
    false
  end
end

  defp is_available?(_), do: true

Or simplify it to:

  defp is_available?(%{
         city: %{
           country: %{iso_code: iso_code}
         }
       }), do: iso_code not in @unavailable_countries

  defp is_available?(_), do: true
4 Likes

I will try the first thanks!
I think the simplified version wouldn’t work because I only want it to return if the item is in the list otherwise not, but this could return both true or false.

Edit: the check with the if around the function works, thanks!

I have fixed the “fixed” example to match your needs.

1 Like