Sha256: ac9a1cedf0ee44bf1b776c83a8abff7b4b37b7500f4b6f3535c782a07bfe1b1c

Contents?: true

Size: 1.87 KB

Versions: 10

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for `rescue` blocks with no body.
      #
      # @example
      #
      #   # bad
      #   def some_method
      #     do_something
      #   rescue
      #   end
      #
      #   # bad
      #   begin
      #     do_something
      #   rescue
      #   end
      #
      #   # good
      #   def some_method
      #     do_something
      #   rescue
      #     handle_exception
      #   end
      #
      #   # good
      #   begin
      #     do_something
      #   rescue
      #     handle_exception
      #   end
      #
      # @example AllowComments: true (default)
      #
      #   # good
      #   def some_method
      #     do_something
      #   rescue
      #     # do nothing
      #   end
      #
      #   # good
      #   begin
      #     do_something
      #   rescue
      #     # do nothing
      #   end
      #
      # @example AllowComments: false
      #
      #   # bad
      #   def some_method
      #     do_something
      #   rescue
      #     # do nothing
      #   end
      #
      #   # bad
      #   begin
      #     do_something
      #   rescue
      #     # do nothing
      #   end
      class SuppressedException < Cop
        MSG = 'Do not suppress exceptions.'

        def on_resbody(node)
          return if node.body
          return if cop_config['AllowComments'] && comment_between_rescue_and_end?(node)

          add_offense(node)
        end

        private

        def comment_between_rescue_and_end?(node)
          end_line = nil
          node.each_ancestor(:kwbegin, :def, :defs, :block) do |ancestor|
            end_line = ancestor.loc.end.line
            break
          end
          return false unless end_line

          processed_source[node.first_line...end_line].any? { |line| comment_line?(line) }
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
rubocop-0.88.0 lib/rubocop/cop/lint/suppressed_exception.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/lint/suppressed_exception.rb
rubocop-0.87.1 lib/rubocop/cop/lint/suppressed_exception.rb
rubocop-0.87.0 lib/rubocop/cop/lint/suppressed_exception.rb
rubocop-0.86.0 lib/rubocop/cop/lint/suppressed_exception.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/lint/suppressed_exception.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/lint/suppressed_exception.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/lint/suppressed_exception.rb
rubocop-0.85.1 lib/rubocop/cop/lint/suppressed_exception.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/lint/suppressed_exception.rb