Compiler warning about unsafe variable no more present?

Hi everybody,
I’m reading a book (Programming Elixir) where it’s stated that the following code:

line_no = 50
# ...
if (line_no == 50) do
  IO.puts "new page\f"
  line_no = 0
end

IO.puts line_no

gives the following warning:

warning: the variable "line_no" is unsafe as it has been set inside one of: case, cond, receive, if, and, or, &&, ||. Please explicitly return the variable value instead. ...

I tried that code but I don’t have any warnings.
But since I tried with latest elixir version, I thought that it might be a warning in previous versions of elixir.
So I tried to search on Github some literals of the message (for example is unsafe or has been set inside or explicitly return) but I cannot find any result.

For example, I was able to find the warning for an unused variable (in this file lib/elixir/src/elixir_env.erl)

So can anyone explain me where I can find more about this warning?

Thank you very much.

If You try this code in the console You will have an error

iex(1)> line_no = 50    
50
iex(2)> if line_no == 50 do
...(2)> IO.puts "new page\f"
...(2)> line_no = 0
...(2)> end
warning: variable "line_no" is unused (if the variable is not meant to be used, prefix it with an underscore)

Which version of Elixir do You use?

It is also different from Ruby code, mainly because of immutability. But here the problem is with scope. It should be…

line_no = 50
line_no = if line_no == 50, do: 0, else: line_no

Hello,
I probably didn’t explain very well…
But I’m not talking about “unused” variable which is actually displayed as a warning. But about unsafe variable…
If you ran that code with the latest version of elixir as me, it also probably didn’t show up…

Hence my question to be fair…

This topic might be related…

Up to version 1.7 the variable reassignment would have “leaked”, and a warning was emitted from 1.3.

Since 1.8 behaviour has changed, the reassignment does not leak any more and no warning is emitted.

Since the reassignment does not leak anymore, the warning wouldn’t make sense anyway.

(Versions are from memory, I might be slightly off)

1 Like

I just found the following commit (chose the commit tabs on the search result page) where it seems that it was removed here: https://github.com/elixir-lang/elixir/commit/fd149eeb563494ca494e149b5a154d7ed6431c3e

I don’t know why the search don’t include all the branches or how to search on a particular branch only because I chose the v1.0 branch and did the search there but according to the URL of the search it was only for master, anyway…)

Then I looked on the current file here:

So it seems that this warning is still here and should apply…

Any ideas what’s going on?

How is that even possible ?

That’s not a current commit. I checked the tags, and the warning is in 1.6.6, but not in 1.7.0.

Scoping rules were different back then.

1 Like

Afaik github only does searches on master and nothing else.

1 Like

Yes, I was on mobile at this moment I should have did wrong copy/paste…

Yeah, I only noticed that now… It would have been nice if search was scoped to branches and also in the history (in older commits).
Though, for the later, since search results also displays commits, we can still found on history by checking one at a time on the history commits.

Ok that makes sense!
So this is not applicable anymore.
The book I’m reading cover elixir from 1.6 hence the error at this moment.

Anyway, in the book they described the reason behind this warning because of the high probability “to forget to initialize the variable outside the block, but to then rely on it having a value after the block”.

And I think that if this was the only reason, it made sense and it makes sense to still have the warning imho…