Sha256: b9313f0bbb10c8c93228a530883eae4b0710316374468b4882253e0a30dd9b68

Contents?: true

Size: 833 Bytes

Versions: 5

Compression:

Stored size: 833 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop 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
      class RegexpAsCondition < Cop
        MSG = 'Do not use regexp literal as a condition.' \
              ' The regexp literal matches `$_` implicitly.'

        def on_match_current_line(node)
          add_offense(node)
        end

        def autocorrect(node)
          lambda do |corrector|
            corrector.replace(node, "#{node.source} =~ $_")
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
rubocop-0.88.0 lib/rubocop/cop/lint/regexp_as_condition.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/lint/regexp_as_condition.rb
rubocop-0.87.1 lib/rubocop/cop/lint/regexp_as_condition.rb
rubocop-0.87.0 lib/rubocop/cop/lint/regexp_as_condition.rb
rubocop-0.86.0 lib/rubocop/cop/lint/regexp_as_condition.rb