class RuboCop::Cop::Style::RedundantException

This cop checks for RuntimeError as the argument of raise/fail.

Currently it checks for code like this:

@example

raise RuntimeError, 'message'

Constants

MSG
TARGET_NODE

Public Instance Methods

autocorrect(node) click to toggle source

switch `raise RuntimeError, 'message'` to `raise 'message'`

# File lib/rubocop/cop/style/redundant_exception.rb, line 31
def autocorrect(node)
  start_range = node.source_range.begin
  no_comma = range_with_surrounding_comma(node.source_range.end, :right)
  comma_range = start_range.join(no_comma)
  final_range = range_with_surrounding_space(comma_range, :right)
  ->(corrector) { corrector.replace(final_range, '') }
end
on_send(node) click to toggle source
# File lib/rubocop/cop/style/redundant_exception.rb, line 18
def on_send(node)
  return unless node.command?(:raise) || node.command?(:fail)

  _receiver, _selector, *args = *node

  return unless args.size == 2

  first_arg, = *args

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