Class: RuboCop::Cop::Style::FileNull

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

Overview

Use ‘File::NULL` instead of hardcoding the null device (`/dev/null` on Unix-like OSes, `NUL` or `NUL:` on Windows), so that code is platform independent. Only looks for full string matches, substrings within a longer string are not considered.

However, only files that use the string ‘’/dev/null’‘ are targeted for detection. This is because the string `’NUL’‘ is not limited to the null device. This behavior results in false negatives when the `’/dev/null’‘ string is not used, but it is a trade-off to avoid false positives. `NULL:` Unlike `’NUL’‘, `’NUL:‘` is regarded as something like `C:` and is always detected.

NOTE: Uses inside arrays and hashes are ignored.

Examples:

# bad
'/dev/null'
'NUL'
'NUL:'

# good
File::NULL

# ok - inside an array
null_devices = %w[/dev/null nul]

# ok - inside a hash
{ unix: "/dev/null", windows: "nul" }

Constant Summary collapse

REGEXP =
%r{\A(/dev/null|NUL:?)\z}i.freeze
MSG =
'Use `File::NULL` instead of `%<source>s`.'

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #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_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, 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

#on_new_investigationObject



51
52
53
54
55
56
57
58
59
# File 'lib/rubocop/cop/style/file_null.rb', line 51

def on_new_investigation
  return unless (ast = processed_source.ast)

  @contain_dev_null_string_in_file = ast.each_descendant(:str).any? do |str|
    content = str.str_content

    valid_string?(content) && content.downcase == '/dev/null' # rubocop:disable Style/FileNull
  end
end

#on_str(node) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubocop/cop/style/file_null.rb', line 61

def on_str(node)
  value = node.value
  return unless valid_string?(value)
  return if acceptable?(node)
  return if value.downcase == 'nul' && !@contain_dev_null_string_in_file # rubocop:disable Style/FileNull
  return unless REGEXP.match?(value)

  add_offense(node, message: format(MSG, source: value)) do |corrector|
    corrector.replace(node, 'File::NULL')
  end
end