class RuboCop::Cop::Style::FileNull
{ unix: “/dev/null”, windows: “nul” }
# ok - inside a hash
null_devices = %w[/dev/null nul]
# ok - inside an array
File::NULL
# good
’NUL:‘
’NUL’
‘/dev/null’
# bad
@example
—-
path = “/dev/null”
—-
[source,ruby]
‘File::NULL`:
For example, the following string will change on Windows when changed to
on multiple platforms and was previously hardcoded to a specific null device.
It is possible for a string value to be changed if code is being run
@safety
NOTE: Uses inside arrays and hashes are ignored.
Unlike `’NUL’‘, `’NUL:‘` is regarded as something like `C:` and is always detected.
but it is a trade-off to avoid false positives. `NULL:`
This behavior results in false negatives when the `’/dev/null’‘ string is not used,
This is because the string `’NUL’‘ is not limited to the null device.
However, only files that use the string `’/dev/null’‘ are targeted for detection.
considered.
Only looks for full string matches, substrings within a longer string are not
OSes, `NUL` or `NUL:` on Windows), so that code is platform independent.
Use `File::NULL` instead of hardcoding the null device (`/dev/null` on Unix-like
def acceptable?(node)
def acceptable?(node) # Using a hardcoded null device is acceptable when inside an array or # inside a hash to ensure behavior doesn't change. return false unless node.parent node.parent.type?(:array, :pair) end
def on_new_investigation
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
def on_str(node)
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
def valid_string?(value)
def valid_string?(value) !value.empty? && value.valid_encoding? end