Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support autocorrection for Lint/RaiseException #8169

Merged
merged 1 commit into from Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@
* [#8151](https://github.com/rubocop-hq/rubocop/pull/8151): Support autocorrection for `Style/NestedTernaryOperator`. ([@koic][])
* [#8142](https://github.com/rubocop-hq/rubocop/pull/8142): Add `Lint/ConstantResolution` cop. ([@robotdana][])
* [#8170](https://github.com/rubocop-hq/rubocop/pull/8170): Support autocorrection for `Lint/RegexpAsCondition`. ([@koic][])
* [#8169](https://github.com/rubocop-hq/rubocop/pull/8169): Support autocorrection for `Lint/RaiseException`. ([@koic][])

### Bug fixes

Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -1622,6 +1622,7 @@ Lint/RaiseException:
Enabled: pending
Safe: false
VersionAdded: '0.81'
VersionChanged: '0.86'
AllowedImplicitNamespaces:
- 'Gem'

Expand Down
4 changes: 2 additions & 2 deletions docs/modules/ROOT/pages/cops_lint.adoc
Expand Up @@ -2139,9 +2139,9 @@ rather than meant to be part of the resulting symbols.

| Pending
| No
| No
| Yes (Unsafe)
| 0.81
| -
| 0.86
|===

This cop checks for `raise` or `fail` statements which are
Expand Down
8 changes: 8 additions & 0 deletions lib/rubocop/cop/lint/raise_exception.rb
Expand Up @@ -44,6 +44,14 @@ def on_send(node)
exception_new_with_message?(node, &check(node))
end

def autocorrect(node)
lambda do |corrector|
exception_class = node.children.first&.cbase_type? ? '::StandardError' : 'StandardError'

corrector.replace(node, exception_class)
end
end

private

def check(node)
Expand Down
70 changes: 59 additions & 11 deletions spec/rubocop/cop/lint/raise_exception_spec.rb
Expand Up @@ -3,74 +3,114 @@
RSpec.describe RuboCop::Cop::Lint::RaiseException, :config do
let(:cop_config) { { 'AllowedImplicitNamespaces' => ['Gem'] } }

it 'registers an offense for `raise` with `::Exception`' do
it 'registers an offense and corrects for `raise` with `::Exception`' do
expect_offense(<<~RUBY)
raise ::Exception
^^^^^^^^^^^ Use `StandardError` over `Exception`.
RUBY

expect_correction(<<~RUBY)
raise ::StandardError
RUBY
end

it 'registers an offense for `raise` with `::Exception.new`' do
it 'registers an offense and corrects for `raise` with `::Exception.new`' do
expect_offense(<<~RUBY)
raise ::Exception.new 'Error with exception'
^^^^^^^^^^^ Use `StandardError` over `Exception`.
RUBY

expect_correction(<<~RUBY)
raise ::StandardError.new 'Error with exception'
RUBY
end

it 'registers an offense for `raise` with `::Exception` and message' do
it 'registers an offense and corrects for `raise` with `::Exception` and message' do
expect_offense(<<~RUBY)
raise ::Exception, 'Error with exception'
^^^^^^^^^^^ Use `StandardError` over `Exception`.
RUBY

expect_correction(<<~RUBY)
raise ::StandardError, 'Error with exception'
RUBY
end

it 'registers an offense for `raise` with `Exception`' do
it 'registers an offense and corrects for `raise` with `Exception`' do
expect_offense(<<~RUBY)
raise Exception
^^^^^^^^^ Use `StandardError` over `Exception`.
RUBY

expect_correction(<<~RUBY)
raise StandardError
RUBY
end

it 'registers an offense for `raise` with `Exception` and message' do
it 'registers an offense and corrects for `raise` with `Exception` and message' do
expect_offense(<<~RUBY)
raise Exception, 'Error with exception'
^^^^^^^^^ Use `StandardError` over `Exception`.
RUBY

expect_correction(<<~RUBY)
raise StandardError, 'Error with exception'
RUBY
end

it 'registers an offense for `raise` with `Exception.new` and message' do
it 'registers an offense and corrects for `raise` with `Exception.new` and message' do
expect_offense(<<~RUBY)
raise Exception.new 'Error with exception'
^^^^^^^^^ Use `StandardError` over `Exception`.
RUBY

expect_correction(<<~RUBY)
raise StandardError.new 'Error with exception'
RUBY
end

it 'registers an offense for `raise` with `Exception.new(args*)` ' do
it 'registers an offense and corrects for `raise` with `Exception.new(args*)` ' do
expect_offense(<<~RUBY)
raise Exception.new('arg1', 'arg2')
^^^^^^^^^ Use `StandardError` over `Exception`.
RUBY

expect_correction(<<~RUBY)
raise StandardError.new('arg1', 'arg2')
RUBY
end

it 'registers an offense for `fail` with `Exception`' do
it 'registers an offense and corrects for `fail` with `Exception`' do
expect_offense(<<~RUBY)
fail Exception
^^^^^^^^^ Use `StandardError` over `Exception`.
RUBY

expect_correction(<<~RUBY)
fail StandardError
RUBY
end

it 'registers an offense for `fail` with `Exception` and message' do
it 'registers an offense and corrects for `fail` with `Exception` and message' do
expect_offense(<<~RUBY)
fail Exception, 'Error with exception'
^^^^^^^^^ Use `StandardError` over `Exception`.
RUBY

expect_correction(<<~RUBY)
fail StandardError, 'Error with exception'
RUBY
end

it 'registers an offense for `fail` with `Exception.new` and message' do
it 'registers an offense and corrects for `fail` with `Exception.new` and message' do
expect_offense(<<~RUBY)
fail Exception.new 'Error with exception'
^^^^^^^^^ Use `StandardError` over `Exception`.
RUBY

expect_correction(<<~RUBY)
fail StandardError.new 'Error with exception'
RUBY
end

it 'does not register an offense for `raise` without arguments' do
Expand Down Expand Up @@ -99,7 +139,7 @@ def self.foo
RUBY
end

it 'does not register an offense when Exception with cbase specified' do
it 'registers an offense and corrects when Exception with cbase specified' do
expect_offense(<<~RUBY)
module Gem
def self.foo
Expand All @@ -108,6 +148,14 @@ def self.foo
end
end
RUBY

expect_correction(<<~RUBY)
module Gem
def self.foo
raise ::StandardError
end
end
RUBY
end
end
end