class RuboCop::Cop::Performance::RedundantMatch

This cop identifies use of `Regexp#match` or `String#match in a context where the integral return value of `=~` would do just as well.

@example

@bad
do_something if str.match(/regex/)
while regex.match('str')
  do_something
end

@good
method(str.match(/regex/))
return regex.match('str')

Constants

MSG

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/performance/redundant_match.rb, line 41
def autocorrect(node)
  # Regexp#match can take a second argument, but this cop doesn't
  # register an offense in that case
  receiver, _method, arg = *node
  new_source = receiver.source + ' =~ ' + arg.source
  ->(corrector) { corrector.replace(node.source_range, new_source) }
end
on_send(node) click to toggle source
# File lib/rubocop/cop/performance/redundant_match.rb, line 34
def on_send(node)
  return unless match_call?(node) &&
                (!node.value_used? || only_truthiness_matters?(node)) &&
                !(node.parent && node.parent.block_type?)
  add_offense(node, :expression, MSG)
end