Sha256: 86fc9b60c993035eef43ce6b7e07d5ebd6ce57f564f430fc1992035bf447ce5c

Contents?: true

Size: 1.16 KB

Versions: 13

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for cases when you could use a block
      # accepting version of a method that does automatic
      # resource cleanup.
      #
      # @example
      #
      #   # bad
      #   f = File.open('file')
      #
      #   # good
      #   f = File.open('file') do
      #     ...
      #   end
      class AutoResourceCleanup < Cop
        MSG = 'Use the block version of `%s.%s`.'.freeze

        TARGET_METHODS = [
          [:File, :open]
        ].freeze

        def on_send(node)
          receiver_node, method_name, *arg_nodes = *node

          TARGET_METHODS.each do |(target_class, target_method)|
            target_receiver = s(:const, nil, target_class)

            next if receiver_node != target_receiver
            next if method_name != target_method
            next if node.parent && node.parent.block_type?
            next if !arg_nodes.empty? && arg_nodes.last.block_pass_type?

            add_offense(node,
                        :expression,
                        format(MSG, target_class, target_method))
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/auto_resource_cleanup.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/auto_resource_cleanup.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/auto_resource_cleanup.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/auto_resource_cleanup.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/auto_resource_cleanup.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/auto_resource_cleanup.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/auto_resource_cleanup.rb
rubocop-0.47.1 lib/rubocop/cop/style/auto_resource_cleanup.rb
rubocop-0.47.0 lib/rubocop/cop/style/auto_resource_cleanup.rb
rubocop-0.46.0 lib/rubocop/cop/style/auto_resource_cleanup.rb
rubocop-0.45.0 lib/rubocop/cop/style/auto_resource_cleanup.rb
rubocop-0.44.1 lib/rubocop/cop/style/auto_resource_cleanup.rb
rubocop-0.44.0 lib/rubocop/cop/style/auto_resource_cleanup.rb