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

Bug in Performance/MapCompact #242

Closed
zverok opened this issue May 5, 2021 · 2 comments · Fixed by #243
Closed

Bug in Performance/MapCompact #242

zverok opened this issue May 5, 2021 · 2 comments · Fixed by #243
Labels
bug Something isn't working

Comments

@zverok
Copy link

zverok commented May 5, 2021

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
@koic koic added the bug Something isn't working label May 6, 2021
koic added a commit to koic/rubocop-performance that referenced this issue May 6, 2021
Fixes rubocop#242.

This PR fixes an error for `Performance/MapCompact` when using
multiline `map { ... }.compact` and assigning to return value.
@koic koic closed this as completed in #243 May 6, 2021
koic added a commit that referenced this issue May 6, 2021
[Fix #242] Fix an error for `Performance/MapCompact`
@koic
Copy link
Member

koic commented May 6, 2021

Thank you for the feedback! I've released RuboCop Performance 1.11.3.

@zverok
Copy link
Author

zverok commented May 6, 2021

@koic Wow, thank you for a quick fix 😻

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