Skip to content

Fix autocorrect for Style/HashConversion to avoid syntax error #14343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

koic
Copy link
Member

@koic koic commented Jul 6, 2025

This PR fixes autocorrect code for Style/HashConversion to avoid syntax error.

Before

Autocorrection causes a syntax error.

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.

$ 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

{}

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@koic koic force-pushed the fix_autocorrect_for_style_hash_conversion branch from db3bd54 to 1d474ce Compare July 6, 2025 08:53
@@ -0,0 +1 @@
* [#14343](https://github.com/rubocop/rubocop/pull/14343): Fix autocorrect code for `Style/HashConversion` to avoi syntax error. ([@koic][])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

{}
```
@koic koic force-pushed the fix_autocorrect_for_style_hash_conversion branch from 1d474ce to 0ef54b5 Compare July 6, 2025 14:15
@koic koic merged commit 0b08bb8 into rubocop:master Jul 6, 2025
24 checks passed
@koic koic deleted the fix_autocorrect_for_style_hash_conversion branch July 6, 2025 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
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