class RuboCop::Cop::Lint::RegexpAsCondition

Checks for regexp literals used as ‘match-current-line`. If a regexp literal is in condition, the regexp matches `$_` implicitly.

@example

# bad
if /foo/
  do_something
end

# good
if /foo/ =~ $_
  do_something
end

Constants

MSG

Public Instance Methods

on_match_current_line(node) click to toggle source
# File lib/rubocop/cop/lint/regexp_as_condition.rb, line 26
def on_match_current_line(node)
  return if node.ancestors.none?(&:conditional?)
  return if part_of_ignored_node?(node)

  add_offense(node) { |corrector| corrector.replace(node, "#{node.source} =~ $_") }

  ignore_node(node)
end