forked from rubocop/rubocop
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix rubocop#7299] Implement Lint/RaiseException cop
- Loading branch information
Showing
7 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# This cop checks for `raise` or `fail` statements which are | ||
# raising `Exception` class. | ||
# | ||
# @example | ||
# # bad | ||
# raise Exception, 'Error message here' | ||
# | ||
# # good | ||
# raise StandardError, 'Error message here' | ||
class RaiseException < Cop | ||
MSG = 'Use `StandardError` over `Exception`.' | ||
|
||
def_node_matcher :exception?, <<~PATTERN | ||
(send nil? ${:raise :fail} (const _ :Exception) ... ) | ||
PATTERN | ||
|
||
def_node_matcher :exception_new_with_message?, <<~PATTERN | ||
(send nil? ${:raise :fail} | ||
(send (const _ :Exception) :new ... )) | ||
PATTERN | ||
|
||
def on_send(node) | ||
add_offense(node) if raise_exception?(node) | ||
end | ||
|
||
private | ||
|
||
def raise_exception?(node) | ||
exception?(node) || exception_new_with_message?(node) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::RaiseException do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense for `raise` with `::Exception`' do | ||
expect_offense(<<~RUBY) | ||
raise ::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`. | ||
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`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for `raise` with `Exception`' do | ||
expect_offense(<<~RUBY) | ||
raise 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`. | ||
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`. | ||
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`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for `fail` with `Exception`' do | ||
expect_offense(<<~RUBY) | ||
fail 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`. | ||
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`. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for `raise` without arguments' do | ||
expect_no_offenses('raise') | ||
end | ||
|
||
it 'does not register an offense for `fail` without arguments' do | ||
expect_no_offenses('fail') | ||
end | ||
end |