Skip to content

Commit

Permalink
Merge pull request #8168 from koic/refine_offense_range_for_raise_exc…
Browse files Browse the repository at this point in the history
…eption

Refine offense range for `Lint/RaiseException`
  • Loading branch information
koic committed Jun 19, 2020
2 parents 0a0408a + fd4239c commit 17e4dcd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions lib/rubocop/cop/lint/raise_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class RaiseException < Cop
MSG = 'Use `StandardError` over `Exception`.'

def_node_matcher :exception?, <<~PATTERN
(send nil? {:raise :fail} (const ${cbase nil?} :Exception) ... )
(send nil? {:raise :fail} $(const ${cbase nil?} :Exception) ... )
PATTERN

def_node_matcher :exception_new_with_message?, <<~PATTERN
(send nil? {:raise :fail}
(send (const ${cbase nil?} :Exception) :new ... ))
(send $(const ${cbase nil?} :Exception) :new ... ))
PATTERN

def on_send(node)
Expand All @@ -47,10 +47,10 @@ def on_send(node)
private

def check(node)
lambda do |cbase|
lambda do |exception_class, cbase|
return if cbase.nil? && implicit_namespace?(node)

add_offense(node)
add_offense(exception_class)
end
end

Expand Down
22 changes: 11 additions & 11 deletions spec/rubocop/cop/lint/raise_exception_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,70 @@
it 'registers an offense for `raise` with `::Exception`' do
expect_offense(<<~RUBY)
raise ::Exception
^^^^^^^^^^^^^^^^^ Use `StandardError` over `Exception`.
^^^^^^^^^^^ Use `StandardError` over `Exception`.
RUBY
end

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

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

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

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

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

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

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

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

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

Expand Down Expand Up @@ -104,7 +104,7 @@ def self.foo
module Gem
def self.foo
raise ::Exception
^^^^^^^^^^^^^^^^^ Use `StandardError` over `Exception`.
^^^^^^^^^^^ Use `StandardError` over `Exception`.
end
end
RUBY
Expand Down

0 comments on commit 17e4dcd

Please sign in to comment.