Skip to content

Commit

Permalink
[Fix #12746] Fix a false positive for Lint/ToEnumArguments
Browse files Browse the repository at this point in the history
Fixes #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 authored and bbatsov committed Mar 6, 2024
1 parent dea3b76 commit c0582a4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
@@ -0,0 +1 @@
* [#12746](https://github.com/rubocop/rubocop/pull/12746): Fix a false positive for `Lint/ToEnumArguments` when enumerator is created for another method in no arguments method definition. ([@koic][])
9 changes: 7 additions & 2 deletions lib/rubocop/cop/lint/to_enum_arguments.rb
Expand Up @@ -51,8 +51,13 @@ def on_send(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)

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 c0582a4

Please sign in to comment.