Skip to content

Commit 128618c

Browse files
koicbbatsov
authored andcommitted
Fix false positives for Style/RedundantDoubleSplatHashBraces
Fixes #12263. This PR fixes false positives for `Style/RedundantDoubleSplatHashBraces` when method call for no hash braced double splat receiver. The code shown in #12263 is incompatible as shown below and is a false positive: ```ruby def x(opts) = puts(opts) h = {foo: :bar} x(**h.merge(k: :h)) #=> {:foo=>:bar, :k=>:h} x(k: :v, **h) #=> {:k=>:v, :foo=>:bar} ```
1 parent 03cdf04 commit 128618c

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#12263](https://github.com/rubocop/rubocop/issues/12263): Fix false positives for `Style/RedundantDoubleSplatHashBraces` when method call for no hash braced double splat receiver. ([@koic][])

lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ class RedundantDoubleSplatHashBraces < Base
2727

2828
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
2929
def on_hash(node)
30-
return if !node.braces? || node.pairs.empty? || node.pairs.any?(&:hash_rocket?)
30+
return if node.pairs.empty? || node.pairs.any?(&:hash_rocket?)
3131
return unless (parent = node.parent)
32-
return unless (kwsplat = node.each_ancestor(:kwsplat).first)
3332
return if parent.call_type? && !merge_method?(parent)
33+
return unless (kwsplat = node.each_ancestor(:kwsplat).first)
34+
return if allowed_double_splat_receiver?(kwsplat)
3435

3536
add_offense(kwsplat) do |corrector|
3637
autocorrect(corrector, node, kwsplat)
@@ -40,6 +41,14 @@ def on_hash(node)
4041

4142
private
4243

44+
def allowed_double_splat_receiver?(kwsplat)
45+
return false unless kwsplat.children.first.call_type?
46+
47+
root_receiver = root_receiver(kwsplat.children.first)
48+
49+
!root_receiver&.hash_type?
50+
end
51+
4352
def autocorrect(corrector, node, kwsplat)
4453
corrector.remove(kwsplat.loc.operator)
4554
corrector.remove(opening_brace(node))
@@ -51,6 +60,15 @@ def autocorrect(corrector, node, kwsplat)
5160
autocorrect_merge_methods(corrector, merge_methods, kwsplat)
5261
end
5362

63+
def root_receiver(node)
64+
receiver = node.receiver
65+
if receiver&.receiver
66+
root_receiver(receiver)
67+
else
68+
receiver
69+
end
70+
end
71+
5472
def select_merge_method_nodes(kwsplat)
5573
extract_send_methods(kwsplat).select do |node|
5674
merge_method?(node)

spec/rubocop/cop/style/redundant_double_splat_hash_braces_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@
138138
RUBY
139139
end
140140

141+
it 'does not register an offense when method call for no hash braced double splat receiver' do
142+
expect_no_offenses(<<~RUBY)
143+
do_something(**options.merge({foo: bar}))
144+
RUBY
145+
end
146+
147+
it 'does not register an offense when safe navigation method call for no hash braced double splat receiver' do
148+
expect_no_offenses(<<~RUBY)
149+
do_something(**options&.merge({foo: bar}))
150+
RUBY
151+
end
152+
141153
it 'does not register an offense when using empty double splat hash braces arguments' do
142154
expect_no_offenses(<<~RUBY)
143155
do_something(**{})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy