Skip to content

Style/HashConversion Autocorrect Error #14326

@a-lavis

Description

@a-lavis

Expected behavior

When running rubocop foo.rb with the following .rubocop.yml:

AllCops:
  NewCops: enable
  TargetRubyVersion: 3.4

and foo.rb is the following file:

# frozen_string_literal: true

Hash[1, Hash[:a, 'foo']]

I expect RuboCop to not encounter an error when autocorrecting.

Actual behavior

When running rubocop -A foo.rb --debug, I see the following output:

For /Users/aidanlavis/test-rubocop: configuration from /Users/aidanlavis/test-rubocop/.rubocop.yml
Default configuration from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /Users/aidanlavis/test-rubocop/foo.rb
Loading cache from /Users/aidanlavis/.cache/rubocop_cache/672bc1035bb67c84641889aa9000cb8d7fb7e66b/6d7a3b621ca1730e04accd938619e4bdab66cfb1/e1d72e8b7dd0368270c5e67cf10843f22c7a60cc
An error occurred while Style/HashConversion cop was inspecting /Users/aidanlavis/test-rubocop/foo.rb:3:8.
/Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter.rb:427:in 'Parser::Source::TreeRewriter#trigger_policy': Parser::Source::TreeRewriter detected clobbering (Parser::ClobberingError)
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter.rb:414:in 'Parser::Source::TreeRewriter#enforce_policy'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter/action.rb:234:in 'Method#call'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter/action.rb:234:in 'Parser::Source::TreeRewriter::Action#swallow'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter/action.rb:98:in 'Parser::Source::TreeRewriter::Action#with'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter/action.rb:125:in 'Parser::Source::TreeRewriter::Action#place_in_hierarchy'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter/action.rb:107:in 'Parser::Source::TreeRewriter::Action#do_combine'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter/action.rb:118:in 'Parser::Source::TreeRewriter::Action#place_in_hierarchy'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter/action.rb:132:in 'block in Parser::Source::TreeRewriter::Action#combine_children'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter/action.rb:131:in 'Array#each'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter/action.rb:131:in 'Enumerable#inject'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter/action.rb:131:in 'Parser::Source::TreeRewriter::Action#combine_children'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter/action.rb:222:in 'Parser::Source::TreeRewriter::Action#merge'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter/action.rb:105:in 'Parser::Source::TreeRewriter::Action#do_combine'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter/action.rb:31:in 'Parser::Source::TreeRewriter::Action#combine'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/parser-3.3.8.0/lib/parser/source/tree_rewriter.rb:143:in 'Parser::Source::TreeRewriter#merge!'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/base.rb:380:in 'RuboCop::Cop::Base#apply_correction'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/base.rb:459:in 'RuboCop::Cop::Base#attempt_correction'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/base.rb:440:in 'RuboCop::Cop::Base#use_corrector'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/base.rb:434:in 'RuboCop::Cop::Base#correct'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/base.rb:210:in 'RuboCop::Cop::Base#add_offense'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/style/hash_conversion.rb:130:in 'RuboCop::Cop::Style::HashConversion#multi_argument'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/style/hash_conversion.rb:66:in 'RuboCop::Cop::Style::HashConversion#on_send'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/commissioner.rb:143:in 'Kernel#public_send'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/commissioner.rb:143:in 'block (2 levels) in RuboCop::Cop::Commissioner#trigger_restricted_cops'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/commissioner.rb:171:in 'RuboCop::Cop::Commissioner#with_cop_error_handling'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/commissioner.rb:142:in 'block in RuboCop::Cop::Commissioner#trigger_restricted_cops'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/commissioner.rb:141:in 'Array#each'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/commissioner.rb:141:in 'RuboCop::Cop::Commissioner#trigger_restricted_cops'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/commissioner.rb:70:in 'RuboCop::Cop::Commissioner#on_send'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.45.1/lib/rubocop/ast/traversal.rb:169:in 'block in RuboCop::AST::Traversal#on_send'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.45.1/lib/rubocop/ast/traversal.rb:166:in 'Array#each'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.45.1/lib/rubocop/ast/traversal.rb:166:in 'Enumerable#each_with_index'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.45.1/lib/rubocop/ast/traversal.rb:166:in 'RuboCop::AST::Traversal#on_send'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/commissioner.rb:71:in 'RuboCop::Cop::Commissioner#on_send'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-ast-1.45.1/lib/rubocop/ast/traversal.rb:20:in 'RuboCop::AST::Traversal#walk'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/commissioner.rb:87:in 'RuboCop::Cop::Commissioner#investigate'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/team.rb:174:in 'RuboCop::Cop::Team#investigate_partial'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cop/team.rb:101:in 'RuboCop::Cop::Team#investigate'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:348:in 'block in RuboCop::Runner#inspect_file'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:347:in 'Array#each'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:347:in 'Enumerable#flat_map'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:347:in 'RuboCop::Runner#inspect_file'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:290:in 'block in RuboCop::Runner#do_inspection_loop'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:324:in 'block in RuboCop::Runner#iterate_until_no_changes'
        from <internal:kernel>:168:in 'Kernel#loop'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:317:in 'RuboCop::Runner#iterate_until_no_changes'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:286:in 'RuboCop::Runner#do_inspection_loop'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:167:in 'block in RuboCop::Runner#file_offenses'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:192:in 'RuboCop::Runner#file_offense_cache'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:166:in 'RuboCop::Runner#file_offenses'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:154:in 'RuboCop::Runner#process_file'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:135:in 'block in RuboCop::Runner#each_inspected_file'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:134:in 'Array#each'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:134:in 'Enumerable#reduce'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:134:in 'RuboCop::Runner#each_inspected_file'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:120:in 'RuboCop::Runner#inspect_files'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/runner.rb:73:in 'RuboCop::Runner#run'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cli/command/execute_runner.rb:26:in 'block in RuboCop::CLI::Command::ExecuteRunner#execute_runner'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cli/command/execute_runner.rb:52:in 'RuboCop::CLI::Command::ExecuteRunner#with_redirect'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cli/command/execute_runner.rb:25:in 'RuboCop::CLI::Command::ExecuteRunner#execute_runner'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cli/command/execute_runner.rb:17:in 'RuboCop::CLI::Command::ExecuteRunner#run'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cli/command.rb:11:in 'RuboCop::CLI::Command.run'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cli/environment.rb:18:in 'RuboCop::CLI::Environment#run'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cli.rb:122:in 'RuboCop::CLI#run_command'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cli.rb:129:in 'RuboCop::CLI#execute_runners'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cli.rb:51:in 'block in RuboCop::CLI#run'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cli.rb:81:in 'RuboCop::CLI#profile_if_needed'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/lib/rubocop/cli.rb:43:in 'RuboCop::CLI#run'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.77.0/exe/rubocop:19:in '<top (required)>'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/bin/rubocop:25:in 'Kernel#load'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/bin/rubocop:25:in '<main>'
C

Offenses:

foo.rb:3:1: C: [Corrected] Layout/SpaceInsideHashLiteralBraces: Space inside { missing.
{1 => Hash[:a, 'foo']}
^
foo.rb:3:1: C: [Corrected] Style/HashConversion: Prefer literal hash to Hash[arg1, arg2, ...].
Hash[1, Hash[:a, 'foo']]
^^^^^^^^^^^^^^^^^^^^^^^^
foo.rb:3:7: C: [Corrected] Style/HashConversion: Prefer literal hash to Hash[arg1, arg2, ...].
{1 => Hash[:a, 'foo']}
      ^^^^^^^^^^^^^^^
foo.rb:3:8: C: [Corrected] Layout/SpaceInsideHashLiteralBraces: Space inside { missing.
{ 1 => {:a => 'foo'} }
       ^
foo.rb:3:9: C: [Corrected] Style/HashSyntax: Use the new Ruby 1.9 hash syntax.
{ 1 => {:a => 'foo'} }
        ^^^^^
foo.rb:3:20: C: [Corrected] Layout/SpaceInsideHashLiteralBraces: Space inside } missing.
{ 1 => {:a => 'foo'} }
                   ^
foo.rb:3:22: C: [Corrected] Layout/SpaceInsideHashLiteralBraces: Space inside } missing.
{1 => Hash[:a, 'foo']}
                     ^

1 file inspected, 7 offenses detected, 7 offenses corrected

1 error occurred:
An error occurred while Style/HashConversion cop was inspecting /Users/aidanlavis/test-rubocop/foo.rb:3:8.
configuration from /Users/aidanlavis/test-rubocop/.rubocop.yml
Errors are usually caused by RuboCop bugs.
Please, update to the latest RuboCop version if not already in use, and report a bug if the issue still occurs on this version.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.77.0 (using Parser 3.3.8.0, rubocop-ast 1.45.1, analyzing as Ruby 2.7, running on ruby 3.4.4) [arm64-darwin24]
Finished in 0.08734100000583567 seconds

Steps to reproduce the problem

Run rubocop -A foo.rb where foo.rb is the above file.

RuboCop version

> rubocop -V
1.77.0 (using Parser 3.3.8.0, Prism 1.4.0, rubocop-ast 1.45.1, analyzing as Ruby 3.4, running on ruby 3.4.4) [arm64-darwin24]

See also

Likely related to #14306, which was fixed in v1.77.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions