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

[Fix #437] Fix a false positive for Performance/ChainArrayAllocation #439

Conversation

koic
Copy link
Member

@koic koic commented Jan 31, 2024

Fixes #437.

This PR fixes a false positive for Performance/ChainArrayAllocation when using select with block argument after select.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • 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.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

Comment on lines 57 to 81
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

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 }

Copy link
Member Author

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?

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?

Copy link
Member Author

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`.
@koic koic force-pushed the fix_a_false_positive_for_performance_chain_array_allocation branch from 7bd4307 to 2103446 Compare February 2, 2024 08:19
@koic koic merged commit c07dcd2 into rubocop:master Feb 3, 2024
13 checks passed
@koic koic deleted the fix_a_false_positive_for_performance_chain_array_allocation branch February 3, 2024 15:54
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.

Incorrect recommendation suggested by Performance/ChainArrayAllocation Cop
2 participants