class RuboCop::Cop::Style::RedundantRegexpConstructor

Checks for the instantiation of regexp using redundant ‘Regexp.new` or `Regexp.compile`. Autocorrect replaces to regexp literal which is the simplest and fastest.

@example

# bad
Regexp.new(/regexp/)
Regexp.compile(/regexp/)

# good
/regexp/
Regexp.new('regexp')
Regexp.compile('regexp')

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/style/redundant_regexp_constructor.rb, line 33
def on_send(node)
  return unless (regexp, regopt = redundant_regexp_constructor(node))

  add_offense(node, message: format(MSG, method: node.method_name)) do |corrector|
    pattern = regexp.map(&:source).join
    regopt = regopt.join

    corrector.replace(node, "/#{pattern}/#{regopt}")
  end
end