Skip to content

Add `Cop` that detects `map` followed by `to_h`

The following discussion from !32315 (merged) should be addressed:

  • @splattael started a discussion: (+4 comments)

    Usually, we can pass the block to Array#to_h directly and drop the .map which requires less object allocations. Not a huge thing here but still worth mentioning.

          response.to_h do |result|
            group_name = result.dig('metric', by)
            _timestamp, value = result['value']
            [group_name, value.to_i]
          end

    WDYT?

I did a quick search and it appears that there are 28 matches for \.map.+(end|\})\.to_h that would benefit from this.

Moreover, it appears that there is a fork of rubocop-performance which has this rule defined as Performance/HashTransformation, but it has never been submitted back upstream: https://github.com/eugeneius/rubocop-performance/blob/hash_transformation/config/default.yml#L113-L116