Class: RuboCop::Cop::Style::FileEmpty

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetRubyVersion
Defined in:
lib/rubocop/cop/style/file_empty.rb

Overview

Prefer to use File.empty?('path/to/file') when checking if a file is empty.

Examples:

# 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')

Cop Safety Information:

  • 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.

Constant Summary collapse

MSG =
'Use `%<file_class>s.empty?(%<arg>s)` instead.'
RESTRICT_ON_SEND =
%i[>= != == zero? empty?].freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from TargetRubyVersion

minimum_target_ruby_version, required_minimum_ruby_version, support_target_ruby_version?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#offensive?(node) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubocop/cop/style/file_empty.rb', line 37

def_node_matcher :offensive?, <<~PATTERN
  {
    (send $(const {nil? cbase} {:File :FileTest}) :zero? $_)
    (send (send $(const {nil? cbase} {:File :FileTest}) :size $_) {:== :>=} (int 0))
    (send (send (send $(const {nil? cbase} {:File :FileTest}) :size $_) :!) {:== :>=} (int 0))
    (send (send $(const {nil? cbase} {:File :FileTest}) :size $_) :zero?)
    (send (send $(const {nil? cbase} {:File :FileTest}) {:read :binread} $_) {:!= :==} (str empty?))
    (send (send (send $(const {nil? cbase} {:File :FileTest}) {:read :binread} $_) :!) {:!= :==} (str empty?))
    (send (send $(const {nil? cbase} {:File :FileTest}) {:read :binread} $_) :empty?)
  }
PATTERN

#on_send(node) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# 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