From 0970cc155bc9fd70742220cd6517a90725a7ebc2 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 29 Apr 2023 09:20:54 +0900 Subject: [PATCH] Fix a false positive for `Lint/IncompatibleIoSelectWithFiberScheduler` This PR fixes a false positive for `Lint/IncompatibleIoSelectWithFiberScheduler` when using excepts argument. AFAIK, `IO.wait_readable` and `IO.wait_writable` cannot replace IO objects waiting for exceptions. --- ...for_lint_incompatible_io_select_with_fiber_scheduler.md | 1 + .../lint/incompatible_io_select_with_fiber_scheduler.rb | 7 +++++-- .../incompatible_io_select_with_fiber_scheduler_spec.rb | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_a_false_positive_for_lint_incompatible_io_select_with_fiber_scheduler.md diff --git a/changelog/fix_a_false_positive_for_lint_incompatible_io_select_with_fiber_scheduler.md b/changelog/fix_a_false_positive_for_lint_incompatible_io_select_with_fiber_scheduler.md new file mode 100644 index 000000000000..4b050c48d74b --- /dev/null +++ b/changelog/fix_a_false_positive_for_lint_incompatible_io_select_with_fiber_scheduler.md @@ -0,0 +1 @@ +* [#11830](https://github.com/rubocop/rubocop/pull/11830): Fix a false positive for `Lint/IncompatibleIoSelectWithFiberScheduler`. ([@koic][]) diff --git a/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb b/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb index c12546fcac72..871aa36a9fc8 100644 --- a/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +++ b/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb @@ -6,6 +6,9 @@ module Lint # # This cop 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. @@ -42,8 +45,8 @@ class IncompatibleIoSelectWithFiberScheduler < Base PATTERN def on_send(node) - read, write, _excepts, timeout = *io_select(node) - return unless read + 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) diff --git a/spec/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler_spec.rb b/spec/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler_spec.rb index 0483933955f4..eee4aa54000c 100644 --- a/spec/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler_spec.rb +++ b/spec/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler_spec.rb @@ -164,6 +164,12 @@ RUBY end + it 'does not register an offense when using `IO.select` with read and excepts arguments' do + expect_no_offenses(<<~RUBY) + IO.select([rp], [], [excepts]) + RUBY + end + it 'does not register an offense when using `Enumerable#select`' do expect_no_offenses(<<~RUBY) collection.select { |item| item.do_something? }