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

Commits on Feb 13, 2022

  1. Fix a false positive for Lint/InheritException

    Fixes standardrb/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 committed Feb 13, 2022
    Copy the full SHA
    f2277fc View commit details
    Browse the repository at this point in the history