Skip to content

Add option to only enforce map/block correction of Perfomance/Sum #204

@dgollahon

Description

@dgollahon

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

I have found the Performance/Sum cop useful in several cases but the false positive rate is too high for me because there are various cases in my codebase(s) that use reduce(:+) on objects where sum is not applicable. In cases where I'm already using #sum, however, there were several opportunities to use the block form which is simpler and faster. I would like to be able to have these detected without enabling the entire cop.

Describe the solution you'd like

An option that just enforces using the block form of #sum when it can be detected:

# bad
[1, 2, 3].map { |elem| elem ** 2 }.sum
[1, 2, 3].collect(&:count).sum(10)

# good
[1, 2, 3].sum { |elem| elem ** 2 }
[1, 2, 3].sum(10, &:count)

Describe alternatives you've considered

This may be too niche of an option but I think it would be helpful since I will likely disable this cop otherwise because the false positive rate for me is too high. There are also a couple of cases where I might be able to use the init parameter to #sum but there are cases I have where I do not prefer writing the code that way as it is not necessary or helpful AFAICT.

Additional context

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions