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
Running the "standardrb --fix" command changes "Class.new(Interrupt)" to "Class.new(RuntimeError)" (v1.1.5) #388
Comments
This definitely seems weird. @koic does this look like an unhandled edge case in the associated cop? |
As far as I investigated, this behavior was introduced by rubocop/rubocop#3427. But the behavior seems weird to me too. I will more investigate it. |
I've opened rubocop/rubocop#10406 to resolve this issue. |
I've added there a couple of comments myself. Seems to me that the gist of it as:
|
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.
Fixes standardrb/standard#388 and reverts #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.
The fix is in the latest version of standard now too 😄 |
Thanks, folks! :) |
First of all - thanks for the great work. We use your product all the time and we love it :)
Steps to Reproduce
When the following file is formatted with the
standardrb --fix
command:Interrupt
is changed toRuntimeError
Why Is This a Bug?
Standard is a Ruby style guide, linter, and formatter. The behavior outlined in this issue demonstrates that the formatter has changed code semantics and not style.
The text was updated successfully, but these errors were encountered: