Skip to content

Commit 2508d94

Browse files
viralpraxisbbatsov
authored andcommitted
Fix false positives for Lint/DuplicateMethods when the self-alias trick is used
When there's a self alias, there's no need to register an offense: ```ruby class a alias foo foo # or alias_method :foo, :foo def foo; end ``` Using the alias-self trick indicates that developer is trying to avoid "method redefinition" warning.
1 parent 9582c40 commit 2508d94

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#14300](https://github.com/rubocop/rubocop/pull/14300): Fix false positives for `Lint/DuplicateMethods` cop when self-alias trick is used. ([@viralpraxis][])

lib/rubocop/cop/lint/duplicate_methods.rb

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ module Cop
55
module Lint
66
# Checks for duplicated instance (or singleton) method
77
# definitions.
8+
# NOTE: Aliasing a method to itself is allowed, as it indicates that
9+
# the developer intends to suppress Ruby's method redefinition warnings.
10+
# See https://bugs.ruby-lang.org/issues/13574.
811
#
912
# @example
1013
#
@@ -40,6 +43,18 @@ module Lint
4043
#
4144
# alias bar foo
4245
#
46+
# # good
47+
# alias foo foo
48+
# def foo
49+
# 1
50+
# end
51+
#
52+
# # good
53+
# alias_method :foo, :foo
54+
# def foo
55+
# 1
56+
# end
57+
#
4358
# @example AllCops:ActiveSupportExtensionsEnabled: false (default)
4459
#
4560
# # good
@@ -113,19 +128,21 @@ def on_defs(node)
113128

114129
# @!method method_alias?(node)
115130
def_node_matcher :method_alias?, <<~PATTERN
116-
(alias (sym $_name) sym)
131+
(alias (sym $_name) (sym $_original_name))
117132
PATTERN
118133

119134
def on_alias(node)
120-
return unless (name = method_alias?(node))
135+
name, original_name = method_alias?(node)
136+
return unless name && original_name
137+
return if name == original_name
121138
return if node.ancestors.any?(&:if_type?)
122139

123140
found_instance_method(node, name)
124141
end
125142

126143
# @!method alias_method?(node)
127144
def_node_matcher :alias_method?, <<~PATTERN
128-
(send nil? :alias_method (sym $_name) _)
145+
(send nil? :alias_method (sym $_name) (sym $_original_name))
129146
PATTERN
130147

131148
# @!method delegate_method?(node)
@@ -140,7 +157,10 @@ def on_alias(node)
140157
def_node_matcher :sym_name, '(sym $_name)'
141158

142159
def on_send(node) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
143-
if (name = alias_method?(node))
160+
name, original_name = alias_method?(node)
161+
162+
if name && original_name
163+
return if name == original_name
144164
return if node.ancestors.any?(&:if_type?)
145165

146166
found_instance_method(node, name)

spec/rubocop/cop/lint/duplicate_methods_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,17 @@ def some_method
261261
RUBY
262262
end
263263

264+
it "does not register an offense for duplicate self-alias in #{type}" do
265+
expect_no_offenses(<<~RUBY, 'example.rb')
266+
#{opening_line}
267+
alias some_method some_method
268+
def some_method
269+
implement 1
270+
end
271+
end
272+
RUBY
273+
end
274+
264275
it "doesn't register an offense for non-duplicate alias in #{type}" do
265276
expect_no_offenses(<<~RUBY)
266277
#{opening_line}
@@ -284,6 +295,28 @@ def some_method
284295
RUBY
285296
end
286297

298+
it "does not register an offense for duplicate self-alias_method in #{type}" do
299+
expect_no_offenses(<<~RUBY, 'example.rb')
300+
#{opening_line}
301+
alias_method :some_method, :some_method
302+
def some_method
303+
implement 1
304+
end
305+
end
306+
RUBY
307+
end
308+
309+
it "does not register an offense for duplicate self-alias_method with dynamic original name in #{type}" do
310+
expect_no_offenses(<<~RUBY, 'example.rb')
311+
#{opening_line}
312+
alias_method :some_method, unknown()
313+
def some_method
314+
implement 1
315+
end
316+
end
317+
RUBY
318+
end
319+
287320
it "accepts for non-duplicate alias_method in #{type}" do
288321
expect_no_offenses(<<~RUBY)
289322
#{opening_line}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy