diff --git a/CHANGELOG.md b/CHANGELOG.md index fde0ef37340..18890f4c21b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/modules/ROOT/pages/cops_style.adoc b/docs/modules/ROOT/pages/cops_style.adoc index 53e3b5a10fd..d57f0caa66a 100644 --- a/docs/modules/ROOT/pages/cops_style.adoc +++ b/docs/modules/ROOT/pages/cops_style.adoc @@ -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 diff --git a/lib/rubocop/cop/style/redundant_begin.rb b/lib/rubocop/cop/style/redundant_begin.rb index 19bf39dcb62..309897f4cf1 100644 --- a/lib/rubocop/cop/style/redundant_begin.rb +++ b/lib/rubocop/cop/style/redundant_begin.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/rubocop/cop/style/redundant_begin_spec.rb b/spec/rubocop/cop/style/redundant_begin_spec.rb index b7bb1b8ab8e..b6a71d91352 100644 --- a/spec/rubocop/cop/style/redundant_begin_spec.rb +++ b/spec/rubocop/cop/style/redundant_begin_spec.rb @@ -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)