Skip to content

Commit

Permalink
Fix a false positive for Style/Aliascop when alias in a method def
Browse files Browse the repository at this point in the history
The following cases will result in a NoMethodError
if replaced with alias_method.
Therefore, it is correct to regard it
as a violation and not to replace it.

```ruby
def foo
  alias :ala :bala
end
```
  • Loading branch information
ydah committed Nov 1, 2022
1 parent 2b03227 commit 05029d9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog/fix_fix_a_false_positive_for_style_alias.md
@@ -0,0 +1 @@
* [#11014](https://github.com/rubocop/rubocop/pull/11014): Fix a false positive for `Style/Alias`cop when alias in a method def. ([@ydah][])
10 changes: 9 additions & 1 deletion lib/rubocop/cop/style/alias.rb
Expand Up @@ -7,6 +7,11 @@ module Style
# depending on configuration.
# It also flags uses of `alias :symbol` rather than `alias bareword`.
#
# However, it will always enforce `method_alias` when used `alias`
# in an instance method definition and in a singleton method definition.
# If used in a block, always enforce `alias_method`
# unless it is an `instance_eval` block.
#
# @example EnforcedStyle: prefer_alias (default)
# # bad
# alias_method :bar, :foo
Expand All @@ -22,6 +27,7 @@ module Style
#
# # good
# alias_method :bar, :foo
#
class Alias < Base
include ConfigurableEnforcedStyle
extend AutoCorrector
Expand Down Expand Up @@ -71,7 +77,9 @@ def alias_keyword_possible?(node)
end

def alias_method_possible?(node)
scope_type(node) != :instance_eval && node.children.none?(&:gvar_type?)
scope_type(node) != :instance_eval &&
node.children.none?(&:gvar_type?) &&
node&.parent&.type != :def
end

def add_offense_for_args(node, &block)
Expand Down
42 changes: 36 additions & 6 deletions spec/rubocop/cop/style/alias_spec.rb
Expand Up @@ -106,22 +106,52 @@ module M
RUBY
end

it 'does not register an offense for alias_method with explicit receiver' do
it 'does not register registers an offense for alias in a def' do
expect_no_offenses(<<~RUBY)
class C
receiver.alias_method :ala, :bala
def foo
alias :ala :bala
end
RUBY
end

it 'does not register an offense for alias_method in a method def' do
expect_no_offenses(<<~RUBY)
def method
it 'registers an offense for alias in a defs' do
expect_offense(<<~RUBY)
def some_obj.foo
alias :ala :bala
^^^^^ Use `alias_method` instead of `alias`.
end
RUBY

expect_correction(<<~RUBY)
def some_obj.foo
alias_method :ala, :bala
end
RUBY
end

it 'registers an offense for alias in a block' do
expect_offense(<<~RUBY)
included do
alias :ala :bala
^^^^^ Use `alias_method` instead of `alias`.
end
RUBY

expect_correction(<<~RUBY)
included do
alias_method :ala, :bala
end
RUBY
end

it 'does not register an offense for alias_method with explicit receiver' do
expect_no_offenses(<<~RUBY)
class C
receiver.alias_method :ala, :bala
end
RUBY
end

it 'does not register an offense for alias_method in self.method def' do
expect_no_offenses(<<~RUBY)
def self.method
Expand Down

0 comments on commit 05029d9

Please sign in to comment.