class RuboCop::Cop::Style::AutoResourceCleanup

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
File.open('file') do |f|
  # ...
end

Constants

MSG
RESTRICT_ON_SEND
TARGET_METHODS

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/style/auto_resource_cleanup.rb, line 26
def on_send(node)
  TARGET_METHODS.each do |target_class, target_method|
    next if node.method_name != target_method

    target_receiver = s(:const, nil, target_class)
    next if node.receiver != target_receiver

    next if cleanup?(node)

    add_offense(node, message: format(MSG, class: target_class, method: target_method))
  end
end

Private Instance Methods

cleanup?(node) click to toggle source
# File lib/rubocop/cop/style/auto_resource_cleanup.rb, line 41
def cleanup?(node)
  parent = node.parent
  node.block_argument? || (parent && (parent.block_type? || !parent.lvasgn_type?))
end