Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mvz opened this issue Sep 5, 2020 · 0 comments · Fixed by #166
Closed

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

mvz opened this issue Sep 5, 2020 · 0 comments · Fixed by #166
Labels
bug Something isn't working

Comments

@mvz
Copy link
Contributor

mvz commented Sep 5, 2020

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.

@koic koic added the bug Something isn't working label Sep 5, 2020
koic added a commit to koic/rubocop-performance that referenced this issue Sep 5, 2020
Fixes rubocop#165.

This PR fixes a false positive for `Performance/Sum`
when using initial value argument is a variable.
koic added a commit to koic/rubocop-performance that referenced this issue Sep 5, 2020
Fixes rubocop#165.

This PR fixes a false positive for `Performance/Sum`
when using initial value argument is a variable.
@koic koic closed this as completed in #166 Sep 5, 2020
koic added a commit that referenced this issue Sep 5, 2020
[Fix #165] Fix a false positive for `Performance/Sum`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants