From 38bd12e7092818a5fad381e28a72bcc831e09b4c Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Thu, 8 Oct 2020 16:48:19 -0400 Subject: [PATCH] [Fix #8864] Fix false positive for `Style/RedundantBegin` with a postfix `while` or `until` --- CHANGELOG.md | 1 + lib/rubocop/cop/style/redundant_begin.rb | 1 + spec/rubocop/cop/style/redundant_begin_spec.rb | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed92221bcd7..e4e409bd6bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Bug fixes * [#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][]) ## 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 309897f4cf1..e27d4abd966 100644 --- a/lib/rubocop/cop/style/redundant_begin.rb +++ b/lib/rubocop/cop/style/redundant_begin.rb @@ -86,6 +86,7 @@ def on_block(node) def on_kwbegin(node) return if node.parent&.assignment? + return if 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 b6a71d91352..bbc5a868132 100644 --- a/spec/rubocop/cop/style/redundant_begin_spec.rb +++ b/spec/rubocop/cop/style/redundant_begin_spec.rb @@ -204,6 +204,24 @@ def method RUBY end + it 'does not register an offense when using `begin` with `while`' do + expect_no_offenses(<<~RUBY) + begin + do_first_thing + some_value = do_second_thing + end while some_value + RUBY + end + + it 'does not register an offense when using `begin` with `until`' do + expect_no_offenses(<<~RUBY) + begin + do_first_thing + some_value = do_second_thing + end until some_value + RUBY + end + context '< Ruby 2.5', :ruby24 do it 'accepts a do-end block with a begin-end' do expect_no_offenses(<<~RUBY)