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

Conversation

koic
Copy link
Member

@koic koic commented Feb 9, 2022

Fixes testdouble/standard#388 and reverts #3427.

This PR prevents the following false positive and auto-correction.

 class TestClass
-  FatalError = Class.new(Interrupt)
+  FatalError = Class.new(RuntimeError)
 end

Below is an inheritance tree for the exception classes.

        ---------------
        | BasicObject |
        ---------------
               ^
               |
        ---------------
        |   Kernel    |
        ---------------
               ^
               |
        ---------------
        |   Object    |
        ---------------
               ^
               |
        ---------------
        |  Exception  |
        ---------------
               ^
               |
      |------------------|
---------------  -----------------
|StandardError|  |SignalException|
---------------  -----------------
      ^                  ^
      |                  |
---------------  -----------------
|RuntimeError |  |   Interrupt   |
---------------  -----------------

As the inheritance tree shows, Interrupt is not is_a? in either StandardError or RuntimeError.

RuntimeError.new.is_a?(StandardError) #=> true
Interrupt.new.is_a?(StandardError)    #=> false
Interrupt.new.is_a?(RuntimeError)     #=> false

This goes against The Liskov Substitution Principle. Interup should not be replaced with StandardError or its subclasses.
Also, according to The Open/Closed Principle, Interrup class should be open to inheritance.

The same is true for SystemStackError, NoMemoryError, SecurityError, NotImplementedError, LoadError, SyntaxError, ScriptError, SignalException, and SystemExit classes.

This PR allows for exception handling according to Object-oriented principles and Ruby exception handling mechanism.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

koic added a commit to koic/rubocop that referenced this issue Feb 9, 2022
This PR change `EnforcedStyle` from `runtime_error` to `standard_error`
for `Lint/InheritException`.

I noticed this while investigating rubocop#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.
koic added a commit to koic/rubocop that referenced this issue Feb 9, 2022
This PR marks `Lint/InheritException` as unsafe.

This cop's autocorrection is unsafe because `rescue` that omit
exception class handle `StandardError` and its subclasses,
but not `Exception` and its subclasses.

```ruby
begin
rescue # Handles only `StandardError` and its subclasses because exception class is omitted.
end
```

It's still unsafe after rubocop#10406 is resolved.
koic added a commit to koic/rubocop that referenced this issue Feb 9, 2022
This PR marks `Lint/InheritException` as unsafe.

This cop's autocorrection is unsafe because `rescue` that omit
exception class handle `StandardError` and its subclasses,
but not `Exception` and its subclasses.

```ruby
begin
rescue # Handles only `StandardError` and its subclasses because exception class is omitted.
end
```

It's still unsafe after rubocop#10406 is resolved.
koic added a commit to koic/rubocop that referenced this issue Feb 9, 2022
This PR marks `Lint/InheritException` as unsafe auto-correction.

This cop's autocorrection is unsafe because `rescue` that omit
exception class handle `StandardError` and its subclasses,
but not `Exception` and its subclasses.

```ruby
begin
rescue # Handles only `StandardError` and its subclasses because exception class is omitted.
end
```

It's still unsafe after rubocop#10406 is resolved.
@koic koic force-pushed the fix_a_false_positive_for_lint_inherit_exception branch from 8c5f9b1 to 0834571 Compare Feb 10, 2022
bbatsov pushed a commit that referenced this issue Feb 11, 2022
This PR marks `Lint/InheritException` as unsafe auto-correction.

This cop's autocorrection is unsafe because `rescue` that omit
exception class handle `StandardError` and its subclasses,
but not `Exception` and its subclasses.

```ruby
begin
rescue # Handles only `StandardError` and its subclasses because exception class is omitted.
end
```

It's still unsafe after #10406 is resolved.
koic added a commit to koic/rubocop that referenced this issue Feb 11, 2022
This PR change `EnforcedStyle` from `runtime_error` to `standard_error`
for `Lint/InheritException`.

I noticed this while investigating rubocop#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.
bbatsov pushed a commit that referenced this issue Feb 13, 2022
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.
@bbatsov
Copy link
Collaborator

bbatsov commented Feb 13, 2022

The same is true for SystemStackError, NoMemoryError, SecurityError, NotImplementedError, LoadError, SyntaxError, ScriptError, SignalException, and SystemExit classes.

I was the impression that those classes should not be extended by application code, as they are internal to Ruby, plus usually they can't be handled in a meaningful way. I think this was also the main point of the special check for them - if you end up using those most likely you're doing something wrong.

@koic
Copy link
Member Author

koic commented Feb 13, 2022

I think this Lint/InheritException cop has the role of protecting users from accidentally extending Exception class where they should extend StandardError or RuntimeError classes. This difference between Exception, StandardError, and RuntimeError may be mistaken by new Ruby user. I've seen some misunderstandings, especially among programmers skill transfering from Java to Ruby. This is because Java extends Exception class when creating custom exceptions.

On the other hand, I agree that it is rare for these exception classes to be extended at user layer, but these exception classes (e.g. LoadError) may be intentionally extended. In other words, it's possible that user intended it, not a mistake. So I think it's better to register an offense only with Exception.

Fixes testdouble/standard#388 and
reverts rubocop#3427.

This PR prevents the following false positive and auto-correction.

```diff
 class TestClass
-  FatalError = Class.new(Interrupt)
+  FatalError = Class.new(RuntimeError)
 end
```

Below is an inheritance tree for the exception classes.

```
        ---------------
        | BasicObject |
        ---------------
               ^
               |
        ---------------
        |   Kernel    |
        ---------------
               ^
               |
        ---------------
        |   Object    |
        ---------------
               ^
               |
        ---------------
        |  Exception  |
        ---------------
               ^
               |
      |------------------|
---------------  -----------------
|StandardError|  |SignalException|
---------------  -----------------
      ^                  ^
      |                  |
---------------  -----------------
|RuntimeError |  |   Interrupt   |
---------------  -----------------
```

As the inheritance tree shows, `Interrupt` is not `is_a?` in either
`StandardError` or `RuntimeError`.

```ruby
RuntimeError.new.is_a?(StandardError) #=> true
Interrupt.new.is_a?(StandardError)    #=> false
Interrupt.new.is_a?(RuntimeError)     #=> false
```

This goes against The Liskov Substitution Principle.
`Interup` should not be replaced with `StandardError` or its subclasses.
Also, according to The Open/Closed Principle, `Interrup` class should be
open to inheritance.

The same is true for `SystemStackError`, `NoMemoryError`, `SecurityError`,
`NotImplementedError`, `LoadError`, `SyntaxError`, `ScriptError`,
`SignalException`, and `SystemExit` classes.

This PR allows for exception handling according to Object-oriented principles
and Ruby exception handling mechanism.
@koic koic force-pushed the fix_a_false_positive_for_lint_inherit_exception branch from 0834571 to f2277fc Compare Feb 13, 2022
@bbatsov bbatsov merged commit 02fba0d into rubocop:master Feb 13, 2022
34 checks passed
@bbatsov
Copy link
Collaborator

bbatsov commented Feb 13, 2022

Fair enough, let's simplify this.

@koic koic deleted the fix_a_false_positive_for_lint_inherit_exception branch Feb 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants