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

Performance/Sum is not type-safe and may introduce a bug #177

Closed
drbrain opened this issue Oct 5, 2020 · 0 comments · Fixed by #179
Closed

Performance/Sum is not type-safe and may introduce a bug #177

drbrain opened this issue Oct 5, 2020 · 0 comments · Fixed by #179

Comments

@drbrain
Copy link

drbrain commented Oct 5, 2020

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.

Expected behavior

Rubocop only suggests type-safe changes or appropriately warns about type-safety risks in its suggestions.

Actual behavior

Rubocop equates reduce(&:+) and sum as if they are completely equivalent.

Steps to reproduce the problem

See irb output above

You can also run rubocop -r rubocop-performance --only Performance/Sum on this file:

def m
  [].reduce(&:+)
end

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 or bundle exec rubocop -V if using Bundler. Here's an example:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant