Class: RuboCop::Cop::Lint::IncompatibleIoSelectWithFiberScheduler

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

Overview

Checks for ‘IO.select` that is incompatible with Fiber Scheduler since Ruby 3.0.

When an array of IO objects waiting for an exception (the third argument of ‘IO.select`) is used as an argument, there is no alternative API, so offenses are not registered.

NOTE: When the method is successful the return value of ‘IO.select` is `[[IO]]`, and the return value of `io.wait_readable` and `io.wait_writable` are `self`. They are not autocorrected when assigning a return value because these types are different. It’s up to user how to handle the return value.

Examples:


# bad
IO.select([io], [], [], timeout)

# good
io.wait_readable(timeout)

# bad
IO.select([], [io], [], timeout)

# good
io.wait_writable(timeout)

Constant Summary collapse

MSG =
'Use `%<preferred>s` instead of `%<current>s`.'
RESTRICT_ON_SEND =
%i[select].freeze

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_new_investigation, #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

#io_select(node) ⇒ Object



41
42
43
44
# File 'lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb', line 41

def_node_matcher :io_select, <<~PATTERN
  (send
    (const {nil? cbase} :IO) :select $...)
PATTERN

#on_send(node) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb', line 46

def on_send(node)
  read, write, excepts, timeout = *io_select(node)
  return if excepts && !excepts.children.empty?
  return unless scheduler_compatible?(read, write) || scheduler_compatible?(write, read)

  preferred = preferred_method(read, write, timeout)
  message = format(MSG, preferred: preferred, current: node.source)

  add_offense(node, message: message) do |corrector|
    next if node.parent&.assignment?

    corrector.replace(node, preferred)
  end
end