Sha256: bd0871f6ffed19fc731beca0200d43d92aae67675bdbbc5c5abfb5eecf68af61

Contents?: true

Size: 1.39 KB

Versions: 15

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # This cop identifies unnecessary use of a `block_given?` where explicit check
      # of block argument would suffice.
      #
      # @example
      #   # bad
      #   def method(&block)
      #     do_something if block_given?
      #   end
      #
      #   # good
      #   def method(&block)
      #     do_something if block
      #   end
      #
      #   # good - block is reassigned
      #   def method(&block)
      #     block ||= -> { do_something }
      #     warn "Using default ..." unless block_given?
      #     # ...
      #   end
      #
      class BlockGivenWithExplicitBlock < Base
        extend AutoCorrector

        RESTRICT_ON_SEND = %i[block_given?].freeze
        MSG = 'Check block argument explicitly instead of using `block_given?`.'

        def_node_matcher :reassigns_block_arg?, '`(lvasgn %1 ...)'

        def on_send(node)
          def_node = node.each_ancestor(:def, :defs).first
          return unless def_node

          block_arg = def_node.arguments.find(&:blockarg_type?)
          return unless block_arg

          block_arg_name = block_arg.loc.name.source.to_sym
          return if reassigns_block_arg?(def_node, block_arg_name)

          add_offense(node) do |corrector|
            corrector.replace(node, block_arg_name)
          end
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
rubocop-performance-1.13.1 lib/rubocop/cop/performance/block_given_with_explicit_block.rb
rubocop-performance-1.13.0 lib/rubocop/cop/performance/block_given_with_explicit_block.rb
rubocop-performance-1.12.0 lib/rubocop/cop/performance/block_given_with_explicit_block.rb
rubocop-performance-1.11.5 lib/rubocop/cop/performance/block_given_with_explicit_block.rb
rubocop-performance-1.11.4 lib/rubocop/cop/performance/block_given_with_explicit_block.rb
rubocop-performance-1.11.3 lib/rubocop/cop/performance/block_given_with_explicit_block.rb
rubocop-performance-1.11.2 lib/rubocop/cop/performance/block_given_with_explicit_block.rb
rubocop-performance-1.11.1 lib/rubocop/cop/performance/block_given_with_explicit_block.rb
rubocop-performance-1.11.0 lib/rubocop/cop/performance/block_given_with_explicit_block.rb
rubocop-performance-1.10.2 lib/rubocop/cop/performance/block_given_with_explicit_block.rb
rubocop-performance-1.10.1 lib/rubocop/cop/performance/block_given_with_explicit_block.rb
rubocop-performance-1.10.0 lib/rubocop/cop/performance/block_given_with_explicit_block.rb
rubocop-performance-1.9.2 lib/rubocop/cop/performance/block_given_with_explicit_block.rb
rubocop-performance-1.9.1 lib/rubocop/cop/performance/block_given_with_explicit_block.rb
rubocop-performance-1.9.0 lib/rubocop/cop/performance/block_given_with_explicit_block.rb