A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/rubocop/rubocop/issues/14306 below:

`Style/HashConversion` Autocorrect Error · Issue #14306 · rubocop/rubocop · GitHub

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[Hash[]]

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.76.2/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/2862d55ea752d35d5743a6544d9357b2dfab4c61/6d7a3b621ca1730e04accd938619e4bdab66cfb1/aeb741d3bee863cb8de15507f35ff7b0e001bccd
An error occurred while Style/HashConversion cop was inspecting /Users/aidanlavis/test-rubocop/foo.rb:3:5.
/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/lib/rubocop/cop/style/hash_conversion.rb:121:in 'RuboCop::Cop::Style::HashConversion#multi_argument'
        from /Users/aidanlavis/.rbenv/versions/3.4.4/lib/ruby/gems/3.4.0/gems/rubocop-1.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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.76.2/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>'
W

Offenses:

foo.rb:3:1: C: [Corrected] Style/HashConversion: Prefer ary.to_h to Hash[ary].
Hash[Hash[]]
^^^^^^^^^^^^
foo.rb:3:1: C: [Corrected] Style/RedundantParentheses: Don't use parentheses around a literal.
({}).to_h
^^^^
foo.rb:3:6: W: [Corrected] Lint/RedundantTypeConversion: Redundant to_h detected.
({}).to_h
     ^^^^
foo.rb:3:6: C: [Corrected] Style/EmptyLiteral: Use hash literal {} instead of Hash[].
Hash[Hash[]]
     ^^^^^^

1 file inspected, 4 offenses detected, 4 offenses corrected

1 error occurred:
An error occurred while Style/HashConversion cop was inspecting /Users/aidanlavis/test-rubocop/foo.rb:3:5.
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.76.2 (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.08435299999837298 seconds
Steps to reproduce the problem

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

RuboCop version
> rubocop -V
1.76.2 (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]

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4