Sha256: 92df753d788a7705e9aa490392a180e845cb7e9dd5d6efc2a27382443c62af3b

Contents?: true

Size: 1.09 KB

Versions: 8

Compression:

Stored size: 1.09 KB

Contents

# encoding: utf-8

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
        include AST::Sexp
        MSG = 'Use the block version of `%s.%s`.'

        TARGET_METHODS = [
          [:File, :open]
        ]

        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?

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rubocop-0.34.1 lib/rubocop/cop/style/auto_resource_cleanup.rb
rubocop-0.34.0 lib/rubocop/cop/style/auto_resource_cleanup.rb
rubocop-0.33.0 lib/rubocop/cop/style/auto_resource_cleanup.rb
rubocop-0.32.1 lib/rubocop/cop/style/auto_resource_cleanup.rb
rubocop-0.32.0 lib/rubocop/cop/style/auto_resource_cleanup.rb
rubocop-0.31.0 lib/rubocop/cop/style/auto_resource_cleanup.rb
rubocop-0.30.1 lib/rubocop/cop/style/auto_resource_cleanup.rb
rubocop-0.30.0 lib/rubocop/cop/style/auto_resource_cleanup.rb