Sha256: 16ad21eec439a30e7a809004b5eb52bc199b756a4ddcfcd7dee4d132fc23b07c

Contents?: true

Size: 1.89 KB

Versions: 38

Compression:

Stored size: 1.89 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

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

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

Version data entries

38 entries across 38 versions & 6 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/redundant_exception.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.29.1 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.29.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.28.2 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.28.1 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.28.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.27.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.26.1 lib/rubocop/cop/style/redundant_exception.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.26.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.25.1 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.25.0 lib/rubocop/cop/style/redundant_exception.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.24.1 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.24.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.23.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.22.3 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.22.2 lib/rubocop/cop/style/redundant_exception.rb
rubocop-1.22.1 lib/rubocop/cop/style/redundant_exception.rb