Skip to content

Autocorrect for Performance/Sum drops initial value if it is not a number literal #165

@mvz

Description

@mvz

When autocorrecting, the new Performance/Sum cop should take the initial value provided to inject or reduce and pass that as the first argument to sum, even when it is not a number literal.

This is corrected correctly:

foo.inject(23, :+)

But this is not:

foo.inject(bar, :+)

Expected behavior

The code

foo.inject(bar, :+)

should be corrected to

foo.sum(bar)

Also, more complex code like the following real-world example:

paid_bills.reduce(AmountWithVAT.new, :+)

should be corrected to

paid_bills.sum(AmountWithVAT.new)

Actual behavior

The two examples given above are corrected to

foo.sum

and

paid_bills.sum

Steps to reproduce the problem

Let rubocop-performance autocorrect the examples given above

RuboCop version

0.90.0 (using Parser 2.7.1.4, rubocop-ast 0.3.0, running on ruby 2.7.1 x86_64-linux)

rubocop-performance is at version 1.8.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions