Class: RuboCop::Cop::Lint::ShadowedException

Inherits:
Base
  • Object
show all
Includes:
RangeHelp, RescueNode
Defined in:
lib/rubocop/cop/lint/shadowed_exception.rb

Overview

Checks for a rescued exception that get shadowed by a less specific exception being rescued before a more specific exception is rescued.

An exception is considered shadowed if it is rescued after its ancestor is, or if it and its ancestor are both rescued in the same rescue statement. In both cases, the more specific rescue is unnecessary because it is covered by rescuing the less specific exception. (ie. rescue Exception, StandardError has the same behavior whether StandardError is included or not, because all StandardErrors are rescued by rescue Exception).

Examples:


# bad

begin
  something
rescue Exception
  handle_exception
rescue StandardError
  handle_standard_error
end

# bad
begin
  something
rescue Exception, StandardError
  handle_error
end

# good

begin
  something
rescue StandardError
  handle_standard_error
rescue Exception
  handle_exception
end

# good, however depending on runtime environment.
#
# This is a special case for system call errors.
# System dependent error code depends on runtime environment.
# For example, whether `Errno::EAGAIN` and `Errno::EWOULDBLOCK` are
# the same error code or different error code depends on environment.
# This good case is for `Errno::EAGAIN` and `Errno::EWOULDBLOCK` with
# the same error code.
begin
  something
rescue Errno::EAGAIN, Errno::EWOULDBLOCK
  handle_standard_error
end

Constant Summary collapse

MSG =
'Do not shadow rescued Exceptions.'

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from RescueNode

#modifier_locations

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#on_rescue(node) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rubocop/cop/lint/shadowed_exception.rb', line 67

def on_rescue(node)
  return if rescue_modifier?(node)

  _body, *rescues, _else = *node
  rescued_groups = rescued_groups_for(rescues)

  rescue_group_rescues_multiple_levels = rescued_groups.any? do |group|
    contains_multiple_levels_of_exceptions?(group)
  end

  return if !rescue_group_rescues_multiple_levels && sorted?(rescued_groups)

  add_offense(offense_range(rescues))
end