Sha256: 74f7bf8286375e4bc1c42576a1f3a54f49a1e6c1e9c1816947bc1db677fb278e

Contents?: true

Size: 1.17 KB

Versions: 10

Compression:

Stored size: 1.17 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
      #
      # @example
      #
      #   # good
      #
      #   x < y && y < z
      #   10 <= x && x <= 20
      class MultipleComparison < Cop
        MSG = 'Use the `&&` operator to compare multiple values.'

        def_node_matcher :multiple_compare?, <<~PATTERN
          (send (send _ {:< :> :<= :>=} $_) {:< :> :<= :>=} _)
        PATTERN

        def on_send(node)
          return unless multiple_compare?(node)

          add_offense(node)
        end

        def autocorrect(node)
          center = multiple_compare?(node)
          new_center = "#{center.source} && #{center.source}"

          lambda do |corrector|
            corrector.replace(center.source_range, new_center)
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 9 versions & 3 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/lint/multiple_comparison.rb
rubocop-0.81.0 lib/rubocop/cop/lint/multiple_comparison.rb
rubocop-0.80.1 lib/rubocop/cop/lint/multiple_comparison.rb
rubocop-0.80.0 lib/rubocop/cop/lint/multiple_comparison.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/lint/multiple_comparison.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/lint/multiple_comparison.rb
rubocop-0.79.0 lib/rubocop/cop/lint/multiple_comparison.rb
rubocop-0.78.0 lib/rubocop/cop/lint/multiple_comparison.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/lint/multiple_comparison.rb
rubocop-0.77.0 lib/rubocop/cop/lint/multiple_comparison.rb