Skip to content

Commit

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

This PR fixes a false positive for `Lint/ToEnumArguments`
when using anonymous keyword arguments forwarding.
  • Loading branch information
koic committed Aug 22, 2023
1 parent 51ed9ab commit f1b2832
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12143](https://github.com/rubocop/rubocop/issues/12143): Fix a false positive for `Lint/ToEnumArguments` when using anonymous keyword arguments forwarding. ([@koic][])
8 changes: 5 additions & 3 deletions lib/rubocop/cop/lint/to_enum_arguments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def arguments_match?(arguments, def_node)
end
end

# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
def argument_match?(send_arg, def_arg)
def_arg_name = def_arg.children[0]

Expand All @@ -87,12 +87,14 @@ def argument_match?(send_arg, def_arg)
send_arg.hash_type? &&
send_arg.pairs.any? { |pair| passing_keyword_arg?(pair, def_arg_name) }
when :kwrestarg
send_arg.each_child_node(:kwsplat).any? { |child| child.source == def_arg.source }
send_arg.each_child_node(:kwsplat, :forwarded_kwrestarg).any? do |child|
child.source == def_arg.source
end
when :forward_arg
send_arg.forwarded_args_type?
end
end
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
end
end
end
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cop/lint/to_enum_arguments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,28 @@ def m(...)
RUBY
end
end

context 'anonymous positional arguments forwarding', :ruby32 do
it 'does not register an offense when enumerator is created with the correct arguments' do
expect_no_offenses(<<~RUBY)
def do_something(*)
return to_enum(:do_something, *) unless block_given?
do_something_else
end
RUBY
end
end

context 'anonymous keyword arguments forwarding', :ruby32 do
it 'does not register an offense when enumerator is created with the correct arguments' do
expect_no_offenses(<<~RUBY)
def do_something(**)
return to_enum(:do_something, **) unless block_given?
do_something_else
end
RUBY
end
end
end

0 comments on commit f1b2832

Please sign in to comment.