Skip to content

Commit

Permalink
Merge pull request #8822 from koic/make_style_redundant_begin_aware_o…
Browse files Browse the repository at this point in the history
…f_begin_without_rescue

Make `Style/RedundantBegin` aware of `begin` without `rescue`
  • Loading branch information
koic committed Oct 5, 2020
2 parents 30d70fc + 7b94b88 commit d4ce6ce
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
* [#8796](https://github.com/rubocop-hq/rubocop/pull/8796): Add new `Lint/HashCompareByIdentity` cop. ([@fatkodima][])
* [#8668](https://github.com/rubocop-hq/rubocop/pull/8668): Add new `Lint/RedundantSafeNavigation` cop. ([@fatkodima][])
* [#8842](https://github.com/rubocop-hq/rubocop/issues/8842): Add notification about cache being used to debug mode. ([@hatkyinc2][])
* [#8822](https://github.com/rubocop-hq/rubocop/pull/8822): Make `Style/RedundantBegin` aware of `begin` without `rescue` or `ensure`. ([@koic][])

### Bug fixes

Expand Down
8 changes: 8 additions & 0 deletions docs/modules/ROOT/pages/cops_style.adoc
Expand Up @@ -7557,6 +7557,14 @@ rescue StandardError => e
something
end
# bad
begin
do_something
end
# good
do_something
# bad
# When using Ruby 2.5 or later.
do_something do
Expand Down
34 changes: 26 additions & 8 deletions lib/rubocop/cop/style/redundant_begin.rb
Expand Up @@ -28,6 +28,14 @@ module Style
# end
#
# # bad
# begin
# do_something
# end
#
# # good
# do_something
#
# # bad
# # When using Ruby 2.5 or later.
# do_something do
# begin
Expand Down Expand Up @@ -60,7 +68,9 @@ class RedundantBegin < Base
MSG = 'Redundant `begin` block detected.'

def on_def(node)
check(node)
return unless node.body&.kwbegin_type?

register_offense(node.body)
end
alias on_defs on_def

Expand All @@ -69,18 +79,26 @@ def on_block(node)

return if node.send_node.lambda_literal?
return if node.braces?
return unless node.body&.kwbegin_type?

check(node)
register_offense(node.body)
end

private
def on_kwbegin(node)
return if node.parent&.assignment?

def check(node)
return unless node.body&.kwbegin_type?
first_child = node.children.first
return if first_child.rescue_type? || first_child.ensure_type?

register_offense(node)
end

private

add_offense(node.body.loc.begin) do |corrector|
corrector.remove(node.body.loc.begin)
corrector.remove(node.body.loc.end)
def register_offense(node)
add_offense(node.loc.begin) do |corrector|
corrector.remove(node.loc.begin)
corrector.remove(node.loc.end)
end
end
end
Expand Down
49 changes: 49 additions & 0 deletions spec/rubocop/cop/style/redundant_begin_spec.rb
Expand Up @@ -155,6 +155,55 @@ def method
RUBY
end

it 'registers an offense and corrects when using `begin` without `rescue` or `ensure`' do
expect_offense(<<~RUBY)
begin
^^^^^ Redundant `begin` block detected.
do_something
end
RUBY

expect_correction("\n do_something\n\n")
end

it 'does not register an offense when using `begin` with `rescue`' do
expect_no_offenses(<<~RUBY)
begin
do_something
rescue
handle_exception
end
RUBY
end

it 'does not register an offense when using `begin` with `ensure`' do
expect_no_offenses(<<~RUBY)
begin
do_something
ensure
finalize
end
RUBY
end

it 'does not register an offense when using `begin` for assignment' do
expect_no_offenses(<<~RUBY)
var = begin
foo
bar
end
RUBY
end

it 'does not register an offense when using `begin` for or assignment' do
expect_no_offenses(<<~RUBY)
var ||= begin
foo
bar
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 d4ce6ce

Please sign in to comment.