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 infinite loop error for Style/IfWithSemicolon #13497

Conversation

koic
Copy link
Member

@koic koic commented Nov 26, 2024

This PR fixes infinite loop error for Style/IfWithSemicolon when using nested if/;/end in if body.

$ cat example.rb
if cond1; foo + if cond2; bar
  else
  end
end
$ bundle exec rubocop -a -d
(snip)
Parser::Source::TreeRewriter detected clobbering
/Users/koic/.rbenv/versions/3.4-dev/lib/ruby/gems/3.4.0+0/gems/parser-3.3.6.0/lib/parser/source/tree_rewriter.rb:427:
in 'Parser::Source::TreeRewriter#trigger_policy'
/Users/koic/.rbenv/versions/3.4-dev/lib/ruby/gems/3.4.0+0/gems/parser-3.3.6.0/lib/parser/source/tree_rewriter.rb:414:
in 'Parser::Source::TreeRewriter#enforce_policy'
/Users/koic/.rbenv/versions/3.4-dev/lib/ruby/gems/3.4.0+0/gems/parser-3.3.6.0/lib/parser/source/tree_rewriter/action.rb:234:
in 'Method#call'

The autocorrection for the nested conventional case is handled as a repeated autocorrection, so there is no impact on the user experience.


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.

@@ -14,6 +14,7 @@ module Style
# result = some_condition ? something : another_thing
#
class IfWithSemicolon < Base
include IgnoredNode
Copy link
Collaborator

Choose a reason for hiding this comment

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

IgnoredNode is already included in RuboCop::Cop::Base, so including it again inside a cop is redundant.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I see! I've updated this PR. Thank you.

This PR fixes infinite loop error for `Style/IfWithSemicolon`
when using nested if/;/end in if body.

```console
$ cat example.rb
if cond1; foo + if cond2; bar
  else
  end
end
$ bundle exec rubocop -a -d
(snip)
Parser::Source::TreeRewriter detected clobbering
/Users/koic/.rbenv/versions/3.4-dev/lib/ruby/gems/3.4.0+0/gems/parser-3.3.6.0/lib/parser/source/tree_rewriter.rb:427:
in 'Parser::Source::TreeRewriter#trigger_policy'
/Users/koic/.rbenv/versions/3.4-dev/lib/ruby/gems/3.4.0+0/gems/parser-3.3.6.0/lib/parser/source/tree_rewriter.rb:414:
in 'Parser::Source::TreeRewriter#enforce_policy'
/Users/koic/.rbenv/versions/3.4-dev/lib/ruby/gems/3.4.0+0/gems/parser-3.3.6.0/lib/parser/source/tree_rewriter/action.rb:234:
in 'Method#call'
```

The autocorrection for the nested conventional case is handled as a repeated autocorrection,
so there is no impact on the user experience.
@koic koic force-pushed the fix_infinite_loop_error_for_style_if_with_semicolon branch from 71169e9 to 326982c Compare November 26, 2024 09:04
@bbatsov bbatsov merged commit 8ac8947 into rubocop:master Nov 26, 2024
22 checks passed
@bbatsov
Copy link
Collaborator

bbatsov commented Nov 26, 2024

Thanks!

@koic koic deleted the fix_infinite_loop_error_for_style_if_with_semicolon branch November 26, 2024 10:27
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