Skip to content

Commit

Permalink
[Fix rubocop#12284] Fix false positives for Style/SingleArgumentDig
Browse files Browse the repository at this point in the history
Fixes rubocop#12284.

This PR fixes false positives for `Style/SingleArgumentDig`
when using some anonymous argument syntax.
  • Loading branch information
koic committed Oct 16, 2023
1 parent c3ac85d commit e5866b6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12284](https://github.com/rubocop/rubocop/issues/12284): Fix false positives for `Style/SingleArgumentDig` when using some anonymous argument syntax. ([@koic][])
3 changes: 2 additions & 1 deletion lib/rubocop/cop/style/single_argument_dig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class SingleArgumentDig < Base

MSG = 'Use `%<receiver>s[%<argument>s]` instead of `%<original>s`.'
RESTRICT_ON_SEND = %i[dig].freeze
IGNORED_ARGUMENT_TYPES = %i[block_pass forwarded_restarg forwarded_args hash].freeze

# @!method single_argument_dig?(node)
def_node_matcher :single_argument_dig?, <<~PATTERN
Expand All @@ -44,7 +45,7 @@ def on_send(node)

expression = single_argument_dig?(node)
return unless expression
return if expression.forwarded_args_type?
return if IGNORED_ARGUMENT_TYPES.include?(expression.type)

receiver = node.receiver.source
argument = expression.source
Expand Down
34 changes: 34 additions & 0 deletions spec/rubocop/cop/style/single_argument_dig_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,40 @@ def foo(...)
end
end

context '>= Ruby 3.1', :ruby31 do
context 'when using dig with anonymous block argument forwarding' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
def foo(&)
{ key: 'value' }.dig(&)
end
RUBY
end
end
end

context '>= Ruby 3.2', :ruby32 do
context 'when using dig with anonymous rest argument forwarding' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
def foo(*)
{ key: 'value' }.dig(*)
end
RUBY
end
end

context 'when using dig with anonymous keyword argument forwarding' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
def foo(**)
{ key: 'value' }.dig(**)
end
RUBY
end
end
end

describe 'dig over a variable as caller' do
context 'with single argument' do
it 'registers an offense and corrects unsuitable use of dig' do
Expand Down

0 comments on commit e5866b6

Please sign in to comment.