class RuboCop::Cop::Style::FileEmpty
Prefer to use ‘File.empty?(’path/to/file’)‘ when checking if a file is empty.
@safety
This cop is unsafe, because `File.size`, `File.read`, and `File.binread` raise `ENOENT` exception when there is no file corresponding to the path, while `File.empty?` does not raise an exception.
@example
# bad File.zero?('path/to/file') File.size('path/to/file') == 0 File.size('path/to/file') >= 0 File.size('path/to/file').zero? File.read('path/to/file').empty? File.binread('path/to/file') == '' FileTest.zero?('path/to/file') # good File.empty?('path/to/file') FileTest.empty?('path/to/file')
Constants
- MSG
- RESTRICT_ON_SEND
Public Instance Methods
Source
# File lib/rubocop/cop/style/file_empty.rb, line 49 def on_send(node) offensive?(node) do |const_node, arg_node| add_offense(node, message: format(MSG, file_class: const_node.source, arg: arg_node.source)) do |corrector| corrector.replace(node, "#{bang(node)}#{const_node.source}.empty?(#{arg_node.source})") end end end
Private Instance Methods
Source
# File lib/rubocop/cop/style/file_empty.rb, line 62 def bang(node) if (node.method?(:==) && node.child_nodes.first.method?(:!)) || (%i[>= !=].include?(node.method_name) && !node.child_nodes.first.method?(:!)) '!' end end