Skip to content

Incorrect recommendation suggested by Performance/ChainArrayAllocation Cop #437

@matthewhively

Description

@matthewhively

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy