class RuboCop::Cop::Lint::RedundantDirGlobSort

Sort globbed results by default in Ruby 3.0. This cop checks for redundant ‘sort` method to `Dir.glob` and `Dir[]`.

@safety

This cop is unsafe, in case of having a file and a directory with
identical names, since directory will be loaded before the file, which
will break `exe/files.rb` that rely on `exe.rb` file.

@example

# bad
Dir.glob('./lib/**/*.rb').sort.each do |file|
end

Dir['./lib/**/*.rb'].sort.each do |file|
end

# good
Dir.glob('./lib/**/*.rb').each do |file|
end

Dir['./lib/**/*.rb'].each do |file|
end

Constants

GLOB_METHODS
MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/lint/redundant_dir_glob_sort.rb, line 40
def on_send(node)
  return unless (receiver = node.receiver)
  return unless receiver.receiver&.const_type? && receiver.receiver.short_name == :Dir
  return unless GLOB_METHODS.include?(receiver.method_name)
  return if multiple_argument?(receiver)

  selector = node.loc.selector

  add_offense(selector) do |corrector|
    corrector.remove(selector)
    corrector.remove(node.loc.dot)
  end
end

Private Instance Methods

multiple_argument?(glob_method) click to toggle source
# File lib/rubocop/cop/lint/redundant_dir_glob_sort.rb, line 56
def multiple_argument?(glob_method)
  glob_method.arguments.count >= 2 || glob_method.first_argument&.splat_type?
end