diff --git a/CHANGELOG.md b/CHANGELOG.md index e4e409bd6bf..f5e6801db8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * [#8782](https://github.com/rubocop-hq/rubocop/issues/8782): Fix incorrect autocorrection for `Style/TernaryParentheses` with `defined?`. ([@dvandersluis][]) * [#8864](https://github.com/rubocop-hq/rubocop/issues/8864): Fix false positive for `Style/RedundantBegin` with a postfix `while` or `until`. ([@dvandersluis][]) +* [#8869](https://github.com/rubocop-hq/rubocop/issues/8869): Fix a false positive for `Style/RedundantBegin` when using `begin` for or assignment and method call. ([@koic][]) ## 0.93.0 (2020-10-08) diff --git a/lib/rubocop/cop/style/redundant_begin.rb b/lib/rubocop/cop/style/redundant_begin.rb index e27d4abd966..97062a6c3c7 100644 --- a/lib/rubocop/cop/style/redundant_begin.rb +++ b/lib/rubocop/cop/style/redundant_begin.rb @@ -85,8 +85,7 @@ def on_block(node) end def on_kwbegin(node) - return if node.parent&.assignment? - return if node.parent&.post_condition_loop? + return if node.each_ancestor.any?(&:assignment?) || node.parent&.post_condition_loop? first_child = node.children.first return if first_child.rescue_type? || first_child.ensure_type? diff --git a/spec/rubocop/cop/style/redundant_begin_spec.rb b/spec/rubocop/cop/style/redundant_begin_spec.rb index bbc5a868132..7d70fc2de55 100644 --- a/spec/rubocop/cop/style/redundant_begin_spec.rb +++ b/spec/rubocop/cop/style/redundant_begin_spec.rb @@ -222,6 +222,17 @@ def method RUBY end + it 'does not register an offense when using `begin` for or assignment and method call' do + expect_no_offenses(<<~RUBY) + var ||= begin + foo + bar + end.baz do + qux + end + RUBY + end + context '< Ruby 2.5', :ruby24 do it 'accepts a do-end block with a begin-end' do expect_no_offenses(<<~RUBY)