Skip to content

Commit

Permalink
[Fix rubocop#11591] Fix a false positive for Lint/ToEnumArguments
Browse files Browse the repository at this point in the history
Fixes rubocop#11591.

THis PR fixes a false positive for `Lint/ToEnumArguments`
when enumerator is not created for `__callee__` and `__callee__` methods.
  • Loading branch information
koic committed Feb 18, 2023
1 parent c4916de commit 8912abc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11591](https://github.com/rubocop/rubocop/issues/11591): Fix a false positive for `Lint/ToEnumArguments` when enumerator is not created for `__callee__` and `__callee__` methods. ([@koic][])
8 changes: 6 additions & 2 deletions lib/rubocop/cop/lint/to_enum_arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ def on_send(node)
return unless def_node

enum_conversion_call?(node) do |method_node, arguments|
add_offense(node) unless method_name?(method_node, def_node.method_name) &&
arguments_match?(arguments, def_node)
next if method_node.call_type? &&
!method_node.method?(:__method__) && !method_node.method?(:__callee__)
next if method_name?(method_node, def_node.method_name) &&
arguments_match?(arguments, def_node)

add_offense(node)
end
end

Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/lint/to_enum_arguments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@ def m(x)
RUBY
end

it 'does not register an offense when enumerator is not created for `__method__` and `__callee__` methods' do
expect_no_offenses(<<~RUBY)
def m(x)
return to_enum(never_nullable(value), x)
end
RUBY
end

it 'does not register an offense when enumerator is not created for `__method__` and `__callee__` methods ' \
'and using safe navigation operator' do
expect_no_offenses(<<~RUBY)
def m(x)
return to_enum(obj&.never_nullable(value), x)
end
RUBY
end

%w[:m __callee__ __method__].each do |code|
it "does not register an offense when enumerator is created with `#{code}` and the correct arguments" do
expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit 8912abc

Please sign in to comment.