rubocop / rubocop-performance

An extension of RuboCop focused on code performance checks.
https://docs.rubocop.org/rubocop-performance
MIT License
682 stars 80 forks source link

Incorrect recommendation suggested by Performance/ChainArrayAllocation Cop #437

Closed matthewhively closed 9 months ago

matthewhively commented 9 months ago

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?