Fix autocorrect for Style/HashConversion to avoid syntax error#14343
Merged
koic merged 1 commit intorubocop:masterfrom Jul 6, 2025
Merged
Fix autocorrect for Style/HashConversion to avoid syntax error#14343koic merged 1 commit intorubocop:masterfrom
Style/HashConversion to avoid syntax error#14343koic merged 1 commit intorubocop:masterfrom
Conversation
db3bd54 to
1d474ce
Compare
dvandersluis
reviewed
Jul 6, 2025
| @@ -0,0 +1 @@ | |||
| * [#14343](https://github.com/rubocop/rubocop/pull/14343): Fix autocorrect code for `Style/HashConversion` to avoi syntax error. ([@koic][]) | |||
Member
There was a problem hiding this comment.
Suggested change
| * [#14343](https://github.com/rubocop/rubocop/pull/14343): Fix autocorrect code for `Style/HashConversion` to avoi syntax error. ([@koic][]) | |
| * [#14343](https://github.com/rubocop/rubocop/pull/14343): Fix autocorrect code for `Style/HashConversion` to avoid syntax error. ([@koic][]) |
This PR fixes autocorrect code for `Style/HashConversion`
to avoid syntax error.
## Before
Autocorrection causes a syntax error.
```console
echo "Hash[].to_h" | bundle exec rubocop --stdin example.rb -A
Inspecting 1 file
F
Offenses:
example.rb:1:1: C: [Corrected] Style/EmptyLiteral: Use hash literal {} instead of Hash[].
Hash[].to_h
^^^^^^
example.rb:1:1: C: [Corrected] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
Hash[].to_h
^
example.rb:1:1: C: [Corrected] Style/HashConversion: Prefer literal hash to Hash[arg1, arg2, ...].
Hash[].to_h
^^^^^^
example.rb:1:8: W: [Corrected] Lint/RedundantTypeConversion: Redundant to_h detected.
Hash[].to_h
^^^^
example.rb:2:3: F: Lint/Syntax: unexpected token tLPAREN2
(Using Ruby 2.7 parser; configure using TargetRubyVersion parameter, under AllCops)
{}()
^
1 file inspected, 5 offenses detected, 4 offenses corrected
====================
# frozen_string_literal: true
{}()
```
## After
No syntax errors.
```console
$ echo "Hash[].to_h" | bundle exec rubocop --stdin example.rb -A
Inspecting 1 file
W
Offenses:
example.rb:1:1: C: [Corrected] Style/EmptyLiteral: Use hash literal {} instead of Hash[].
Hash[].to_h
^^^^^^
example.rb:1:1: C: [Corrected] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
Hash[].to_h
^
example.rb:1:1: C: [Corrected] Style/HashConversion: Prefer literal hash to Hash[arg1, arg2, ...].
Hash[].to_h
^^^^^^
example.rb:1:8: W: [Corrected] Lint/RedundantTypeConversion: Redundant to_h detected.
Hash[].to_h
^^^^
example.rb:2:1: C: [Corrected] Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
{}
^
1 file inspected, 5 offenses detected, 5 offenses corrected
====================
# frozen_string_literal: true
{}
```
1d474ce to
0ef54b5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes autocorrect code for
Style/HashConversionto avoid syntax error.Before
Autocorrection causes a syntax error.
After
No syntax errors.
Before submitting the PR make sure the following are checked:
[Fix #issue-number](if the related issue exists).master(if not - rebase it).bundle exec rake default. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.mdif the new code introduces user-observable changes. See changelog entry format for details.