Skip to content

Commit

Permalink
[Fix #8179] Fix an infinite correction loop for `Layout/MultilineBloc…
Browse files Browse the repository at this point in the history
…kLayout`

Fixes #8179.

This PR fixes an infinite correction loop error for `Layout/MultilineBlockLayout`
when missing newline before opening parenthesis `(` for block body.

```ruby
# frozen_string_literal: true

foo do |o| (
    bar
  )
end
```

```console
% rubocop -a --only Layout/MultilineBlockLayout
Inspecting 1 file
C

Offenses:

example.rb:3:12: C: [Corrected] Layout/MultilineBlockLayout: Block body
expression is on the same line as the block start.
foo do |o| ( ...
           ^

0 files inspected, 1 offense detected, 1 offense corrected
Infinite loop detected in /Users/koic/src/github.com/koic/rubocop-issues/8179/example.rb.
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rubocop-0.85.1/lib/rubocop/runner.rb:276:in
`block in iterate_until_no_changes'
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rubocop-0.85.1/lib/rubocop/runner.rb:272:in
`loop'
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rubocop-0.85.1/lib/rubocop/runner.rb:272:in
`iterate_until_no_changes'
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rubocop-0.85.1/lib/rubocop/runner.rb:243:in
`do_inspection_loop'
/Users/koic/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rubocop-0.85.1/lib/rubocop/runner.rb:122:in
`block in file_offenses'
```
  • Loading branch information
koic authored and bbatsov committed Jun 21, 2020
1 parent d1005ce commit 42a03c3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,7 @@
* [#7979](https://github.com/rubocop-hq/rubocop/issues/7979): Fix "uninitialized constant DidYouMean::SpellChecker" exception. ([@bquorning][])
* [#8098](https://github.com/rubocop-hq/rubocop/issues/8098): Fix a false positive for `Style/RedundantRegexpCharacterClass` when using interpolations. ([@owst][])
* [#8150](https://github.com/rubocop-hq/rubocop/pull/8150): Fix a false positive for `Layout/EmptyLinesAroundAttributeAccessor` when using attribute accessors in `if` ... `else` branches. ([@koic][])
* [#8179](https://github.com/rubocop-hq/rubocop/issues/8179): Fix an infinite correction loop error for `Layout/MultilineBlockLayout` when missing newline before opening parenthesis `(` for block body. ([@koic][])

### Changes

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/multiline_block_layout.rb
Expand Up @@ -121,7 +121,7 @@ def autocorrect_arguments(corrector, node)
end

def autocorrect_body(corrector, node, block_body)
first_node = if block_body.begin_type?
first_node = if block_body.begin_type? && !block_body.source.start_with?('(')
block_body.children.first
else
block_body
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/layout/multiline_block_layout_spec.rb
Expand Up @@ -216,6 +216,24 @@
RUBY
end

it 'registers an offense and corrects for missing newline before opening parenthesis `(` for block body' do
expect_offense(<<~RUBY)
foo do |o| (
^ Block body expression is on the same line as the block start.
bar
)
end
RUBY

expect_correction(<<~RUBY)
foo do |o|
(
bar
)
end
RUBY
end

it 'registers an offense and corrects a line-break within arguments' do
expect_offense(<<~RUBY)
test do |x,
Expand Down

0 comments on commit 42a03c3

Please sign in to comment.