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 should avoid, or warn about, empty arrays #179

Merged
merged 1 commit into from
Oct 12, 2020

Conversation

ghiculescu
Copy link
Contributor

@ghiculescu ghiculescu commented Oct 8, 2020

Fixes #177

Doesn't fully resolve the issue, since it only works on array literals. But this removes at least one false positive.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@ghiculescu ghiculescu force-pushed the perf-sum-empty-array branch 2 times, most recently from 532a523 to 2e91541 Compare October 8, 2020 15:55
@marcandre
Copy link
Contributor

I can't see anyone ever writing [].inject(...)

@ghiculescu
Copy link
Contributor Author

I can't see anyone ever writing [].inject(...)

No, it's more a starting point for the broader fix. I should have marked this PR as a draft.

@ghiculescu ghiculescu marked this pull request as draft October 8, 2020 15:57
@ghiculescu ghiculescu changed the title Performance/Sum should avoid empty arrays Performance/Sum should avoid, or warn about, empty arrays Oct 8, 2020
@ghiculescu
Copy link
Contributor Author

Okay, I don't think rubocop can know if an array is empty unless it's an array literal. So I added a warning to the error message for cases that look like #177. They already don't autocorrect.

@ghiculescu ghiculescu marked this pull request as ready for review October 8, 2020 16:14
@koic koic merged commit 3b413ef into rubocop:master Oct 12, 2020
@koic
Copy link
Member

koic commented Oct 12, 2020

Thanks!

@ghiculescu ghiculescu deleted the perf-sum-empty-array branch October 12, 2020 14:58
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 this pull request may close these issues.

Performance/Sum is not type-safe and may introduce a bug
3 participants