a = 23
if(true, do: a = 4)
warning: variable "a" is unused
Note variables defined inside case, cond, fn, if and similar do not leak. If you want to conditionally override an existing variable "a", you will have to explicitly return the variable. For example:
if some_condition? do
atom = :one
else
atom = :two
end
should be written as
atom =
if some_condition? do
:one
else
:two
end
Unused variable found at:
opened 10:46AM - 27 Feb 18 UTC
closed 02:49PM - 27 Feb 18 UTC
### Precheck
* Do not use the issues tracker for help or support (try Elixir … Forum, Stack Overflow, IRC, etc.)
* For proposing a new feature, please start a discussion on the Elixir Core mailing list
* For bugs, do a quick search and make sure the bug has not yet been reported
* Finally, be nice and have fun!
### Environment
* Elixir & Erlang versions (elixir --version): Erlang 19.3 release, see below for Elixir version.
* Operating system: Ubuntu Linux 16.04.3, Amazon Linux amazonlinux:2017.03.1.20170812
### Current behavior
```
template_path = ""
if (config[:app] == :exlam) do
template_path = Path.join(["./priv", "templates", "#{name}.eex"])
else
template_path = Path.join(["#{:code.priv_dir(:exlam)}", "templates", "#{name}.eex"])
end
```
Elixir 1.7.0-dev (cbcdcb1623be11e1ff0bd0bd46cb812c563c3bbd), on Ubuntu Linux 16.04.3, template_path is set to the path of the file.
Elixir 1.7.0-dev (4e3e9cefc879b9173461b5a58f766390de9f9989), on Amazon Linux amazonlinux:2017.03.1.20170812, template_path is "".
Fix in code:
```
template_path = ""
template_path = if (config[:app] == :exlam) do
template_path = Path.join(["./priv", "templates", "#{name}.eex"])
IO.puts "path is exlam #{template_path}"
template_path
else
template_path = Path.join(["#{:code.priv_dir(:exlam)}", "templates", "#{name}.eex"])
IO.puts "path is not exlam #{template_path}"
template_path
end
```
Include code samples, errors and stacktraces if appropriate.
### Expected behavior
Not sure. Should assignment from within the if be supported? (This was code I found in an existing library.)
elixir/CHANGELOG.md at v1.7 · elixir-lang/elixir · GitHub
[Kernel] Raise on unsafe variables in order to allow us to better track unused variable
Release v1.7.0 · elixir-lang/elixir · GitHub
[Kernel] Raise on unsafe variables in order to allow us to better track unused variables (also known as imperative assignment / variable leakage)
5 Likes