Sha256: 22a21e730248563c6f5b64a4701ccadba5c7f8782807b4256ad50d8029f97d4f

Contents?: true

Size: 1.14 KB

Versions: 97

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # Identifies places where `sort { |a, b| a <=> b }` can be replaced with `sort`.
      #
      # @example
      #   # bad
      #   array.sort { |a, b| a <=> b }
      #
      #   # good
      #   array.sort
      #
      class RedundantSortBlock < Base
        include SortBlock
        extend AutoCorrector

        MSG = 'Use `sort` without block.'

        def on_block(node)
          return unless (send, var_a, var_b, body = sort_with_block?(node))

          replaceable_body?(body, var_a, var_b) do
            register_offense(send, node)
          end
        end

        def on_numblock(node)
          return unless (send, arg_count, body = sort_with_numblock?(node))
          return unless arg_count == 2

          replaceable_body?(body, :_1, :_2) do
            register_offense(send, node)
          end
        end

        private

        def register_offense(send, node)
          range = sort_range(send, node)

          add_offense(range) do |corrector|
            corrector.replace(range, 'sort')
          end
        end
      end
    end
  end
end

Version data entries

97 entries across 97 versions & 7 rubygems

Version Path
harbr-0.1.80 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.79 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.78 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.77 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.76 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.75 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.74 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.73 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.72 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.71 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.70 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.69 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.68 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.67 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.66 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.65 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.64 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.63 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.62 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb
harbr-0.1.61 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_sort_block.rb