Sha256: a428a7cf15efeaeadbd5850919dcf277fde8deea19fb62023c975b782d3d2a4d

Contents?: true

Size: 1.88 KB

Versions: 73

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # Identifies uses of `Range#include?` and `Range#member?`, which iterates over each
      # item in a `Range` to see if a specified item is there. In contrast,
      # `Range#cover?` simply compares the target item with the beginning and
      # end points of the `Range`. In a great majority of cases, this is what
      # is wanted.
      #
      # @safety
      #   This cop is unsafe because `Range#include?` (or `Range#member?`) and `Range#cover?`
      #   are not equivalent behavior.
      #   Example of a case where `Range#cover?` may not provide the desired result:
      #
      #   [source,ruby]
      #   ----
      #   ('a'..'z').cover?('yellow') # => true
      #   ----
      #
      # @example
      #   # bad
      #   ('a'..'z').include?('b') # => true
      #   ('a'..'z').member?('b')  # => true
      #
      #   # good
      #   ('a'..'z').cover?('b') # => true
      class RangeInclude < Base
        extend AutoCorrector

        MSG = 'Use `Range#cover?` instead of `Range#%<bad_method>s`.'
        RESTRICT_ON_SEND = %i[include? member?].freeze

        # TODO: If we traced out assignments of variables to their uses, we
        # might pick up on a few more instances of this issue
        # Right now, we only detect direct calls on a Range literal
        # (We don't even catch it if the Range is in double parens)

        def_node_matcher :range_include, <<~PATTERN
          (send {irange erange (begin {irange erange})} ${:include? :member?} ...)
        PATTERN

        def on_send(node)
          range_include(node) do |bad_method|
            message = format(MSG, bad_method: bad_method)

            add_offense(node.loc.selector, message: message) do |corrector|
              corrector.replace(node.loc.selector, 'cover?')
            end
          end
        end
      end
    end
  end
end

Version data entries

73 entries across 73 versions & 2 rubygems

Version Path
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.1.99 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.1.94 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb
harbr-0.1.91 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/range_include.rb