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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocorrect nested UnlessElse causing "Parser::Source::TreeRewriter detected clobbering" #10375

Closed
MaxLap opened this issue Jan 25, 2022 · 0 comments
Labels

Comments

@MaxLap
Copy link
Contributor

@MaxLap MaxLap commented Jan 25, 2022

Juniors can be so creative in how poor they can write code 馃槅

Running this snippet:

unless abc
  a
else
  unless cde
    b
  else
    c
  end
end

causes this message: An error occurred while Style/UnlessElse cop was inspecting the_snippet.rb

adding -d to obtain a backtrace:

Parser::Source::TreeRewriter detected clobbering
/home/max/.rvm/gems/ruby-2.6.3/gems/parser-3.1.0.0/lib/parser/source/tree_rewriter.rb:427:in `trigger_policy'
/home/max/.rvm/gems/ruby-2.6.3/gems/parser-3.1.0.0/lib/parser/source/tree_rewriter.rb:414:in `enforce_policy'
/home/max/.rvm/gems/ruby-2.6.3/gems/parser-3.1.0.0/lib/parser/source/tree_rewriter/action.rb:233:in `call'
/home/max/.rvm/gems/ruby-2.6.3/gems/parser-3.1.0.0/lib/parser/source/tree_rewriter/action.rb:233:in `swallow'
/home/max/.rvm/gems/ruby-2.6.3/gems/parser-3.1.0.0/lib/parser/source/tree_rewriter/action.rb:97:in `with'
/home/max/.rvm/gems/ruby-2.6.3/gems/parser-3.1.0.0/lib/parser/source/tree_rewriter/action.rb:124:in `place_in_hierarchy'
/home/max/.rvm/gems/ruby-2.6.3/gems/parser-3.1.0.0/lib/parser/source/tree_rewriter/action.rb:106:in `do_combine'
/home/max/.rvm/gems/ruby-2.6.3/gems/parser-3.1.0.0/lib/parser/source/tree_rewriter/action.rb:117:in `place_in_hierarchy'
/home/max/.rvm/gems/ruby-2.6.3/gems/parser-3.1.0.0/lib/parser/source/tree_rewriter/action.rb:131:in `block in combine_children'
/home/max/.rvm/gems/ruby-2.6.3/gems/parser-3.1.0.0/lib/parser/source/tree_rewriter/action.rb:130:in `each'
/home/max/.rvm/gems/ruby-2.6.3/gems/parser-3.1.0.0/lib/parser/source/tree_rewriter/action.rb:130:in `inject'
/home/max/.rvm/gems/ruby-2.6.3/gems/parser-3.1.0.0/lib/parser/source/tree_rewriter/action.rb:130:in `combine_children'
/home/max/.rvm/gems/ruby-2.6.3/gems/parser-3.1.0.0/lib/parser/source/tree_rewriter/action.rb:221:in `merge'
/home/max/.rvm/gems/ruby-2.6.3/gems/parser-3.1.0.0/lib/parser/source/tree_rewriter/action.rb:104:in `do_combine'
/home/max/.rvm/gems/ruby-2.6.3/gems/parser-3.1.0.0/lib/parser/source/tree_rewriter/action.rb:30:in `combine'
/home/max/.rvm/gems/ruby-2.6.3/gems/parser-3.1.0.0/lib/parser/source/tree_rewriter.rb:143:in `merge!'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cop/base.rb:291:in `apply_correction'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cop/base.rb:375:in `attempt_correction'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cop/base.rb:356:in `use_corrector'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cop/base.rb:350:in `correct'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cop/base.rb:127:in `add_offense'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cop/style/unless_else.rb:31:in `on_if'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:100:in `public_send'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:100:in `block (2 levels) in trigger_responding_cops'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:160:in `with_cop_error_handling'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:99:in `block in trigger_responding_cops'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:98:in `each'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:98:in `trigger_responding_cops'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:69:in `on_if'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-ast-1.15.1/lib/rubocop/ast/traversal.rb:154:in `on_if'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:71:in `on_if'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-ast-1.15.1/lib/rubocop/ast/traversal.rb:20:in `walk'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cop/commissioner.rb:86:in `investigate'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cop/team.rb:155:in `investigate_partial'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cop/team.rb:77:in `investigate'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/runner.rb:309:in `inspect_file'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/runner.rb:253:in `block in do_inspection_loop'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/runner.rb:287:in `block in iterate_until_no_changes'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/runner.rb:280:in `loop'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/runner.rb:280:in `iterate_until_no_changes'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/runner.rb:249:in `do_inspection_loop'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/runner.rb:130:in `block in file_offenses'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/runner.rb:155:in `file_offense_cache'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/runner.rb:129:in `file_offenses'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/runner.rb:120:in `process_file'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/runner.rb:101:in `block in each_inspected_file'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/runner.rb:100:in `each'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/runner.rb:100:in `reduce'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/runner.rb:100:in `each_inspected_file'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/runner.rb:86:in `inspect_files'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/runner.rb:47:in `run'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cli/command.rb:11:in `run'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cli/environment.rb:18:in `run'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cli.rb:71:in `run_command'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cli.rb:78:in `execute_runners'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/lib/rubocop/cli.rb:47:in `run'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/exe/rubocop:12:in `block in <top (required)>'
/home/max/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
/home/max/.rvm/gems/ruby-2.6.3/gems/rubocop-1.25.0/exe/rubocop:12:in `<top (required)>'
/home/max/.rvm/gems/ruby-2.6.3/bin/rubocop:23:in `load'
/home/max/.rvm/gems/ruby-2.6.3/bin/rubocop:23:in `<main>'
/home/max/.rvm/gems/ruby-2.6.3/bin/ruby_executable_hooks:24:in `eval'
/home/max/.rvm/gems/ruby-2.6.3/bin/ruby_executable_hooks:24:in `<main>'

Notes

Happens with -a and -A. Seems related to having nested uses of the cop.

RuboCop version

1.25.0 (using Parser 3.1.0.0, rubocop-ast 1.15.1, running on ruby 2.6.3 x86_64-linux)

RuboCop config

AllCops:
  DisplayCopNames: true
  Exclude:
    - 'db/**/*'
    - '_*/**/*'
    - 'private/**/*'
    - 'public/**/*'
  Include:
    # those are pure ruby files used by gem axlsx as view
    - '**/*.axlsx'
    - '**/*.rake'

  TargetRubyVersion: 2.6
  NewCops: disable

Command line

rubocop -d --config path_to_my_conf -a path_to_snippet.rb

@koic koic added the bug label Jan 25, 2022
@MaxLap MaxLap changed the title Nested UnlessElse causing "Parser::Source::TreeRewriter detected clobbering" Autocorrect nested UnlessElse causing "Parser::Source::TreeRewriter detected clobbering" Jan 25, 2022
@koic koic closed this as completed in 6915fd3 Mar 20, 2022
koic added a commit that referenced this issue Mar 20, 2022
鈥ect

[Fix #10375] Defer auto-correction of nested unless/else
koic pushed a commit to koic/rubocop that referenced this issue Mar 21, 2022
An unless/else nested inside another unless/else would cause rewrite clobbering
when auto-correcting both offenses. By ignoring the node after correcting it
and skipping auto-correction for inner nodes with the same offense, we avoid
this problem.

The auto-correct loop will pick up the remaining offense and fix it. No test
case is added for this particular aspect, but it's standard RuboCop behavior.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants