-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Labels
Description
In the following example, Style/Alias breaks code.
$ cat /tmp/alias.rb
class Foo
attr_reader :root_foo
def initialize(name: 'bar')
@root_foo = 'foo'
alias :"root_#{name}" :root_foo
alias :"root_#{name}_2" :root_foo
end
end
foo = Foo.new
p foo.root_foo
p foo.root_bar
$ ruby /tmp/alias.rb
"foo"
"foo"
$ bundle exec rubocop -x --only Style/Alias /tmp/alias.rb
Offenses:
/tmp/alias.rb:7:5: C: [Corrected] Style/Alias: Use alias_method instead of alias.
alias :"root_#{name}" :root_foo
^^^^^
/tmp/alias.rb:8:5: C: [Corrected] Style/Alias: Use alias_method instead of alias.
alias :"root_#{name}_2" :root_foo
^^^^^
1 file inspected, 2 offenses detected, 2 offenses corrected
$ cat /tmp/alias.rb
class Foo
attr_reader :root_foo
def initialize(name: 'bar')
@root_foo = 'foo'
alias_method :"root_#{name}", :root_foo
alias_method :"root_#{name}_2", :root_foo
end
end
foo = Foo.new
p foo.root_foo
p foo.root_bar
$ ruby /tmp/alias.rb
/tmp/alias.rb:7:in `initialize': undefined method `alias_method' for an instance of Foo (NoMethodError)
alias_method :"root_#{name}", :root_foo
^^^^^^^^^^^^
from /tmp/alias.rb:12:in `new'
from /tmp/alias.rb:12:in `<main>'
Expected behavior
Valid code.
Actual behavior
Invalid code.
Steps to reproduce the problem
See above. It is worth noting that when there's only one alias, nothing happens (no diagnostic, and of course, no rewriting). There must be some state somewhere that should be cleared.
Cheers!
RuboCop version
$ bundle exec rubocop -V
1.65.1 (using Parser 3.3.4.2, rubocop-ast 1.32.0, running on ruby 3.3.4) [arm64-darwin23]
- rubocop-performance 1.21.1
Reactions are currently unavailable