Project

General

Profile

Actions

Feature #13922

open

Consider showing warning messages about same-named aliases - either directly or perhaps via the "did you mean gem"

Added by shevegen (Robert A. Heiler) about 7 years ago. Updated about 7 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:82886]

Description

I file here rather than at https://github.com/yuki24/did_you_mean because I am not sure
if the site by Yuki Nishijima may be appropriate, so I think first ruby core has to
decide on this.

Consider the following code:

#!/usr/bin/ruby -w
# =========================================================================== #
class Foo
  def initialize
    unused_variable_warning = 42
    bar
  end
  def bar
    puts 'hello from bar'
  end; alias bar1 bar
       alias bar2 bar
       alias bar3 bar
       alias bar3 bar

end

Foo.new

The output will be something like:

foo.rb:7: warning: assigned but unused variable - unused_variable_warning
hello from bar

This is all fine. We get a warning, which is good.

My question is: should the same alias name also cause
a warning?

It is probably unimportant because it is an alias to the very same
method anyway, but I was just wondering in general.

In my opinion it may be better to actually also show a warning
similar to unused variables. But I can not say since perhaps
people may prefer to not see any warning - I think that in most
cases, in the above, though, most people may have made a typo
or so. This is actually how I encountered this, I wanted to add
a new alias and noticed that I have had already two other, same
named aliases. It is no big deal but I was only wondering whether
the above behaviour to not show any warning was just an oversight
or whether it is deliberate.

Updated by znz (Kazuhiro NISHIYAMA) about 7 years ago

In following example, ruby -w warns alias_after_def and duplicated_def only, but rubocop warns all combinations.

% cat /tmp/a.rb
#!/usr/bin/ruby -w
class Foo
  def m
  end

  def alias_after_def
  end

  alias alias_after_def m

  alias def_after_alias m

  def def_after_alias
  end

  alias duplicated_alias m
  alias duplicated_alias m

  def duplicated_def
  end

  def duplicated_def
  end
end
% ruby -w /tmp/a.rb
/tmp/a.rb:9: warning: method redefined; discarding old alias_after_def
/tmp/a.rb:6: warning: previous definition of alias_after_def was here
/tmp/a.rb:22: warning: method redefined; discarding old duplicated_def
/tmp/a.rb:19: warning: previous definition of duplicated_def was here
% rubocop /tmp/a.rb
Inspecting 1 file
W

Offenses:

/tmp/a.rb:1:1: W: Script file a.rb doesn't have execute permission.
#!/usr/bin/ruby -w
^^^^^^^^^^^^^^^^^^
/tmp/a.rb:2:1: C: Missing top-level class documentation comment.
class Foo
^^^^^
/tmp/a.rb:3:3: C: Put empty method definitions on a single line.
  def m ...
  ^^^^^
/tmp/a.rb:6:3: C: Put empty method definitions on a single line.
  def alias_after_def ...
  ^^^^^^^^^^^^^^^^^^^
/tmp/a.rb:9:3: W: Method Foo#alias_after_def is defined at both /tmp/a.rb:6 and /tmp/a.rb:9.
  alias alias_after_def m
  ^^^^^
/tmp/a.rb:13:3: W: Method Foo#def_after_alias is defined at both /tmp/a.rb:11 and /tmp/a.rb:13.
  def def_after_alias
  ^^^
/tmp/a.rb:13:3: C: Put empty method definitions on a single line.
  def def_after_alias ...
  ^^^^^^^^^^^^^^^^^^^
/tmp/a.rb:17:3: W: Method Foo#duplicated_alias is defined at both /tmp/a.rb:16 and /tmp/a.rb:17.
  alias duplicated_alias m
  ^^^^^
/tmp/a.rb:19:3: C: Put empty method definitions on a single line.
  def duplicated_def ...
  ^^^^^^^^^^^^^^^^^^
/tmp/a.rb:22:3: W: Method Foo#duplicated_def is defined at both /tmp/a.rb:19 and /tmp/a.rb:22.
  def duplicated_def
  ^^^
/tmp/a.rb:22:3: C: Put empty method definitions on a single line.
  def duplicated_def ...
  ^^^^^^^^^^^^^^^^^^

1 file inspected, 11 offenses detected
Actions

Also available in: Atom PDF

Like0
Like0