Skip to content

Commit

Permalink
[Fix rubocop#8869] Fix a false positive for Style/RedundantBegin
Browse files Browse the repository at this point in the history
Fixes rubocop#8869.

This PR fixes a false positive for `Style/RedundantBegin`
when using `begin` for or assignment and method call.
  • Loading branch information
koic committed Oct 9, 2020
1 parent 45e71c2 commit 1a64fa9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)

Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/style/redundant_begin.rb
Expand Up @@ -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?
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/redundant_begin_spec.rb
Expand Up @@ -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)
Expand Down

0 comments on commit 1a64fa9

Please sign in to comment.