Sha256: a1955e41d41738c29a3ff05a8fd5608f091c8655281baf94ac352f299a7994e2

Contents?: true

Size: 1.67 KB

Versions: 18

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for RuntimeError as the argument of raise/fail.
      #
      # It checks for code like this:
      #
      # @example
      #   # Bad
      #   raise RuntimeError, 'message'
      #
      #   # Bad
      #   raise RuntimeError.new('message')
      #
      #   # Good
      #   raise 'message'
      class RedundantException < Cop
        MSG_1 = 'Redundant `RuntimeError` argument can be removed.'.freeze
        MSG_2 = 'Redundant `RuntimeError.new` call can be replaced with ' \
                'just the message.'.freeze

        def on_send(node)
          exploded?(node) { return add_offense(node, :expression, MSG_1) }
          compact?(node) { add_offense(node, :expression, MSG_2) }
        end

        # Switch `raise RuntimeError, 'message'` to `raise 'message'`, and
        # `raise RuntimeError.new('message')` to `raise 'message'`.
        def autocorrect(node)
          exploded?(node) do |command, message|
            return lambda do |corrector|
              corrector.replace(node.source_range,
                                "#{command} #{message.source}")
            end
          end
          compact?(node) do |new_call, message|
            lambda do |corrector|
              corrector.replace(new_call.source_range, message.source)
            end
          end
        end

        def_node_matcher :exploded?, <<-PATTERN
          (send nil ${:raise :fail} (const nil :RuntimeError) $_)
        PATTERN

        def_node_matcher :compact?, <<-PATTERN
          (send nil {:raise :fail} $(send (const nil :RuntimeError) :new $_))
        PATTERN
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/redundant_exception.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/redundant_exception.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/redundant_exception.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/redundant_exception.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/redundant_exception.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/redundant_exception.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.50.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.49.1 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.49.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.48.1 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.48.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.47.1 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.47.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.46.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.45.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.44.1 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.44.0 lib/rubocop/cop/style/redundant_exception.rb