We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Enumerable#reduce and Array#sum have different behavior on an empty list:
$ irb >> [].reduce(&:+) => nil >> [].sum => 0
If code is dependent upon this behavior through using nil to indicate that no values were in the list, applying this change may introduce bugs.
nil
Rubocop only suggests type-safe changes or appropriately warns about type-safety risks in its suggestions.
Rubocop equates reduce(&:+) and sum as if they are completely equivalent.
reduce(&:+)
sum
See irb output above
irb
You can also run rubocop -r rubocop-performance --only Performance/Sum on this file:
rubocop -r rubocop-performance --only Performance/Sum
def m [].reduce(&:+) end
It will say:
t.rb:2:6: C: Performance/Sum: Use sum instead of reduce(&:+).
But nothing about type safety.
Include the output of rubocop -V or bundle exec rubocop -V if using Bundler. Here's an example:
rubocop -V
bundle exec rubocop -V
0.92.0 (using Parser 2.7.1.5, rubocop-ast 0.7.1, running on ruby 2.6.6 x86_64-darwin19)
Also, rubocop-performance 1.8.1
The text was updated successfully, but these errors were encountered:
Performance/Sum
[Fix rubocop#177] Performance/Sum should avoid empty arrays
5491b07
Successfully merging a pull request may close this issue.
Enumerable#reduce and Array#sum have different behavior on an empty list:
If code is dependent upon this behavior through using
nil
to indicate that no values were in the list, applying this change may introduce bugs.Expected behavior
Rubocop only suggests type-safe changes or appropriately warns about type-safety risks in its suggestions.
Actual behavior
Rubocop equates
reduce(&:+)
andsum
as if they are completely equivalent.Steps to reproduce the problem
See
irb
output aboveYou can also run
rubocop -r rubocop-performance --only Performance/Sum
on this file:It will say:
t.rb:2:6: C: Performance/Sum: Use sum instead of reduce(&:+).
But nothing about type safety.
RuboCop version
Include the output of
rubocop -V
orbundle exec rubocop -V
if using Bundler. Here's an example:Also, rubocop-performance 1.8.1
The text was updated successfully, but these errors were encountered: