Skip to content

Commit

Permalink
Change EnforcedStyle to standard_error for Lint/InheritException
Browse files Browse the repository at this point in the history
This PR change `EnforcedStyle` from `runtime_error` to `standard_error`
for `Lint/InheritException`.

I noticed this while investigating #10406.

Below is the inheritance tree for the exception classes.

```
---------------
|  Exception  |
---------------
      ^
      |
---------------
|StandardError| (default for `rescue`)
---------------
      ^
      |
---------------
|RuntimeError | (default for `raise`)
---------------
```

AFAIK, it seems that custom exceptions generally inherit from `StandardError`.
Subclasses of `StandardError` are handled with `rescue` by default.

`raise` creates a `RuntimeError` when the exception class is omitted.
If custom exception inherits from `RuntimeError`, the custom exception
and `RuntimeError` will be is-a.
If custom exception inherits from `StandardError`, the custom exception
and `RuntimeError` will not be is-a.
In other words, inheriting `StandardError` will not be mistakenly
handled as `raise` which omits the exception class.

So, inheriting `StandardError` rather than `RuntimeError` does not include
unnecessary inheritance for `rescue` handling.
  • Loading branch information
koic committed Feb 11, 2022
1 parent 4a3d37b commit bb3a8ab
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
@@ -0,0 +1 @@
* [#10407](https://github.com/rubocop/rubocop/pull/10407): Change `EnforcedStyle` from `runtime_error` to `standard_error` for `Lint/InheritException`. ([@koic][])
4 changes: 2 additions & 2 deletions config/default.yml
Expand Up @@ -1820,10 +1820,10 @@ Lint/InheritException:
VersionAdded: '0.41'
VersionChanged: '<<next>>'
# The default base class in favour of `Exception`.
EnforcedStyle: runtime_error
EnforcedStyle: standard_error
SupportedStyles:
- runtime_error
- standard_error
- runtime_error

Lint/InterpolationCheck:
Description: 'Raise warning for interpolation in single q strs.'
Expand Down
14 changes: 7 additions & 7 deletions lib/rubocop/cop/lint/inherit_exception.rb
Expand Up @@ -6,14 +6,14 @@ 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
# `RuntimeError` (default) or `StandardError` instead.
# `StandardError` (default) or `RuntimeError` instead.
#
# @safety
# This cop's autocorrection is unsafe because `rescue` that omit
# exception class handle `StandardError` and its subclasses,
# but not `Exception` and its subclasses.
#
# @example EnforcedStyle: runtime_error (default)
# @example EnforcedStyle: standard_error (default)
# # bad
#
# class C < Exception; end
Expand All @@ -22,11 +22,11 @@ module Lint
#
# # good
#
# class C < RuntimeError; end
# class C < StandardError; end
#
# C = Class.new(RuntimeError)
# C = Class.new(StandardError)
#
# @example EnforcedStyle: standard_error
# @example EnforcedStyle: runtime_error
# # bad
#
# class C < Exception; end
Expand All @@ -35,9 +35,9 @@ module Lint
#
# # good
#
# class C < StandardError; end
# class C < RuntimeError; end
#
# C = Class.new(StandardError)
# C = Class.new(RuntimeError)
class InheritException < Base
include ConfigurableEnforcedStyle
extend AutoCorrector
Expand Down

0 comments on commit bb3a8ab

Please sign in to comment.