Sha256: 263e4bbe04b9bed695a7859a6590d8bcaf057c3c6b27102cc854e236bef07055

Contents?: true

Size: 770 Bytes

Versions: 26

Compression:

Stored size: 770 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # 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 < Base
        extend AutoCorrector

        MSG = 'Do not use regexp literal as a condition. ' \
              'The regexp literal matches `$_` implicitly.'

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

Version data entries

26 entries across 22 versions & 3 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/regexp_as_condition.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/regexp_as_condition.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/regexp_as_condition.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/lint/regexp_as_condition.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/regexp_as_condition.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/lint/regexp_as_condition.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/lint/regexp_as_condition.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/regexp_as_condition.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/lint/regexp_as_condition.rb
rubocop-1.41.1 lib/rubocop/cop/lint/regexp_as_condition.rb
rubocop-1.41.0 lib/rubocop/cop/lint/regexp_as_condition.rb
rubocop-1.40.0 lib/rubocop/cop/lint/regexp_as_condition.rb
rubocop-1.39.0 lib/rubocop/cop/lint/regexp_as_condition.rb
rubocop-1.38.0 lib/rubocop/cop/lint/regexp_as_condition.rb
rubocop-1.37.1 lib/rubocop/cop/lint/regexp_as_condition.rb
rubocop-1.37.0 lib/rubocop/cop/lint/regexp_as_condition.rb
rubocop-1.36.0 lib/rubocop/cop/lint/regexp_as_condition.rb
rubocop-1.35.1 lib/rubocop/cop/lint/regexp_as_condition.rb
rubocop-1.35.0 lib/rubocop/cop/lint/regexp_as_condition.rb
rubocop-1.34.1 lib/rubocop/cop/lint/regexp_as_condition.rb