Sha256: 3bdc09c017688695370178f148029bb207b518d0d50ce75c119038c1e6c73cb4

Contents?: true

Size: 1.03 KB

Versions: 22

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # This cop 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` instead of `%<bad_method>s`.'

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

          replaceable_body?(body, var_a, var_b) do
            range = sort_range(send, node)

            add_offense(range, message: message(var_a, var_b)) do |corrector|
              corrector.replace(range, 'sort')
            end
          end
        end

        private

        def message(var_a, var_b)
          bad_method = "sort { |#{var_a}, #{var_b}| #{var_a} <=> #{var_b} }"
          format(MSG, bad_method: bad_method)
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 3 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-performance-1.13.3/lib/rubocop/cop/performance/redundant_sort_block.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-performance-1.13.3/lib/rubocop/cop/performance/redundant_sort_block.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-performance-1.13.3/lib/rubocop/cop/performance/redundant_sort_block.rb
rubocop-performance-1.13.3 lib/rubocop/cop/performance/redundant_sort_block.rb
rubocop-performance-1.13.2 lib/rubocop/cop/performance/redundant_sort_block.rb
rubocop-performance-1.13.1 lib/rubocop/cop/performance/redundant_sort_block.rb
rubocop-performance-1.13.0 lib/rubocop/cop/performance/redundant_sort_block.rb
rubocop-performance-1.12.0 lib/rubocop/cop/performance/redundant_sort_block.rb
rubocop-performance-1.11.5 lib/rubocop/cop/performance/redundant_sort_block.rb
rubocop-performance-1.11.4 lib/rubocop/cop/performance/redundant_sort_block.rb
rubocop-performance-1.11.3 lib/rubocop/cop/performance/redundant_sort_block.rb
rubocop-performance-1.11.2 lib/rubocop/cop/performance/redundant_sort_block.rb
rubocop-performance-1.11.1 lib/rubocop/cop/performance/redundant_sort_block.rb
rubocop-performance-1.11.0 lib/rubocop/cop/performance/redundant_sort_block.rb
rubocop-performance-1.10.2 lib/rubocop/cop/performance/redundant_sort_block.rb
rubocop-performance-1.10.1 lib/rubocop/cop/performance/redundant_sort_block.rb
rubocop-performance-1.10.0 lib/rubocop/cop/performance/redundant_sort_block.rb
rubocop-performance-1.9.2 lib/rubocop/cop/performance/redundant_sort_block.rb
rubocop-performance-1.9.1 lib/rubocop/cop/performance/redundant_sort_block.rb
rubocop-performance-1.9.0 lib/rubocop/cop/performance/redundant_sort_block.rb