From bb3a8ab3d7afcd6bfa67808ff2b7cf7c5215effd Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 10 Feb 2022 00:10:00 +0900 Subject: [PATCH] Change `EnforcedStyle` to `standard_error` for `Lint/InheritException` 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. --- ...ge_enforced_style_for_lint_inherit_exception.md | 1 + config/default.yml | 4 ++-- lib/rubocop/cop/lint/inherit_exception.rb | 14 +++++++------- 3 files changed, 10 insertions(+), 9 deletions(-) create mode 100644 changelog/change_enforced_style_for_lint_inherit_exception.md diff --git a/changelog/change_enforced_style_for_lint_inherit_exception.md b/changelog/change_enforced_style_for_lint_inherit_exception.md new file mode 100644 index 00000000000..3db910ae623 --- /dev/null +++ b/changelog/change_enforced_style_for_lint_inherit_exception.md @@ -0,0 +1 @@ +* [#10407](https://github.com/rubocop/rubocop/pull/10407): Change `EnforcedStyle` from `runtime_error` to `standard_error` for `Lint/InheritException`. ([@koic][]) diff --git a/config/default.yml b/config/default.yml index ade37e44762..7dd3d931e19 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1820,10 +1820,10 @@ Lint/InheritException: VersionAdded: '0.41' VersionChanged: '<>' # 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.' diff --git a/lib/rubocop/cop/lint/inherit_exception.rb b/lib/rubocop/cop/lint/inherit_exception.rb index 06e45f01f6b..53b406c1c0a 100644 --- a/lib/rubocop/cop/lint/inherit_exception.rb +++ b/lib/rubocop/cop/lint/inherit_exception.rb @@ -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 @@ -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 @@ -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