Skip to content

Commit

Permalink
[Fix #12275] Fix a false positive for `Style/RedundantDoubleSplatHash…
Browse files Browse the repository at this point in the history
…Braces`

Fixes #12275.

This PR fixes a false positive for `Style/RedundantDoubleSplatHashBraces`
when using double splat within block argument containing a hash literal in an array literal.
  • Loading branch information
koic authored and bbatsov committed Oct 14, 2023
1 parent b7fb064 commit a8bea2b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12275](https://github.com/rubocop/rubocop/issues/12275): Fix a false positive for `Style/RedundantDoubleSplatHashBraces` when using double splat within block argument containing a hash literal in an array literal. ([@koic][])
6 changes: 4 additions & 2 deletions lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ def on_hash(node)
private

def allowed_double_splat_receiver?(kwsplat)
return false unless kwsplat.children.first.call_type?
first_child = kwsplat.children.first
return true if first_child.block_type? || first_child.numblock_type?
return false unless first_child.call_type?

root_receiver = root_receiver(kwsplat.children.first)
root_receiver = root_receiver(first_child)

!root_receiver&.hash_type?
end
Expand Down
33 changes: 33 additions & 0 deletions spec/rubocop/cop/style/redundant_double_splat_hash_braces_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,21 @@
RUBY
end

it 'registers an offense when using double splat hash braces inside block' do
expect_offense(<<~RUBY)
block do
do_something(**{foo: bar, baz: qux})
^^^^^^^^^^^^^^^^^^^^^^ Remove the redundant double splat and braces, use keyword arguments directly.
end
RUBY

expect_correction(<<~RUBY)
block do
do_something(foo: bar, baz: qux)
end
RUBY
end

it 'does not register an offense when using keyword arguments' do
expect_no_offenses(<<~RUBY)
do_something(foo: bar, baz: qux)
Expand Down Expand Up @@ -191,4 +206,22 @@
{ a: a }
RUBY
end

it 'does not register an offense when using double splat within block argument containing a hash literal in an array literal' do
expect_no_offenses(<<~RUBY)
do_something(**x.do_something { [foo: bar] })
RUBY
end

it 'does not register an offense when using double splat within block argument containing a nested hash literal' do
expect_no_offenses(<<~RUBY)
do_something(**x.do_something { {foo: {bar: baz}} })
RUBY
end

it 'does not register an offense when using double splat within numbered block argument containing a nested hash literal' do
expect_no_offenses(<<~RUBY)
do_something(**x.do_something { {foo: {bar: _1}} })
RUBY
end
end

0 comments on commit a8bea2b

Please sign in to comment.