class RuboCop::Cop::Style::FileEmpty
FileTest.empty?(‘path/to/file’)
File.empty?(‘path/to/file’)
# good
FileTest.zero?(‘path/to/file’)
File.binread(‘path/to/file’) == ”
File.read(‘path/to/file’).empty?
File.size(‘path/to/file’).zero?
File.size(‘path/to/file’) >= 0
File.size(‘path/to/file’) == 0
File.zero?(‘path/to/file’)
# bad
@example
while ‘File.empty?` does not raise an exception.
raise `ENOENT` exception when there is no file corresponding to the path,
This cop is unsafe, because `File.size`, `File.read`, and `File.binread`
@safety
Prefer to use `File.empty?(’path/to/file’)‘ when checking if a file is empty.
def bang(node)
def bang(node) if (node.method?(:==) && node.child_nodes.first.method?(:!)) || (%i[>= !=].include?(node.method_name) && !node.child_nodes.first.method?(:!)) '!' end end
def on_send(node)
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