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

Fix an error for Performance/MapCompact when using map(&:do_something).compact.first and there is a line break after map.compact and receiver #285

Merged
merged 1 commit into from
Mar 4, 2022

Conversation

ydah
Copy link
Member

@ydah ydah commented Mar 3, 2022

This fixes an issue where code like the following would cause an error in the Performance/MapCompact cop:

collection
  .map(&:do_something).compact
  .first

The following error was outcome:

An error occurred while Performance/MapCompact cop was inspecting /home/ydah/sandbox/test.rb:1:0.
Parser::Source::TreeRewriter detected clobbering
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/parser-3.1.1.0/lib/parser/source/tree_rewriter.rb:427:in `trigger_policy'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/parser-3.1.1.0/lib/parser/source/tree_rewriter.rb:414:in `enforce_policy'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/parser-3.1.1.0/lib/parser/source/tree_rewriter/action.rb:233:in `call'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/parser-3.1.1.0/lib/parser/source/tree_rewriter/action.rb:233:in `swallow'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/parser-3.1.1.0/lib/parser/source/tree_rewriter/action.rb:97:in `with'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/parser-3.1.1.0/lib/parser/source/tree_rewriter/action.rb:119:in `place_in_hierarchy'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/parser-3.1.1.0/lib/parser/source/tree_rewriter/action.rb:106:in `do_combine'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/parser-3.1.1.0/lib/parser/source/tree_rewriter/action.rb:30:in `combine'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/parser-3.1.1.0/lib/parser/source/tree_rewriter.rb:400:in `combine'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/parser-3.1.1.0/lib/parser/source/tree_rewriter.rb:194:in `replace'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/parser-3.1.1.0/lib/parser/source/tree_rewriter.rb:218:in `remove'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-performance-1.13.2/lib/rubocop/cop/performance/map_compact.rb:78:in `remove_compact_method'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-performance-1.13.2/lib/rubocop/cop/performance/map_compact.rb:61:in `block in on_send'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cop/base.rb:342:in `correct'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cop/base.rb:127:in `add_offense'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-performance-1.13.2/lib/rubocop/cop/performance/map_compact.rb:59:in `on_send'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cop/commissioner.rb:136:in `public_send'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cop/commissioner.rb:136:in `block (2 levels) in trigger_restricted_cops'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cop/commissioner.rb:160:in `with_cop_error_handling'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cop/commissioner.rb:135:in `block in trigger_restricted_cops'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cop/commissioner.rb:134:in `each'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cop/commissioner.rb:134:in `trigger_restricted_cops'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cop/commissioner.rb:70:in `on_send'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-ast-1.16.0/lib/rubocop/ast/traversal.rb:158:in `block in on_send'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-ast-1.16.0/lib/rubocop/ast/traversal.rb:155:in `each'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-ast-1.16.0/lib/rubocop/ast/traversal.rb:155:in `each_with_index'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-ast-1.16.0/lib/rubocop/ast/traversal.rb:155:in `on_send'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cop/commissioner.rb:71:in `on_send'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-ast-1.16.0/lib/rubocop/ast/traversal.rb:20:in `walk'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cop/commissioner.rb:86:in `investigate'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cop/team.rb:155:in `investigate_partial'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cop/team.rb:83:in `investigate'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/runner.rb:309:in `inspect_file'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/runner.rb:253:in `block in do_inspection_loop'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/runner.rb:287:in `block in iterate_until_no_changes'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/runner.rb:280:in `loop'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/runner.rb:280:in `iterate_until_no_changes'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/runner.rb:249:in `do_inspection_loop'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/runner.rb:130:in `block in file_offenses'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/runner.rb:155:in `file_offense_cache'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/runner.rb:129:in `file_offenses'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/runner.rb:120:in `process_file'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/runner.rb:101:in `block in each_inspected_file'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/runner.rb:100:in `each'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/runner.rb:100:in `reduce'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/runner.rb:100:in `each_inspected_file'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/runner.rb:86:in `inspect_files'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/runner.rb:47:in `run'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cli/command.rb:11:in `run'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cli/environment.rb:18:in `run'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cli.rb:71:in `run_command'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cli.rb:78:in `execute_runners'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/lib/rubocop/cli.rb:47:in `run'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/exe/rubocop:12:in `block in <top (required)>'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/2.7.0/benchmark.rb:308:in `realtime'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rubocop-1.25.1/exe/rubocop:12:in `<top (required)>'
/home/ydah/.rbenv/versions/2.7.2/bin/rubocop:23:in `load'
/home/ydah/.rbenv/versions/2.7.2/bin/rubocop:23:in `<top (required)>'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bundler-2.2.33/lib/bundler/cli/exec.rb:58:in `load'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bundler-2.2.33/lib/bundler/cli/exec.rb:58:in `kernel_load'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bundler-2.2.33/lib/bundler/cli/exec.rb:23:in `run'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bundler-2.2.33/lib/bundler/cli.rb:479:in `exec'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bundler-2.2.33/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bundler-2.2.33/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bundler-2.2.33/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bundler-2.2.33/lib/bundler/cli.rb:31:in `dispatch'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bundler-2.2.33/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bundler-2.2.33/lib/bundler/cli.rb:25:in `start'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bundler-2.2.33/exe/bundle:49:in `block in <top (required)>'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bundler-2.2.33/lib/bundler/friendly_errors.rb:103:in `with_friendly_errors'
/home/ydah/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bundler-2.2.33/exe/bundle:37:in `<top (required)>'
/home/ydah/.rbenv/versions/2.7.2/bin/bundle:23:in `load'
/home/ydah/.rbenv/versions/2.7.2/bin/bundle:23:in `<main>'

Versions:

  • rubocop: 1.25.1 (using Parser 3.1.1.0, rubocop-ast 1.16.0, running on ruby 2.7.2 x86_64-darwin20)
  • rubocop-performance: 1.13.2

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

…ing).compact.first` and there is a line break after `map.compact` and receiver
@ydah ydah marked this pull request as ready for review March 3, 2022 11:41
@koic koic merged commit 714833d into rubocop:master Mar 4, 2022
@koic
Copy link
Member

koic commented Mar 4, 2022

Thanks!

@ydah ydah deleted the fix-map-compact-error branch March 4, 2022 02:47
@koic
Copy link
Member

koic commented Mar 5, 2022

JFYI, RuboCop Performance 1.13.3 has been released.
https://github.com/rubocop/rubocop-performance/releases/tag/v1.13.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants