Skip to content

lazy not accounted for in ChainArrayAllocation cop #230

@reedjosh1

Description

@reedjosh1

lazy in a chain should negate the ChainArrayAllocation cop.

Expected behavior

some_array.lazy.map(&:some_obj_method).reject(&:nil?).first

Should not trigger a perf cop since this is actually a very performant (at least not too aweful) way of getting the first non-nil result. lazy should negate the chain cop as it no longer is a performance issue.

Actual behavior

Cop triggers as follows:

[Performance/ChainArrayAllocation] [I]  Use unchained `map` and `reject!`(followed by `return array` if required) instead of chaining `map...reject`.

Steps to reproduce the problem

Run perf cops on above code snippet.

RuboCop version

$ grep cop Gemfile.lock
    rubocop (1.6.1)
      rubocop-ast (>= 1.2.0, < 2.0)
    rubocop-ast (1.4.1)
    rubocop-performance (1.9.1)
      rubocop (>= 0.90.0, < 2.0)
      rubocop-ast (>= 0.4.0)
    rubocop-rails (2.9.1)
      rubocop (>= 0.90.0, < 2.0)
    rubocop-rake (0.5.1)
      rubocop
    rubocop-rspec (2.1.0)
      rubocop (~> 1.0)
      rubocop-ast (>= 1.1.0)
      rubocop (>= 0.52)
  rubocop (= 1.6.1)
  rubocop-performance (= 1.9.1)
  rubocop-rails (= 2.9.1)
  rubocop-rake (= 0.5.1)
  rubocop-rspec (= 2.1.0)

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