Skip to content

Commit

Permalink
Merge pull request #12288 from koic/fix_false_positive_for_style_redu…
Browse files Browse the repository at this point in the history
…ndant_double_splat_hash_braces

[Fix #12286] Fix false positives for `Style/RedundantDoubleSplatHashBraces`
  • Loading branch information
koic committed Oct 17, 2023
2 parents affbe3b + 2a1687a commit 6713311
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12286](https://github.com/rubocop/rubocop/issues/12286): Fix false positives for `Style/RedundantDoubleSplatHashBraces` when using double splat with a hash literal enclosed in parenthesized ternary operator. ([@koic][])
5 changes: 3 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 @@ -25,10 +25,11 @@ class RedundantDoubleSplatHashBraces < Base
MSG = 'Remove the redundant double splat and braces, use keyword arguments directly.'
MERGE_METHODS = %i[merge merge!].freeze

# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def on_hash(node)
return if node.pairs.empty? || node.pairs.any?(&:hash_rocket?)
return unless (parent = node.parent)
return unless parent.call_type? || parent.kwsplat_type?
return if parent.call_type? && !merge_method?(parent)
return unless (kwsplat = node.each_ancestor(:kwsplat).first)
return if allowed_double_splat_receiver?(kwsplat)
Expand All @@ -37,7 +38,7 @@ def on_hash(node)
autocorrect(corrector, node, kwsplat)
end
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

private

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,10 @@
do_something(**x.do_something { {foo: {bar: _1}} })
RUBY
end

it 'does not register an offense when using double splat with a hash literal enclosed in parenthesized ternary operator' do
expect_no_offenses(<<~RUBY)
do_something(**(foo ? {bar: bar} : baz))
RUBY
end
end

0 comments on commit 6713311

Please sign in to comment.