Sha256: 66e89c5f11ef2fc38ef5471ed7dd20e866fe525324fa24e4a74beca156683b06

Contents?: true

Size: 950 Bytes

Versions: 20

Compression:

Stored size: 950 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # This cop identifies places where methods are converted to blocks, with the
      # use of `&method`, and passed as arguments to method calls.
      # It is faster to replace those with explicit blocks, calling those methods inside.
      #
      # @example
      #   # bad
      #   array.map(&method(:do_something))
      #   [1, 2, 3].each(&out.method(:puts))
      #
      #   # good
      #   array.map { |x| do_something(x) }
      #   [1, 2, 3].each { |x| out.puts(x) }
      #
      class MethodObjectAsBlock < Base
        MSG = 'Use block explicitly instead of block-passing a method object.'

        def_node_matcher :method_object_as_argument?, <<~PATTERN
          (^send (send _ :method sym))
        PATTERN

        def on_block_pass(node)
          add_offense(node) if method_object_as_argument?(node)
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 3 rubygems

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