Sha256: 6e59db65500087880c5296cc9229b0371ef4b999cbe4f9bd8bc31a6e86759e4c

Contents?: true

Size: 1.4 KB

Versions: 37

Compression:

Stored size: 1.4 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

37 entries across 37 versions & 8 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
rubocop-1.69.2 lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
rubocop-1.69.1 lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
rubocop-1.69.0 lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
rubocop-1.68.0 lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
rubocop-1.67.0 lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
rubocop-1.66.1 lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
rubocop-1.66.0 lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
rubocop-1.65.1 lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
rubocop-1.65.0 lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
rubocop-1.64.1 lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
rubocop-1.63.4 lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
rubocop-1.63.3 lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
rubocop-1.63.2 lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
rubocop-1.63.1 lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
rubocop-1.63.0 lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb