Sha256: 71a281aacfbf35a8e86bb5b6f28c4e4b9fb4165591be7195274c13fdeddd1d91

Contents?: true

Size: 1.72 KB

Versions: 13

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # 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')
      class RedundantMatch < Cop
        MSG = 'Use `=~` in places where the `MatchData` returned by ' \
              '`#match` will not be used.'.freeze

        # 'match' is a fairly generic name, so we don't flag it unless we see
        # a string or regexp literal on one side or the other
        def_node_matcher :match_call?, <<-END
          {(send {str regexp} :match _)
           (send !nil :match {str regexp})}
        END

        def_node_matcher :only_truthiness_matters?, <<-END
          ^({if while until case while_post until_post} equal?(%0) ...)
        END

        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

        def autocorrect(node)
          receiver, _method, arg = *node

          # Regexp#match can take a second argument, but this cop doesn't
          # register an offense in that case
          return unless arg.regexp_type?

          new_source = receiver.source + ' =~ ' + arg.source
          ->(corrector) { corrector.replace(node.source_range, new_source) }
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/redundant_match.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/redundant_match.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/redundant_match.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/redundant_match.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/redundant_match.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/redundant_match.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/performance/redundant_match.rb
rubocop-0.47.1 lib/rubocop/cop/performance/redundant_match.rb
rubocop-0.47.0 lib/rubocop/cop/performance/redundant_match.rb
rubocop-0.46.0 lib/rubocop/cop/performance/redundant_match.rb
rubocop-0.45.0 lib/rubocop/cop/performance/redundant_match.rb
rubocop-0.44.1 lib/rubocop/cop/performance/redundant_match.rb
rubocop-0.44.0 lib/rubocop/cop/performance/redundant_match.rb