Skip to content
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

Bug in the Style/MapToHash cop #12429

Closed
ydakuka opened this issue Dec 1, 2023 · 5 comments · Fixed by #12431
Closed

Bug in the Style/MapToHash cop #12429

ydakuka opened this issue Dec 1, 2023 · 5 comments · Fixed by #12431
Labels

Comments

@ydakuka
Copy link

ydakuka commented Dec 1, 2023

Actual behavior

I have the following code:

def my_method
  hsh = { foo: 1, bar: 2 }
  # hsh = nil

  hsh&.map { |k, v| [k.to_s, v.next] }.to_h
end

I run rubocop with autocorrection and get:

def my_method
  hsh = { foo: 1, bar: 2 }
  # hsh = nil

  hsh&.to_h { |k, v| [k.to_s, v.next] }
end

Expected behavior

def my_method
  hsh = { foo: 1, bar: 2 }
  # hsh = nil

  hsh.to_h { |k, v| [k.to_s, v.next] }
end

RuboCop version

ydakuka@yauhenid:~/Work/project$ bin/rails_docker rubocop -V
1.58.0 (using Parser 3.2.2.4, rubocop-ast 1.30.0, running on ruby 2.7.8) [x86_64-linux]
  - rubocop-capybara 2.19.0
  - rubocop-factory_bot 2.24.0
  - rubocop-performance 1.19.1
  - rubocop-rails 2.22.2
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.25.0
  - rubocop-thread_safety 0.5.1
@koic
Copy link
Member

koic commented Dec 2, 2023

This change will break the compatibility of autocorrection, so I'll close it. Please refer to #12431 (comment) as well

@koic koic closed this as not planned Won't fix, can't repro, duplicate, stale Dec 2, 2023
@ydakuka
Copy link
Author

ydakuka commented Dec 2, 2023

I disagree. In this example, autocorrection results in different behavior.

@koic
Copy link
Member

koic commented Dec 2, 2023

In my tests, the autocorrection made to the presented code appear to be compatible. Removing the safe navigation operator from autocorrected code actually creates incompatibility.

@ydakuka
Copy link
Author

ydakuka commented Dec 2, 2023

Before autocorrection:

2.7.6 :002 > hsh = { foo: 1, bar: 2 }
 => {:foo=>1, :bar=>2} 
2.7.6 :003 > hsh&.map { |k, v| [k.to_s, v.next] }.to_h
 => {"foo"=>2, "bar"=>3} 
2.7.6 :004 > hsh = nil
 => nil 
2.7.6 :005 > hsh&.map { |k, v| [k.to_s, v.next] }.to_h
 => {}

After autocorrection:

2.7.6 :006 > hsh = { foo: 1, bar: 2 }
 => {:foo=>1, :bar=>2} 
2.7.6 :007 > hsh&.to_h { |k, v| [k.to_s, v.next] }
 => {"foo"=>2, "bar"=>3} 
2.7.6 :008 > hsh = nil
 => nil 
2.7.6 :009 > hsh&.to_h { |k, v| [k.to_s, v.next] }
 => nil

Expected behaviour:

2.7.6 :010 > hsh = { foo: 1, bar: 2 }
 => {:foo=>1, :bar=>2} 
2.7.6 :011 > hsh.to_h { |k, v| [k.to_s, v.next] }
 => {"foo"=>2, "bar"=>3} 
2.7.6 :012 > hsh = nil
 => nil 
2.7.6 :013 > hsh.to_h { |k, v| [k.to_s, v.next] }
 => {} 

@koic
Copy link
Member

koic commented Dec 2, 2023

Ah, understand. I will work on this again. Thank you!

@koic koic reopened this Dec 2, 2023
koic added a commit to koic/rubocop that referenced this issue Dec 2, 2023
Fixes rubocop#12429.

This PR fixes incorrect autocorrect for `Style/MapToHash`
when using dot method calls for `to_h`.
@koic koic added the bug label Dec 2, 2023
koic added a commit that referenced this issue Dec 2, 2023
…o_hash

[Fix #12429] Fix incorrect autocorrect for `Style/MapToHash`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants