forked from rubocop/rubocop-performance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrent_monotonic_time.rb
42 lines (36 loc) · 1.22 KB
/
concurrent_monotonic_time.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true
module RuboCop
module Cop
module Performance
# Identifies places where `Concurrent.monotonic_time`
# can be replaced by `Process.clock_gettime(Process::CLOCK_MONOTONIC)`.
#
# @example
#
# # bad
# Concurrent.monotonic_time
#
# # good
# Process.clock_gettime(Process::CLOCK_MONOTONIC)
#
class ConcurrentMonotonicTime < Base
extend AutoCorrector
MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
RESTRICT_ON_SEND = %i[monotonic_time].freeze
def_node_matcher :concurrent_monotonic_time?, <<~PATTERN
(send
(const {nil? cbase} :Concurrent) :monotonic_time ...)
PATTERN
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
end
end
end
end