Skip to content

Extend Performance/Sum to support method callsΒ #455

Open
@toncid

Description

@toncid

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

Right now, Performance/Sum works well for numeric values, but it does not recognize method calls:

[1, 2, 3].inject(0) { |sum, e| sum + e }  # before
[1, 2, 3].sum  # after

[1, 2, 3].inject(0) { |sum, e| sum + e.to_i }  # no suggestion

No-op calls of to_i are here just to paint the picture. πŸ™‚

Describe the solution you'd like

It would be nice if RuboCop could recognize and simplify expressions as follows:

[1, 2, 3].inject(0) { |sum, e| sum + e.to_i }  # before
[1, 2, 3].sum(&:to_i)  # after

Describe alternatives you've considered

I have checked RuboCop Rails and enabled ActiveSupportExtensionsEnabled, but this case does not seem to be supported by them.

Additional context

It might even be a safe operation. I am not aware of any downsides to it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions