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

Incorrect recommendation suggested by Performance/ChainArrayAllocation Cop #437

Closed
matthewhively opened this issue Jan 30, 2024 · 0 comments · Fixed by #439
Closed

Incorrect recommendation suggested by Performance/ChainArrayAllocation Cop #437

matthewhively opened this issue Jan 30, 2024 · 0 comments · Fixed by #439
Labels
bug Something isn't working

Comments

@matthewhively
Copy link

Expected behavior

RuboCop suggests valid corrections on arrays.

Actual behavior

Identifies multiple uses of ActiveRecord query select method
While calling select multiple times on the same relation may technically be redundant, this is a completely distinct method from Array.select, and thus suggesting to use select! (which does not exist), should not happen.

Steps to reproduce the problem

Run rubocop on this file with Performance/ChainArrayAllocation enabled

models/test.rb

class Test < ActiveRecord::Base

  scope :test_scope, ->  {
    select(:field_one, :field_two).
    select("COUNT(DISTINCT(tests.field_three)) AS num").
    where("tests.field_one IS NOT NULL").
    group(:field_two)
  }

  def self.something
    Test.all
        .select(:field_one, :field_two)
        .select("COUNT(DISTINCT(tests.field_three)) AS num")
        .where("tests.field_one IS NOT NULL")
        .group(:field_two)
  end

end

RuboCop version

Found on version

1.31.0 (using Parser 3.3.0.5, rubocop-ast 1.30.0, running on ruby 2.7.8 x86_64-darwin21)
  - rubocop-performance 1.19.1
  - rubocop-rails 2.15.2

Still occurs on latest

1.60.2 (using Parser 3.3.0.5, rubocop-ast 1.30.0, running on ruby 2.7.8) [x86_64-darwin21]
  - rubocop-performance 1.20.2
  - rubocop-rails 2.23.1

Perhaps this is why this cop is disabled by default, because it has these are unsolvable false positives?

@koic koic added the bug Something isn't working label Jan 31, 2024
koic added a commit to koic/rubocop-performance that referenced this issue Jan 31, 2024
…ocation`

Fixes rubocop#437.

This PR fixes a false positive for `Performance/ChainArrayAllocation`
when using `select` with block argument after `select`.
koic added a commit to koic/rubocop-performance that referenced this issue Feb 2, 2024
…ocation`

Fixes rubocop#437.

This PR fixes a false positive for `Performance/ChainArrayAllocation`
when using `select` with block argument after `select`.
koic added a commit that referenced this issue Feb 3, 2024
…e_chain_array_allocation

[Fix #437] Fix a false positive for `Performance/ChainArrayAllocation`
@koic koic closed this as completed in #439 Feb 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants