Elixir: Compiler doesn't warn when has only 1 ignore expression statement in function

Created on 17 Aug 2020  路  4Comments  路  Source: elixir-lang/elixir

I apologize first if this is correct behavior.

Precheck

  • Do not use the issue 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: https://groups.google.com/group/elixir-lang-core
  • For bugs, do a quick search and make sure the bug has not yet been reported
  • Please disclose security vulnerabilities privately at [email protected]
  • Finally, be nice and have fun!

Environment

  • Elixir & Erlang/OTP versions (elixir --version): Elixir 1.11.0-dev (82c4c36) (compiled with Erlang/OTP 23)
  • Operating system: macOS 10.15.6

Current behavior

NOTE: The example of reproduce code founded in elixir-lsp/elixir-ls.

Consider this code:

defmodule IgnorePattern do
  def ignore_var() do
    _otp_release = String.to_integer(System.otp_release())
    {_compiled_with, _} = System.build_info() |> Map.fetch!(:otp_release) |> Integer.parse()
  end

  def ignore_var_not_warning() do
    _otp_release = String.to_integer(System.otp_release())
  end
end

When try compile with mix clean --all && mix compile. The result is:

Compiling 1 file (.ex)
warning: the result of the expression is ignored (suppress the warning by assigning the expression to the _ variable)
  lib/ignore_pattern.ex:3

Generated ignore_pattern app

Expected behavior

The _otp_release inside a function ignore_var_not_warning/0 should has a warning message as same as _otp_release in ignore_var/0 has. The current of compiler hasn't.

All 4 comments

The behavior is as expected. In the function ignore_var the result of String.to_integer(System.otp_release()) will never be used and in ignore_var_not_warning the value will be used as the return value of the function.

Closing per the reasons explained by @NickNeck. The result of expression is being used on the second case (as return value).

@josevalim @NickNeck Thanks for your clarify. And may I asks you a few questions:

  • If the language complaining _otp_release in ignore_var, so why the compiler doesn't complain the primitive value? For example:
def ignore_var_2() do
  _otp_release = 1
  {_compiled_with, _} = System.build_info() |> Map.fetch!(:otp_release) |> Integer.parse()
end

AFAIK It's a heuristic in the Erlang compiler where it assumes such constants might be a result of some configuration and warning on those would be too annoying.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Irio picture Irio  路  3Comments

ckampfe picture ckampfe  路  3Comments

alexrp picture alexrp  路  4Comments

vothane picture vothane  路  3Comments

lukaszsamson picture lukaszsamson  路  3Comments