Sha256: 1da6cd474a22da23427b23a91d36f28d7f2e4fcfe0b0543c9e9f8b8f7a70429c

Contents?: true

Size: 1.91 KB

Versions: 30

Compression:

Stored size: 1.91 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 < Base
        extend AutoCorrector

        MSG_1 = 'Redundant `RuntimeError` argument can be removed.'
        MSG_2 = 'Redundant `RuntimeError.new` call can be replaced with ' \
                'just the message.'

        RESTRICT_ON_SEND = %i[raise fail].freeze

        # Switch `raise RuntimeError, 'message'` to `raise 'message'`, and
        # `raise RuntimeError.new('message')` to `raise 'message'`.
        def on_send(node)
          fix_exploded(node) || fix_compact(node)
        end

        def fix_exploded(node)
          exploded?(node) do |command, message|
            add_offense(node, message: MSG_1) do |corrector|
              if node.parenthesized?
                corrector.replace(node,
                                  "#{command}(#{message.source})")
              else
                corrector.replace(node,
                                  "#{command} #{message.source}")
              end
            end
          end
        end

        def fix_compact(node)
          compact?(node) do |new_call, message|
            add_offense(node, message: MSG_2) do |corrector|
              corrector.replace(new_call, message.source)
            end
          end
        end

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

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

Version data entries

30 entries across 30 versions & 2 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_exception.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_exception.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_exception.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_exception.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_exception.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.10.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.9.1 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.9.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.8.1 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.8.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.7.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.6.1 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.6.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.5.2 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.5.1 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.5.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.4.2 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.4.1 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.4.0 lib/rubocop/cop/style/redundant_exception.rb