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

Fix a false positive for Lint/InheritException #10406

Merged
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
@@ -0,0 +1 @@
* [#10406](https://github.com/rubocop/rubocop/pull/10406): Fix a false positive for `Lint/InheritException` when inheriting a standard lib exception class that is not a subclass of `StandardError`. ([@koic][])
30 changes: 8 additions & 22 deletions lib/rubocop/cop/lint/inherit_exception.rb
Expand Up @@ -3,10 +3,9 @@
module RuboCop
module Cop
module Lint
# This cop looks for error classes inheriting from `Exception`
# and its standard library subclasses, excluding subclasses of
# `StandardError`. It is configurable to suggest using either
# `StandardError` (default) or `RuntimeError` instead.
# This cop looks for error classes inheriting from `Exception`.
# It is configurable to suggest using either `StandardError` (default) or
# `RuntimeError` instead.
#
# @safety
# This cop's autocorrection is unsafe because `rescue` that omit
Expand Down Expand Up @@ -42,24 +41,11 @@ class InheritException < Base
include ConfigurableEnforcedStyle
extend AutoCorrector

MSG = 'Inherit from `%<prefer>s` instead of `%<current>s`.'
MSG = 'Inherit from `%<prefer>s` instead of `Exception`.'
PREFERRED_BASE_CLASS = {
runtime_error: 'RuntimeError',
standard_error: 'StandardError'
}.freeze
ILLEGAL_CLASSES = %w[
Exception
SystemStackError
NoMemoryError
SecurityError
NotImplementedError
LoadError
SyntaxError
ScriptError
Interrupt
SignalException
SystemExit
].freeze

RESTRICT_ON_SEND = %i[new].freeze

Expand All @@ -71,7 +57,7 @@ class InheritException < Base
PATTERN

def on_class(node)
return unless node.parent_class && illegal_class_name?(node.parent_class)
return unless node.parent_class && exception_class?(node.parent_class)

message = message(node.parent_class)

Expand All @@ -82,7 +68,7 @@ def on_class(node)

def on_send(node)
constant = class_new_call?(node)
return unless constant && illegal_class_name?(constant)
return unless constant && exception_class?(constant)

message = message(constant)

Expand All @@ -97,8 +83,8 @@ def message(node)
format(MSG, prefer: preferred_base_class, current: node.const_name)
end

def illegal_class_name?(class_node)
ILLEGAL_CLASSES.include?(class_node.const_name)
def exception_class?(class_node)
class_node.const_name == 'Exception'
end

def preferred_base_class
Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/lint/inherit_exception_spec.rb
Expand Up @@ -28,6 +28,14 @@ class C < RuntimeError; end
RUBY
end
end

context 'when inheriting a standard lib exception class that is not a subclass of `StandardError`' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class C < Interrupt; end
RUBY
end
end
end

context 'with enforced style set to `standard_error`' do
Expand Down Expand Up @@ -56,6 +64,14 @@ class C < StandardError; end
RUBY
end
end

context 'when inheriting a standard lib exception class that is not a subclass of `StandardError`' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class C < Interrupt; end
RUBY
end
end
end
end
end