diff --git a/CHANGELOG.md b/CHANGELOG.md index 8127fb50255..dcb8e3949b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/layout/multiline_block_layout.rb b/lib/rubocop/cop/layout/multiline_block_layout.rb index 2b591734c1b..c9f8c8be112 100644 --- a/lib/rubocop/cop/layout/multiline_block_layout.rb +++ b/lib/rubocop/cop/layout/multiline_block_layout.rb @@ -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 diff --git a/spec/rubocop/cop/layout/multiline_block_layout_spec.rb b/spec/rubocop/cop/layout/multiline_block_layout_spec.rb index 2a972c80bf2..4ed60d77881 100644 --- a/spec/rubocop/cop/layout/multiline_block_layout_spec.rb +++ b/spec/rubocop/cop/layout/multiline_block_layout_spec.rb @@ -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,