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

Parser::Source::TreeRewriter detected clobbering during Style/RedundantBegin autocorrect #10631

Closed
bquorning opened this issue May 13, 2022 · 1 comment · Fixed by #10650
Closed
Labels

Comments

@bquorning
Copy link
Contributor

Steps to reproduce the problem

When running rubocop --only=Style/RedundantBegin,Layout/BeginEndAlignment -d -a test.rb against a file test.rb with the following contents:

@foo ||= begin
  @bar ||= begin
    baz
  end
end

I get (among other things) this output:

Inspecting 1 file
Scanning /Users/bquorning/Code/rubocop/rubocop/test.rb
An error occurred while Style/RedundantBegin cop was inspecting /Users/[…]/test.rb:2:11.
Parser::Source::TreeRewriter detected clobbering

RuboCop version

$ rubocop -V
1.29.1 (using Parser 3.1.2.0, rubocop-ast 1.17.0, running on ruby 2.7.6 x86_64-darwin20)
  - rubocop-performance 1.13.2
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.10.0
@koic koic added the bug label May 13, 2022
@dvandersluis
Copy link
Member

dvandersluis commented May 13, 2022

This actually raises a clobbering error with only Style/RedundantBegin too. Because the cop is fixing layout as well, the same range gets changed by the autocorrection for both nodes.

I tried changing autocorrection for Style/RedundantBegin to just remove the begin and end keywords which prevents the clobbering error; however, when run on the sample code, we get a clobbering error from Layout/RedundantLineBreak instead.

The clobbering error is of the swallowed_insertions_conflict type. I tried using autocorrect_incompatible_with but it does not help.

Offenses:

test.rb:1:1: C: [Corrected] Layout/RedundantLineBreak: Redundant line break detected.
@foo ||=  ...
^^^^^^^^^
test.rb:1:9: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
@foo ||=
        ^
test.rb:1:10: C: [Corrected] Style/RedundantBegin: Redundant begin block detected.
@foo ||= begin
         ^^^^^
test.rb:2:11: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
  @bar ||=
          ^
test.rb:2:12: C: [Corrected] Style/RedundantBegin: Redundant begin block detected.
  @bar ||= begin
           ^^^^^
test.rb:4:1: C: [Corrected] Layout/TrailingEmptyLines: 2 trailing blank lines detected.
test.rb:4:1: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.

1 file inspected, 8 offenses detected, 7 offenses corrected, 1 more offense can be corrected with `rubocop -A`

1 error occurred:
An error occurred while Layout/RedundantLineBreak cop was inspecting /Users/daniel/RubymineProjects/rubocop/test.rb:2:2.

j-miyake added a commit to j-miyake/rubocop that referenced this issue May 19, 2022
koic added a commit that referenced this issue May 20, 2022
…ndant_begin

[Fix #10631] Fix autocorrect for `Style/RedundantBegin`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants