Elixir: Invalid unused variable warning

Created on 27 Feb 2018  路  2Comments  路  Source: elixir-lang/elixir

Environment

  • Elixir & Erlang versions (elixir --version):
    1.6.1
  • Operating system:
    macos 10.13.3 (17D102)

Current behavior

This test generates warnings about unused variable found.

defmodule UnusedTest do
  use ExUnit.Case

  def fun, do: %{}

  test "unused warning repro" do
    found = fun()
    assert match?(found, %{})
  end
end
warning: variable "found" is unused
  test/projections/unused_test.exs:9

warning: variable "found" is unused
  test/projections/unused_test.exs:10

Expected behavior

No warning, as the variable is actually used in assert's match.

Most helpful comment

The warning is correct. The first argument of match? is a pattern. A variable used in pattern matches any value. Your test is exactly the same as:

  test "unused warning repro" do
    found = fun()
    assert match?(x, %{})
  end

Most probably you wanted:

  test "unused warning repro" do
    found = fun()
    assert match?(^found, %{})
  end

All 2 comments

The warning is correct. The first argument of match? is a pattern. A variable used in pattern matches any value. Your test is exactly the same as:

  test "unused warning repro" do
    found = fun()
    assert match?(x, %{})
  end

Most probably you wanted:

  test "unused warning repro" do
    found = fun()
    assert match?(^found, %{})
  end

Gotcha. Makes sense. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jdeisenberg picture jdeisenberg  路  4Comments

lukaszsamson picture lukaszsamson  路  3Comments

andrewcottage picture andrewcottage  路  3Comments

cmeiklejohn picture cmeiklejohn  路  3Comments

coryodaniel picture coryodaniel  路  3Comments