Skip to content

Bug in Performance/MapCompact #242

@zverok

Description

@zverok

Minimal test case:

# .rubocop.yml
require: rubocop-performance

AllCops:
  TargetRubyVersion: 2.7
# test.rb
organization_ids = organizations.map do |organization|
  organization.id
end.compact

Run: rubocop test.rb --only Performance/MapCompact -d
Outcome:

An error occurred while Performance/MapCompact cop was inspecting /home/zverok/playground/ruby/rubocop-debug/test.rb:1:19.
undefined method `selector' for #<Parser::Source::Map::Variable:0x0000560675f2dc18>
/home/zverok/.rvm/gems/ruby-2.6.3/gems/rubocop-performance-1.11.2/lib/rubocop/cop/performance/map_compact.rb:78:in `invoke_method_after_map_compact_on_same_line?'

Note that the bug is NOT reproduced if we'll change code to the following:

organization_ids = organizations.map { |organization| organization.id }.compact

(Cop just suggest expected change: to use filter_map)

Putting aside the question "why the heck anybody will write code like that" (legacy codebase!), this seem to clearly be a bug.

Versions:

  • rubocop: 1.14.0 (using Parser 3.0.1.1, rubocop-ast 1.5.0, running on ruby 2.6.3 x86_64-linux)
  • rubocop-performance: 1.11.2

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