-
-
Notifications
You must be signed in to change notification settings - Fork 81
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
[Fix #437] Fix a false positive for Performance/ChainArrayAllocation
#439
[Fix #437] Fix a false positive for Performance/ChainArrayAllocation
#439
Conversation
describe 'when using `select` with block argument after `select`' do | ||
it 'registers an offense' do | ||
expect_offense(<<~RUBY) | ||
model.select(:foo, :bar).select { |item| item.do_something } | ||
^^^^^^^ Use unchained `select` and `select!` (followed by `return array` if required) instead of chaining `select...select`. | ||
RUBY | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgive my possible stupidity (I'm not terribly familiar with how the tests in rubocop work)
But wouldn't this still be wrong?
Wouldn't it flag select
(QueryMethods#select) followed by a select
(enumerable).
Which isn't a double select that requires optimization?
You would however want to flag the 3rd select here right?
model.select(:foo, :bar).select { |item| item.do_something }.select { |item| item.do_something_else }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not entirely sure, this patch works as follows:
it 'registers an offense' do
expect_offense(<<~RUBY)
model.select(:foo, :bar).select { |item| item.do_something }.select { |item| item.do_something_else }
^^^^^^^ Use unchained `select` and `select!` (followed by `return array` if required) instead of chaining `select...select`.
^^^^^^^ Use unchained `select` and `select!` (followed by `return array` if required) instead of chaining `select...select`.
RUBY
end
So, this is a patch that distinguishes between select(:arg)
and select { |x| x }
. What potential issues might arise from this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, let me rephrase
with this code
expect_offense(<<~RUBY)
model.select(:foo, :bar).select { |item| item.do_something }
^^^^^^^ Use unchained `select` and `select!` (followed by `return array` if required) instead of chaining `select...select`.
RUBY
What should the suggested corrected code look like?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I understand. This should definitely be recognized as a false positive and I've fixed it. Thank you!
…ocation` Fixes rubocop#437. This PR fixes a false positive for `Performance/ChainArrayAllocation` when using `select` with block argument after `select`.
7bd4307
to
2103446
Compare
Fixes #437.
This PR fixes a false positive for
Performance/ChainArrayAllocation
when usingselect
with block argument afterselect
.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.