rubocop / rubocop-performance

An extension of RuboCop focused on code performance checks.
https://docs.rubocop.org/rubocop-performance
MIT License
670 stars 79 forks source link

Autocorrection support on `Performance/MapMethodChain` #436

Closed r7kamura closed 7 months ago

r7kamura commented 7 months ago

Is your feature request related to a problem? Please describe.

While using rubocop-performance, I encountered the following offense:

example.rb:111:43: C: Performance/MapMethodChain: Use map { |x| x.value.to_s } instead of map method chain.
  items.map(&:value).map(&:to_s)
        ^^^^^^^^^^^^^^^^^^^^^^^^

I see. So you want me to use map { |x| x.value.to_s } instead. If you go so far as to say that, wouldn't it be nice if you supported autocorrect?

The comment states that autocorrection is not supported because appropriate block argument names cannot be determined.

https://github.com/rubocop/rubocop-performance/blob/3ba5d912e55e3b76672a6437511662fdadb64079/lib/rubocop/cop/performance/map_method_chain.rb#L8

Since the message says to use x, shouldn't it be x?

Or, you say that the good example code uses item, why not just use item?

https://github.com/rubocop/rubocop-performance/blob/3ba5d912e55e3b76672a6437511662fdadb64079/lib/rubocop/cop/performance/map_method_chain.rb#L37-L41

I believe that it is more valuable to provide autocorrection than to provide perfectly considered argument names.

Describe the solution you'd like

How about supporting the autocorrection feature with an argument name of item?

Describe alternatives you've considered

Alternatively, using numbered parameters _1 or, in future Ruby, it could be an option.

Additional context

Performance/MapMethodChain cop was added at the following pull request:

I searched past Issues, comments, etc., but could not find any indication that this point has already been discussed.

koic commented 7 months ago

Autocorrect is complementary and should not be named arbitrarily. Also, it can be assumed that people's tastes (and situations) will likely differ, so there will still be some skepticism as to whether it can be replaced.

r7kamura commented 7 months ago

I understand that autocorrection will not be supported in the future. Thank you for your quick response 👍