Skip to content

Instantly share code, notes, and snippets.

@hrubi
Last active September 25, 2022 19:57
Show Gist options
  • Save hrubi/79036b4f589c6a54d4d6289259c664ab to your computer and use it in GitHub Desktop.
Save hrubi/79036b4f589c6a54d4d6289259c664ab to your computer and use it in GitHub Desktop.
Elixir scoping changes
leak1.ex:
defmodule Leak1 do
def leak do
x = 0
if true do
x = 1
end
x
end
end
IO.puts(Leak1.leak)
leak2.ex:
defmodule Leak2 do
def leak do
if true do
x = 1
end
x
end
end
IO.puts(Leak2.leak)
Commit 212bfae (https://github.com/elixir-lang/elixir/commit/212bfae):
% ./bin/elixir leak1.ex
warning: the variable "x" is unsafe as it has been set inside one of: case, cond, receive, if, and, or, &&, ||. Please explicitly return the variable value instead. For example:
case integer do
1 -> atom = :one
2 -> atom = :two
end
should be written as
atom =
case integer do
1 -> :one
2 -> :two
end
Unsafe variable found at:
leak1.ex:7
1
% ./bin/elixir leak2.ex
warning: the variable "x" is unsafe as it has been set inside one of: case, cond, receive, if, and, or, &&, ||. Please explicitly return the variable value instead. For example:
case integer do
1 -> atom = :one
2 -> atom = :two
end
should be written as
atom =
case integer do
1 -> :one
2 -> :two
end
Unsafe variable found at:
leak2.ex:6
1
Commit 113d2c6 (https://github.com/elixir-lang/elixir/commit/113d2c6):
% ./bin/elixir leak1.ex
warning: the variable "x" is unsafe as it has been set inside one of: case, cond, receive, if, and, or, &&, ||. Please explicitly return the variable value instead. For example:
case integer do
1 -> atom = :one
2 -> atom = :two
end
should be written as
atom =
case integer do
1 -> :one
2 -> :two
end
Unsafe variable found at:
leak1.ex:7
1
% ./bin/elixir leak2.ex
warning: variable "x" does not exist and is being expanded to "x()", please use parentheses to remove the ambiguity or change the variable name
leak2.ex:6
warning: variable "x" is unused
leak2.ex:4
** (CompileError) leak2.ex:6: undefined function x/0
(stdlib) lists.erl:1338: :lists.foreach/2
leak2.ex:1: (file)
Commit 1ba04d1 (https://github.com/elixir-lang/elixir/commit/1ba04d1):
% ./bin/elixir leak1.ex
warning: variable "x" is unused
leak1.ex:5
0
% ./bin/elixir leak2.ex
warning: variable "x" does not exist and is being expanded to "x()", please use parentheses to remove the ambiguity or change the variable name
leak2.ex:6
warning: variable "x" is unused
leak2.ex:4
** (CompileError) leak2.ex:6: undefined function x/0
(stdlib) lists.erl:1338: :lists.foreach/2
leak2.ex:1: (file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment