Skip to content

Commit 0b08bb8

Browse files
authored
Merge pull request #14343 from koic/fix_autocorrect_for_style_hash_conversion
Fix autocorrect for `Style/HashConversion` to avoid syntax error
2 parents 01fdcbb + 0ef54b5 commit 0b08bb8

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#14343](https://github.com/rubocop/rubocop/pull/14343): Fix autocorrect code for `Style/HashConversion` to avoid syntax error. ([@koic][])

lib/rubocop/cop/style/hash_conversion.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ def multi_argument(node)
128128
corrector.replace(node, args_to_hash(node.arguments))
129129

130130
parent = node.parent
131-
add_parentheses(parent, corrector) if parent&.send_type? && !parent.parenthesized?
131+
if parent&.send_type? && !parent.method?(:to_h) && !parent.parenthesized?
132+
add_parentheses(parent, corrector)
133+
end
132134
end
133135
end
134136
end

spec/rubocop/cop/style/hash_conversion_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,17 @@
208208
RUBY
209209
end
210210

211+
it 'reports an offense for `Hash[].to_h`' do
212+
expect_offense(<<~RUBY)
213+
Hash[].to_h
214+
^^^^^^ Prefer literal hash to `Hash[arg1, arg2, ...]`.
215+
RUBY
216+
217+
expect_correction(<<~RUBY)
218+
{}.to_h
219+
RUBY
220+
end
221+
211222
context 'AllowSplatArgument: true' do
212223
let(:cop_config) { { 'AllowSplatArgument' => true } }
213224

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