Sha256: 38d9db13cd47b762c720219a2848a29eb04c9da04c56c711d43659b69e4b4042

Contents?: true

Size: 1.21 KB

Versions: 9

Compression:

Stored size: 1.21 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Style
      # This cop checks for RuntimeError as the argument of raise/fail.
      #
      # Currently it checks for code like this:
      #
      # @example
      #
      #   raise RuntimeError, 'message'
      class RedundantException < Cop
        MSG = 'Redundant `RuntimeError` argument can be removed.'

        TARGET_NODE = s(:const, nil, :RuntimeError)

        def on_send(node)
          return unless command?(:raise, node) || command?(:fail, node)

          _receiver, _selector, *args = *node

          return unless args.size == 2

          first_arg, = *args

          add_offense(first_arg, :expression) if first_arg == TARGET_NODE
        end

        # switch `raise RuntimeError, 'message'` to `raise 'message'`
        def autocorrect(node)
          start_range = node.loc.expression.begin
          no_comma = range_with_surrounding_comma(node.loc.expression.end,
                                                  :right)
          comma_range = start_range.join(no_comma)
          final_range = range_with_surrounding_space(comma_range, :right)
          ->(corrector) { corrector.replace(final_range, '') }
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.35.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.34.2 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.34.1 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.34.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.33.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.32.1 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.32.0 lib/rubocop/cop/style/redundant_exception.rb
rubocop-0.31.0 lib/rubocop/cop/style/redundant_exception.rb