Sha256: 1ddf5b6a6fe27cdf9eb289e22e1171bc89c08072f3f035e99cc7a1c0eac71750

Contents?: true

Size: 1.39 KB

Versions: 152

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks for overwriting an exception with an exception result by use `rescue =>`.
      #
      # You intended to write as `rescue StandardError`.
      # However, you have written `rescue => StandardError`.
      # In that case, the result of `rescue` will overwrite `StandardError`.
      #
      # @example
      #
      #   # bad
      #   begin
      #     something
      #   rescue => StandardError
      #   end
      #
      #   # good
      #   begin
      #     something
      #   rescue StandardError
      #   end
      #
      class ConstantOverwrittenInRescue < Base
        extend AutoCorrector
        include RangeHelp

        MSG = '`%<constant>s` is overwritten by `rescue =>`.'

        # @!method overwritten_constant(node)
        def_node_matcher :overwritten_constant, <<~PATTERN
          (resbody nil? (casgn nil? $_) nil?)
        PATTERN

        def self.autocorrect_incompatible_with
          [Naming::RescuedExceptionsVariableName, Style::RescueStandardError]
        end

        def on_resbody(node)
          return unless (constant = overwritten_constant(node))

          add_offense(node.loc.assoc, message: format(MSG, constant: constant)) do |corrector|
            corrector.remove(range_between(node.loc.keyword.end_pos, node.loc.assoc.end_pos))
          end
        end
      end
    end
  end
end

Version data entries

152 entries across 145 versions & 13 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
harbr-0.1.99 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb