Sha256: bce1cbb75dfa635144b231f854d5fe0aa4766e33cbb830b5d6f631371dbf8d0b

Contents?: true

Size: 1.11 KB

Versions: 70

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks that there are no repeated exceptions
      # used in 'rescue' expressions.
      #
      # @example
      #   # bad
      #   begin
      #     something
      #   rescue FirstException
      #     handle_exception
      #   rescue FirstException
      #     handle_other_exception
      #   end
      #
      #   # good
      #   begin
      #     something
      #   rescue FirstException
      #     handle_exception
      #   rescue SecondException
      #     handle_other_exception
      #   end
      #
      class DuplicateRescueException < Base
        include RescueNode

        MSG = 'Duplicate `rescue` exception detected.'

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

          node.resbody_branches.each_with_object(Set.new) do |resbody, previous|
            rescued_exceptions = resbody.exceptions

            rescued_exceptions.each do |exception|
              add_offense(exception) unless previous.add?(exception)
            end
          end
        end
      end
    end
  end
end

Version data entries

70 entries across 70 versions & 7 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/duplicate_rescue_exception.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/duplicate_rescue_exception.rb
rubocop-1.29.1 lib/rubocop/cop/lint/duplicate_rescue_exception.rb
rubocop-1.29.0 lib/rubocop/cop/lint/duplicate_rescue_exception.rb
rubocop-1.28.2 lib/rubocop/cop/lint/duplicate_rescue_exception.rb
rubocop-1.28.1 lib/rubocop/cop/lint/duplicate_rescue_exception.rb
rubocop-1.28.0 lib/rubocop/cop/lint/duplicate_rescue_exception.rb
rubocop-1.27.0 lib/rubocop/cop/lint/duplicate_rescue_exception.rb
rubocop-1.26.1 lib/rubocop/cop/lint/duplicate_rescue_exception.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/duplicate_rescue_exception.rb
rubocop-1.26.0 lib/rubocop/cop/lint/duplicate_rescue_exception.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/duplicate_rescue_exception.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/duplicate_rescue_exception.rb
rubocop-1.25.1 lib/rubocop/cop/lint/duplicate_rescue_exception.rb
rubocop-1.25.0 lib/rubocop/cop/lint/duplicate_rescue_exception.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/duplicate_rescue_exception.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/duplicate_rescue_exception.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/lint/duplicate_rescue_exception.rb
rubocop-1.24.1 lib/rubocop/cop/lint/duplicate_rescue_exception.rb
rubocop-1.24.0 lib/rubocop/cop/lint/duplicate_rescue_exception.rb