diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bf6c33c3f5..58f9e775840 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * [#7784](https://github.com/rubocop-hq/rubocop/pull/7784): Support Ruby 2.7's numbered parameter for `Lint/SafeNavigationChain`. ([@koic][]) * [#7331](https://github.com/rubocop-hq/rubocop/issues/7331): Add `forbidden` option to `Style/ModuleFunction` cop. ([@weh][]) * [#7699](https://github.com/rubocop-hq/rubocop/pull/7699): Add new `Lint/StructNewOverride` cop. ([@ybiquitous][]) +* [#7809](https://github.com/rubocop-hq/rubocop/pull/7809): Add auto-correction for `Style/EndBlock` cop. ([@tejasbubane][]) ### Bug fixes diff --git a/config/default.yml b/config/default.yml index 79a77e574fa..bca34d578f7 100644 --- a/config/default.yml +++ b/config/default.yml @@ -2711,6 +2711,7 @@ Style/EndBlock: StyleGuide: '#no-END-blocks' Enabled: true VersionAdded: '0.9' + VersionChanged: '0.81' Style/EvalWithLocation: Description: 'Pass `__FILE__` and `__LINE__` to `eval` method, as they are used by backtraces.' diff --git a/lib/rubocop/cop/style/end_block.rb b/lib/rubocop/cop/style/end_block.rb index f9404130270..1354d38f25f 100644 --- a/lib/rubocop/cop/style/end_block.rb +++ b/lib/rubocop/cop/style/end_block.rb @@ -19,6 +19,12 @@ class EndBlock < Cop def on_postexe(node) add_offense(node, location: :keyword) end + + def autocorrect(node) + lambda do |corrector| + corrector.replace(node.loc.keyword, 'at_exit') + end + end end end end diff --git a/manual/cops_style.md b/manual/cops_style.md index cba3722fc7d..b38162b6b99 100644 --- a/manual/cops_style.md +++ b/manual/cops_style.md @@ -1909,7 +1909,7 @@ This cop checks ensures source files have no utf-8 encoding comments. Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- -Enabled | Yes | No | 0.9 | - +Enabled | Yes | Yes | 0.9 | 0.81 This cop checks for END blocks. diff --git a/spec/rubocop/cop/style/end_block_spec.rb b/spec/rubocop/cop/style/end_block_spec.rb index 03738315cdb..c5c0c53c37f 100644 --- a/spec/rubocop/cop/style/end_block_spec.rb +++ b/spec/rubocop/cop/style/end_block_spec.rb @@ -3,10 +3,20 @@ RSpec.describe RuboCop::Cop::Style::EndBlock do subject(:cop) { described_class.new } - it 'reports an offense for an END block' do + it 'reports an offense and corrects END block' do expect_offense(<<~RUBY) END { test } ^^^ Avoid the use of `END` blocks. Use `Kernel#at_exit` instead. RUBY + + expect_correction(<<~RUBY) + at_exit { test } + RUBY + end + + it 'does not report offenses for other blocks' do + expect_no_offenses(<<~RUBY) + end_block { test } + RUBY end end