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 incorrect auto-correct for Style/RedundantBegin #9777

Conversation

koic
Copy link
Member

@koic koic commented May 6, 2021

This PR fixes an incorrect auto-correct for Style/RedundantBegin when using multi-line if in begin block.

And fixes standardrb/standard#289 that is causing the following infinite loop error due to Style/RedundantBegin with Style/MultilineMemoization.

% cat example.rb
@memo ||= begin
  if condition
    do_something
  end
end

% bundle exec rubocop -a --only Style/RedundantBegin,Style/MultilineMemoization
(snip)

Inspecting 1 file
C

Offenses:

example.rb:1:1: C: [Corrected] Style/MultilineMemoization: Wrap
multiline memoization blocks in begin and end.
@memo ||= (if condition ...
^^^^^^^^^^^^^^^^^^^^^^^
example.rb:1:11: C: [Corrected] Style/RedundantBegin: Redundant begin
block detected.
@memo ||= begin
          ^^^^^

0 files inspected, 2 offenses detected, 2 offenses corrected
Infinite loop detected in
/Users/koic/src/github.com/koic/rubocop-issues/289/example.rb and caused
by Style/MultilineMemoization
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/runner.rb:285:in
`block in iterate_until_no_changes'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/runner.rb:281:in
`loop'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/runner.rb:281:in `iterate_until_no_changes'

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.

@koic koic force-pushed the fix_an_incorrect_autocorrect_for_style_redundant_begin branch from d4e2fc6 to 70b1982 Compare May 6, 2021 07:37
@@ -248,6 +248,23 @@ def method
RUBY
end

it 'registers and corrects an offense when using `if` multiple statements in `begin` block' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multiple or multi-line?

Btw, if is technically an expression, not a statement (it evaluates to something).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! I updated this PR. Thank you!

This PR fixes an incorrect auto-correct for `Style/RedundantBegin`
when using multi-line `if` in `begin` block.

And fixes standardrb/standard#289 that is causing
the following infinite loop error due to `Style/RedundantBegin` with
`Style/MultilineMemoization`.

```ruby
% cat example.rb
@memo ||= begin
  if condition
    do_something
  end
end

% bundle exec rubocop -a --only Style/RedundantBegin,Style/MultilineMemoization
(snip)

Inspecting 1 file
C

Offenses:

example.rb:1:1: C: [Corrected] Style/MultilineMemoization: Wrap
multiline memoization blocks in begin and end.
@memo ||= (if condition ...
^^^^^^^^^^^^^^^^^^^^^^^
example.rb:1:11: C: [Corrected] Style/RedundantBegin: Redundant begin
block detected.
@memo ||= begin
          ^^^^^

0 files inspected, 2 offenses detected, 2 offenses corrected
Infinite loop detected in
/Users/koic/src/github.com/koic/rubocop-issues/289/example.rb and caused
by Style/MultilineMemoization
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/runner.rb:285:in
`block in iterate_until_no_changes'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/runner.rb:281:in
`loop'
/Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/runner.rb:281:in `iterate_until_no_changes'
```
@koic koic force-pushed the fix_an_incorrect_autocorrect_for_style_redundant_begin branch from 70b1982 to b20bf22 Compare May 7, 2021 08:29
@bbatsov bbatsov merged commit 7be423a into rubocop:master May 7, 2021
@bbatsov
Copy link
Collaborator

bbatsov commented May 7, 2021

Thanks!

@koic koic deleted the fix_an_incorrect_autocorrect_for_style_redundant_begin branch May 7, 2021 08:52
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.

Infinite loop in --fix
2 participants