dmarcoux

dmarcoux

How to fix this Dialyzer warning "The guard clause can never succeed."

Hi,

I started contributing to a project where the CI checks for Dialyzer warnings. There is this code where I inverted the condition and branches of an if/else statement to make it a tiny bit easier to read.

The code in question:

  def create_friend_request(from_user_id, to_user_id) do
    {allowed, reason} = can_send_friend_request_with_reason?(from_user_id, to_user_id)

    if allowed do
      (...)
    else
      (...)
    end
  end

Previously, it was if not allowed do and Dialyzer was fine with it. Now, Dialyzer emits this warning for the line if allowed do: The guard clause can never succeed.

This is how can_send_friend_request_with_reason? is defined:

  @spec can_send_friend_request_with_reason?(T.userid(), T.userid()) ::
          {true, :ok} | {false, String.t()}
  defdelegate can_send_friend_request_with_reason?(from_id, to_id), to: FriendRequestLib

I don’t have much experience with Dialyzer, so I wonder if I am missing something obvious?

In case you need more context than this, this is all happening in this PR. Thank you :slight_smile:

Most Liked

NobbZ

NobbZ

Without your refactor but with these lines removed (teiserver/.dialyzer_ignore.exs at main · beyond-all-reason/teiserver · GitHub) is your line or function being warned about?

Given the nearly 220 lines long dialyzer ignore, they can as well remove dialyzer completely.

Until proven otherwise, I assume that your refactor just uncovered an issue again that was hidden before.

Instead of silencing all the warnings, they should get fixed.

NobbZ

NobbZ

The problem with ignored dialyzer warnings, they bubble up, and will explode elsewhere eventually.

So if there is an ignored warning somewhere in the callchain, it might now just blow up, though without knowing what that ignored warning is, its hard to tell why it blows after the refactor but not before.

Personally I refuse to accomodate dialyzer in projects like this. If dialyzer is used and “enforced”, then there shouldn’t be any ignored warnings, and if there are, there should be a comment explaining every single entry in the ignore file.

In cases like the one you have now, I would just add an entry to the ignore file.

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31307 143
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

We're in Beta

About us Mission Statement