Sha256: 90e094b291f80798a6a77bf242e8f92351929016d0989db0b9066351678d93fe

Contents?: true

Size: 1.47 KB

Versions: 236

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # In math and Python, we can use `x < y < z` style comparison to compare
      # multiple value. However, we can't use the comparison in Ruby. However,
      # the comparison is not syntax error. This cop checks the bad usage of
      # comparison operators.
      #
      # @example
      #
      #   # bad
      #   x < y < z
      #   10 <= x <= 20
      #
      #   # good
      #   x < y && y < z
      #   10 <= x && x <= 20
      class MultipleComparison < Base
        extend AutoCorrector

        MSG = 'Use the `&&` operator to compare multiple values.'
        COMPARISON_METHODS = %i[< > <= >=].freeze
        SET_OPERATION_OPERATORS = %i[& | ^].freeze
        RESTRICT_ON_SEND = COMPARISON_METHODS

        # @!method multiple_compare?(node)
        def_node_matcher :multiple_compare?, <<~PATTERN
          (send (send _ {:< :> :<= :>=} $_) {:#{COMPARISON_METHODS.join(' :')}} _)
        PATTERN

        def on_send(node)
          return unless (center = multiple_compare?(node))
          # It allows multiple comparison using `&`, `|`, and `^` set operation operators.
          # e.g. `x >= y & y < z`
          return if center.send_type? && SET_OPERATION_OPERATORS.include?(center.method_name)

          add_offense(node) do |corrector|
            new_center = "#{center.source} && #{center.source}"

            corrector.replace(center, new_center)
          end
        end
      end
    end
  end
end

Version data entries

236 entries across 227 versions & 23 rubygems

Version Path
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.91 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.90 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.89 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.88 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.87 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.86 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.85 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.84 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.83 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.82 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.81 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.80 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.79 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.78 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.77 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.76 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.75 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.74 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb
harbr-0.1.73 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/lint/multiple_comparison.rb