Class: RuboCop::Cop::Performance::ConcurrentMonotonicTime

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

Overview

Identifies places where ‘Concurrent.monotonic_time` can be replaced by `Process.clock_gettime(Process::CLOCK_MONOTONIC)`.

Examples:


# bad
Concurrent.monotonic_time

# good
Process.clock_gettime(Process::CLOCK_MONOTONIC)

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/cop/performance/concurrent_monotonic_time.rb', line 28

def on_send(node)
  return unless concurrent_monotonic_time?(node)

  optional_unit_parameter = ", #{node.first_argument.source}" if node.first_argument
  prefer = "Process.clock_gettime(Process::CLOCK_MONOTONIC#{optional_unit_parameter})"
  message = format(MSG, prefer: prefer, current: node.source)

  add_offense(node, message: message) do |corrector|
    corrector.replace(node, prefer)
  end
end