Skip to content

Commit

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

This PR fixes a false positive for `Lint/ToEnumArguments`
when enumerator is created for another method in no arguments method definition.
  • Loading branch information
koic committed Mar 6, 2024
1 parent 859f6fd commit b6ea7a0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/rubocop/cop/lint/to_enum_arguments.rb
Expand Up @@ -49,10 +49,16 @@ def on_send(node)
return unless def_node

enum_conversion_call?(node) do |method_node, arguments|
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)
if method_node.call_type? && !method_node.method?(:__method__) && !method_node.method?(:__callee__)
next
end

valid = if method_name?(method_node, def_node.method_name)
arguments_match?(arguments, def_node)
else
def_node.arguments.empty?
end
return if valid

add_offense(node)
end
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/lint/to_enum_arguments_spec.rb
Expand Up @@ -113,6 +113,14 @@ def m(x)
RUBY
end

it 'does not register an offense when enumerator is created for another method in no arguments method definition' do
expect_no_offenses(<<~RUBY)
def m
return to_enum(:not_m) unless block_given?
end
RUBY
end

it 'registers an offense when enumerator is created for `__method__` with missing arguments' do
expect_offense(<<~RUBY)
def m(x)
Expand Down

0 comments on commit b6ea7a0

Please sign in to comment.