Sha256: 26453480f9de63da59ace3d0c9dbce77c707e2594ef549c0408b1f2ae3616a6e

Contents?: true

Size: 981 Bytes

Versions: 1

Compression:

Stored size: 981 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for `raise` or `fail` statements which are
      # raising `Exception` class.
      #
      # @example
      #   # bad
      #   raise Exception, 'Error message here'
      #
      #   # good
      #   raise StandardError, 'Error message here'
      class RaiseException < Cop
        MSG = 'Use `StandardError` over `Exception`.'

        def_node_matcher :exception?, <<~PATTERN
          (send nil? ${:raise :fail} (const _ :Exception) ... )
        PATTERN

        def_node_matcher :exception_new_with_message?, <<~PATTERN
          (send nil? ${:raise :fail}
            (send (const _ :Exception) :new ... ))
        PATTERN

        def on_send(node)
          add_offense(node) if raise_exception?(node)
        end

        private

        def raise_exception?(node)
          exception?(node) || exception_new_with_message?(node)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-0.81.0 lib/rubocop/cop/lint/raise_exception.rb