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

# bad
f = Tempfile.open('temp')

# good
Tempfile.open('temp') do |f|
  # ...
end

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/style/auto_resource_cleanup.rb, line 36
def on_send(node)
  return if !file_open_method?(node) || cleanup?(node)

  current = node.receiver.source_range.begin.join(node.selector.end).source

  add_offense(node, message: format(MSG, current: current))
end

Private Instance Methods

cleanup?(node) click to toggle source
# File lib/rubocop/cop/style/auto_resource_cleanup.rb, line 46
def cleanup?(node)
  return true if node.block_argument?
  return false unless (parent = node.parent)

  parent.block_type? || !parent.lvasgn_type?
end