Skip to content

Commit 80aff64

Browse files
koicbbatsov
authored andcommitted
[Fix #14306] Fix an error for Style/HashConversion
This PR fixes an error for `Style/HashConversion` when using nested `Hash[]`. Fixes #14306.
1 parent 600d0a2 commit 80aff64

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#14306](https://github.com/rubocop/rubocop/issues/14306): Fix an error for `Style/HashConversion` when using nested `Hash[]`. ([@koic][])

lib/rubocop/cop/style/hash_conversion.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class HashConversion < Base
5454
def_node_matcher :hash_from_array?, '(send (const {nil? cbase} :Hash) :[] ...)'
5555

5656
def on_send(node)
57-
return unless hash_from_array?(node)
57+
return if part_of_ignored_node?(node) || !hash_from_array?(node)
5858

5959
# There are several cases:
6060
# If there is one argument:
@@ -68,6 +68,7 @@ def on_send(node)
6868

6969
private
7070

71+
# rubocop:disable Metrics/MethodLength
7172
def single_argument(node)
7273
first_argument = node.first_argument
7374
if first_argument.hash_type?
@@ -82,8 +83,11 @@ def single_argument(node)
8283
replacement = "(#{replacement})" if requires_parens?(first_argument)
8384
corrector.replace(node, "#{replacement}.to_h")
8485
end
86+
87+
ignore_node(node)
8588
end
8689
end
90+
# rubocop:enable Metrics/MethodLength
8791

8892
def use_zip_method_without_argument?(first_argument)
8993
return false unless first_argument&.send_type?
@@ -111,7 +115,12 @@ def register_offense_for_zip_method(node, zip_method)
111115
end
112116

113117
def requires_parens?(node)
114-
(node.call_type? && node.arguments.any? && !node.parenthesized?) || node.operator_keyword?
118+
if node.call_type?
119+
return false if node.method?(:[])
120+
return true if node.arguments.any? && !node.parenthesized?
121+
end
122+
123+
node.operator_keyword?
115124
end
116125

117126
def multi_argument(node)

spec/rubocop/cop/style/hash_conversion_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,28 @@
175175
RUBY
176176
end
177177

178+
it 'reports an offense when using nested `Hash[]` without arguments' do
179+
expect_offense(<<~RUBY)
180+
Hash[Hash[]]
181+
^^^^^^^^^^^^ Prefer ary.to_h to Hash[ary].
182+
RUBY
183+
184+
expect_correction(<<~RUBY)
185+
Hash[].to_h
186+
RUBY
187+
end
188+
189+
it 'reports an offense when using nested `Hash[]` with arguments' do
190+
expect_offense(<<~RUBY)
191+
Hash[Hash[k, v]]
192+
^^^^^^^^^^^^^^^^ Prefer ary.to_h to Hash[ary].
193+
RUBY
194+
195+
expect_correction(<<~RUBY)
196+
Hash[k, v].to_h
197+
RUBY
198+
end
199+
178200
context 'AllowSplatArgument: true' do
179201
let(:cop_config) { { 'AllowSplatArgument' => true } }
180202

0 commit comments

Comments
 (0)