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

Extend Performance/Sum to support method calls #455

Open
toncid opened this issue Jul 4, 2024 · 0 comments
Open

Extend Performance/Sum to support method calls #455

toncid opened this issue Jul 4, 2024 · 0 comments

Comments

@toncid
Copy link

toncid commented Jul 4, 2024

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.

@koic koic transferred this issue from rubocop/rubocop Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants