Sha256: 4ebd941b0bd196728f73b65e647495de3fa3d0740105618dc5b7b0a55348dfe1

Contents?: true

Size: 941 Bytes

Versions: 105

Compression:

Stored size: 941 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # 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

105 entries across 103 versions & 7 rubygems

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